Skip to main content

monarch_hyperactor/
metrics.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9//! Metrics for Python actor endpoints.
10//!
11//! This module contains metrics definitions for tracking Python actor endpoint performance.
12
13use hyperactor::id::Label;
14use hyperactor_telemetry::declare_static_counter;
15use hyperactor_telemetry::declare_static_histogram;
16use opentelemetry::KeyValue;
17
18/// Stands in for a name an endpoint cannot supply.
19pub(crate) const UNKNOWN: &str = "unknown";
20
21/// The attributes carried by every endpoint metric:
22///   - the endpoint's method;
23///   - the actor it belongs to. Specifically, this is the name users use to
24///     spawn their mesh, not the actor ID, which contains additional information
25///     such as channel address.
26pub(crate) struct EndpointAttrs([KeyValue; 2]);
27
28impl EndpointAttrs {
29    pub(crate) fn new(method: &str, actor: Option<&Label>) -> Self {
30        let actor = actor.map_or_else(|| UNKNOWN.to_owned(), |actor| actor.as_str().to_owned());
31        Self([
32            KeyValue::new("method", method.to_owned()),
33            KeyValue::new("actor", actor),
34        ])
35    }
36
37    pub(crate) fn as_slice(&self) -> &[KeyValue] {
38        &self.0
39    }
40}
41
42// ENDPOINT METRICS
43// Tracks the size of endpoint messages in bytes
44declare_static_histogram!(ENDPOINT_MESSAGE_SIZE_HISTOGRAM, "endpoint_message_size");
45// Tracks latency of endpoint calls from the caller's perspective in microseconds
46declare_static_histogram!(
47    ENDPOINT_CALL_LATENCY_US_HISTOGRAM,
48    "endpoint_call_latency_us_histogram"
49);
50// Tracks latency of endpoint call_one operations in microseconds
51declare_static_histogram!(
52    ENDPOINT_CALL_ONE_LATENCY_US_HISTOGRAM,
53    "endpoint_call_one_latency_us_histogram"
54);
55// Tracks latency of endpoint choose operations in microseconds
56declare_static_histogram!(
57    ENDPOINT_CHOOSE_LATENCY_US_HISTOGRAM,
58    "endpoint_choose_latency_us_histogram"
59);
60// Tracks errors that occur during endpoint calls from the caller's perspective
61declare_static_counter!(ENDPOINT_CALL_ERROR, "endpoint_call_error");
62// Tracks errors that occur during endpoint call_one operations
63declare_static_counter!(ENDPOINT_CALL_ONE_ERROR, "endpoint_call_one_error");
64// Tracks errors that occur during endpoint choose operations
65declare_static_counter!(ENDPOINT_CHOOSE_ERROR, "endpoint_choose_error");
66// Tracks throughput of endpoint call operations
67declare_static_counter!(ENDPOINT_CALL_THROUGHPUT, "endpoint_call_throughput");
68// Tracks throughput of endpoint call_one operations
69declare_static_counter!(ENDPOINT_CALL_ONE_THROUGHPUT, "endpoint_call_one_throughput");
70// Tracks throughput of endpoint choose operations
71declare_static_counter!(ENDPOINT_CHOOSE_THROUGHPUT, "endpoint_choose_throughput");
72// Tracks latency of endpoint stream operations in microseconds
73declare_static_histogram!(
74    ENDPOINT_STREAM_LATENCY_US_HISTOGRAM,
75    "endpoint_stream_latency_us_histogram"
76);
77// Tracks errors that occur during endpoint stream operations
78declare_static_counter!(ENDPOINT_STREAM_ERROR, "endpoint_stream_error");
79// Tracks throughput of endpoint stream operations
80declare_static_counter!(ENDPOINT_STREAM_THROUGHPUT, "endpoint_stream_throughput");
81// Tracks throughput of endpoint broadcast operations
82declare_static_counter!(
83    ENDPOINT_BROADCAST_THROUGHPUT,
84    "endpoint_broadcast_throughput"
85);
86// Tracks errors that occur during endpoint broadcast operations
87declare_static_counter!(ENDPOINT_BROADCAST_ERROR, "endpoint_broadcast_error");