Skip to content

Commit

Permalink
change rtp/rtcp_packet_processing_time to Histogram metric type
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Feb 24, 2024
1 parent 69fb40c commit 6cbe5bc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
5 changes: 4 additions & 1 deletion examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::net::SocketAddr;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

use async_broadcast::{broadcast, Receiver};
use clap::Parser;
Expand Down Expand Up @@ -93,7 +94,9 @@ fn init_meter_provider(mut stop_rx: Receiver<()>, worker: Worker) -> MeterProvid
Ok(serde_json::to_writer_pretty(writer, &data).unwrap())
})
.build();
let reader = PeriodicReader::builder(exporter, runtime::TokioCurrentThread).build();
let reader = PeriodicReader::builder(exporter, runtime::TokioCurrentThread)
.with_interval(Duration::from_secs(30))
.build();
let meter_provider = MeterProvider::builder()
.with_reader(reader)
.with_resource(Resource::new(vec![KeyValue::new("chat", "metrics")]))
Expand Down
23 changes: 12 additions & 11 deletions src/handler/srtp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ impl OutboundHandler for SrtpOutbound {
let rtcp_packet = context.encrypt_rtcp(&packet);

server_states.metrics().record_rtcp_packet_out_count(1, &[]);
rtcp_packet
} else {
let metrics = server_states.metrics();
metrics.record_local_srtp_context_not_set_count(1, &[]);
metrics.record_rtcp_packet_processing_time(
server_states.metrics().record_rtcp_packet_processing_time(
Instant::now().duration_since(msg.now).as_micros() as u64,
&[],
);
rtcp_packet
} else {
server_states
.metrics()
.record_local_srtp_context_not_set_count(1, &[]);

Err(Error::Other(format!(
"local_srtp_context is not set yet for four_tuple {:?}",
Expand All @@ -149,15 +150,15 @@ impl OutboundHandler for SrtpOutbound {
let rtp_packet = context.encrypt_rtp(&packet);

server_states.metrics().record_rtp_packet_out_count(1, &[]);
rtp_packet
} else {
let metrics = server_states.metrics();
metrics.record_local_srtp_context_not_set_count(1, &[]);
metrics.record_rtp_packet_processing_time(
server_states.metrics().record_rtp_packet_processing_time(
Instant::now().duration_since(msg.now).as_micros() as u64,
&[],
);

rtp_packet
} else {
server_states
.metrics()
.record_local_srtp_context_not_set_count(1, &[]);
Err(Error::Other(format!(
"local_srtp_context is not set yet for four_tuple {:?}",
four_tuple
Expand Down
32 changes: 12 additions & 20 deletions src/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use opentelemetry::metrics::{Counter, Meter, ObservableGauge, Unit};
use opentelemetry::KeyValue;
use opentelemetry::{
metrics::{Counter, Histogram, Meter, Unit},
KeyValue,
};

pub(crate) struct Metrics {
rtp_packet_in_count: Counter<u64>,
Expand All @@ -8,8 +10,8 @@ pub(crate) struct Metrics {
rtcp_packet_out_count: Counter<u64>,
remote_srtp_context_not_set_count: Counter<u64>,
local_srtp_context_not_set_count: Counter<u64>,
rtp_packet_processing_time: ObservableGauge<u64>,
rtcp_packet_processing_time: ObservableGauge<u64>,
rtp_packet_processing_time: Histogram<u64>,
rtcp_packet_processing_time: Histogram<u64>,
}

impl Metrics {
Expand All @@ -26,11 +28,11 @@ impl Metrics {
.u64_counter("local_srtp_context_not_set_count")
.init(),
rtp_packet_processing_time: meter
.u64_observable_gauge("rtp_packet_processing_time")
.u64_histogram("rtp_packet_processing_time")
.with_unit(Unit::new("us"))
.init(),
rtcp_packet_processing_time: meter
.u64_observable_gauge("rtcp_packet_processing_time")
.u64_histogram("rtcp_packet_processing_time")
.with_unit(Unit::new("us"))
.init(),
}
Expand Down Expand Up @@ -69,21 +71,11 @@ impl Metrics {
self.local_srtp_context_not_set_count.add(value, attributes);
}

pub(crate) fn record_rtp_packet_processing_time(
&self,
measurement: u64,
attributes: &[KeyValue],
) {
self.rtp_packet_processing_time
.observe(measurement, attributes);
pub(crate) fn record_rtp_packet_processing_time(&self, value: u64, attributes: &[KeyValue]) {
self.rtp_packet_processing_time.record(value, attributes);
}

pub(crate) fn record_rtcp_packet_processing_time(
&self,
measurement: u64,
attributes: &[KeyValue],
) {
self.rtcp_packet_processing_time
.observe(measurement, attributes);
pub(crate) fn record_rtcp_packet_processing_time(&self, value: u64, attributes: &[KeyValue]) {
self.rtcp_packet_processing_time.record(value, attributes);
}
}

0 comments on commit 6cbe5bc

Please sign in to comment.