api/generic_server.rs: Ported to opentelemetry 0.18.

This commit is contained in:
Jonathan Davies 2023-01-27 16:42:06 +00:00
parent 045b93bd11
commit 3131052678

View file

@ -13,7 +13,7 @@ use hyper::{HeaderMap, StatusCode};
use opentelemetry::{ use opentelemetry::{
global, global,
metrics::{Counter, ValueRecorder}, metrics::{Counter, Histogram},
trace::{FutureExt, SpanRef, TraceContextExt, Tracer}, trace::{FutureExt, SpanRef, TraceContextExt, Tracer},
Context, KeyValue, Context, KeyValue,
}; };
@ -55,7 +55,7 @@ pub(crate) struct ApiServer<A: ApiHandler> {
// Metrics // Metrics
request_counter: Counter<u64>, request_counter: Counter<u64>,
error_counter: Counter<u64>, error_counter: Counter<u64>,
request_duration: ValueRecorder<f64>, request_duration: Histogram<f64>,
} }
impl<A: ApiHandler> ApiServer<A> { impl<A: ApiHandler> ApiServer<A> {
@ -79,7 +79,7 @@ impl<A: ApiHandler> ApiServer<A> {
)) ))
.init(), .init(),
request_duration: meter request_duration: meter
.f64_value_recorder(format!("api.{}.request_duration", A::API_NAME)) .f64_histogram(format!("api.{}.request_duration", A::API_NAME))
.with_description(format!( .with_description(format!(
"Duration of API calls to the various {} API endpoints", "Duration of API calls to the various {} API endpoints",
A::API_NAME_DISPLAY A::API_NAME_DISPLAY
@ -190,7 +190,8 @@ impl<A: ApiHandler> ApiServer<A> {
.record_duration(&self.request_duration, &metrics_tags[..]) .record_duration(&self.request_duration, &metrics_tags[..])
.await; .await;
self.request_counter.add(1, &metrics_tags[..]); self.request_counter
.add(&current_context, 1, &metrics_tags[..]);
let status_code = match &res { let status_code = match &res {
Ok(r) => r.status(), Ok(r) => r.status(),
@ -198,6 +199,7 @@ impl<A: ApiHandler> ApiServer<A> {
}; };
if status_code.is_client_error() || status_code.is_server_error() { if status_code.is_client_error() || status_code.is_server_error() {
self.error_counter.add( self.error_counter.add(
&current_context,
1, 1,
&[ &[
metrics_tags[0].clone(), metrics_tags[0].clone(),