Skip to content

Commit

Permalink
major update (breaking change) open telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
GlenDC committed Nov 12, 2024
1 parent af4273d commit c1a9af8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 57 deletions.
21 changes: 11 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ percent-encoding = "2.1"
pin-project-lite = "0.2.13"
rustls-pki-types = "^1"
proc-macro2 = "1.0"
opentelemetry = { version = "0.26.0", default-features = false, features = [
opentelemetry = { version = "0.27.0", default-features = false, features = [
"trace",
] }
nom = "7.1.3"
opentelemetry-otlp = { version = "0.26.0", features = ["tokio"] }
opentelemetry_sdk = { version = "0.26.0", default-features = false, features = [
opentelemetry-otlp = { version = "0.27.0", features = ["tokio"] }
opentelemetry_sdk = { version = "0.27.0", default-features = false, features = [
"trace",
"rt-tokio",
] }
opentelemetry-semantic-conventions = { version = "0.26", features = [
opentelemetry-semantic-conventions = { version = "0.27", features = [
"semconv_experimental",
] }
quickcheck = "1.0"
Expand Down
56 changes: 31 additions & 25 deletions examples/http_telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
//! You can now use tools like grafana to collect metrics from the collector running at 127.0.0.1:4317 over GRPC.

use opentelemetry_otlp::{ExportConfig, Protocol, WithExportConfig};
use opentelemetry_sdk::{metrics::reader::DefaultTemporalitySelector, Resource};
use rama::{
http::{
layer::{opentelemetry::RequestMetricsLayer, trace::TraceLayer},
Expand All @@ -51,11 +50,15 @@ use rama::{
telemetry::opentelemetry::{
self,
metrics::UpDownCounter,
sdk::{
metrics::{PeriodicReader, SdkMeterProvider},
runtime, Resource,
},
semantic_conventions::{
self,
resource::{HOST_ARCH, OS_NAME},
},
KeyValue,
InstrumentationScope, KeyValue,
},
Context, Layer,
};
Expand All @@ -70,16 +73,18 @@ struct Metrics {

impl Metrics {
fn new() -> Self {
let meter = opentelemetry::global::meter_with_version(
"example.http_telemetry",
Some(env!("CARGO_PKG_VERSION")),
Some(semantic_conventions::SCHEMA_URL),
Some(vec![
KeyValue::new(OS_NAME, std::env::consts::OS),
KeyValue::new(HOST_ARCH, std::env::consts::ARCH),
]),
let meter = opentelemetry::global::meter_with_scope(
InstrumentationScope::builder("example.http_telemetry")
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url(semantic_conventions::SCHEMA_URL)
.with_attributes(vec![
KeyValue::new(OS_NAME, std::env::consts::OS),
KeyValue::new(HOST_ARCH, std::env::consts::ARCH),
])
.build(),
);
let counter = meter.i64_up_down_counter("visitor_counter").init();

let counter = meter.i64_up_down_counter("visitor_counter").build();
Self { counter }
}
}
Expand All @@ -98,28 +103,29 @@ async fn main() {

// configure OT metrics exporter
let export_config = ExportConfig {
endpoint: "http://localhost:4317".to_owned(),
endpoint: Some("http://localhost:4317".to_owned()),
timeout: Duration::from_secs(3),
protocol: Protocol::Grpc,
};

let meter = opentelemetry_otlp::new_pipeline()
.metrics(opentelemetry_sdk::runtime::Tokio)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_export_config(export_config),
// can also config it using with_* functions like the tracing part above.
)
let meter_exporter = opentelemetry_otlp::MetricExporter::builder()
.with_tonic()
.with_export_config(export_config)
.build()
.expect("build OT exporter");

let meter_reader = PeriodicReader::builder(meter_exporter, runtime::Tokio)
.with_interval(Duration::from_secs(3))
.with_timeout(Duration::from_secs(10))
.build();

let meter = SdkMeterProvider::builder()
.with_resource(Resource::new(vec![KeyValue::new(
"service.name",
"http_telemetry",
)]))
.with_period(Duration::from_secs(3))
.with_timeout(Duration::from_secs(10))
.with_temporality_selector(DefaultTemporalitySelector::new())
.build()
.expect("build OT meter");
.with_reader(meter_reader)
.build();

opentelemetry::global::set_meter_provider(meter);

Expand Down
23 changes: 13 additions & 10 deletions rama-http/src/layer/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rama_core::telemetry::opentelemetry::{
self,
resource::{SERVICE_NAME, SERVICE_VERSION},
},
AttributesFactory, KeyValue, MeterOptions, ServiceInfo,
AttributesFactory, InstrumentationScope, KeyValue, MeterOptions, ServiceInfo,
};
use rama_core::{Context, Layer, Service};
use rama_net::http::RequestContext;
Expand Down Expand Up @@ -58,23 +58,23 @@ impl Metrics {
})
.with_description("Measures the duration of inbound HTTP requests.")
.with_unit("s")
.init();
.build();

let http_server_total_requests = meter
.u64_counter(match &prefix {
Some(prefix) => Cow::Owned(format!("{prefix}.{HTTP_SERVER_TOTAL_REQUESTS}")),
None => Cow::Borrowed(HTTP_SERVER_TOTAL_REQUESTS),
})
.with_description("Measures the total number of HTTP requests have been seen.")
.init();
.build();

let http_server_total_responses = meter
.u64_counter(match &prefix {
Some(prefix) => Cow::Owned(format!("{prefix}.{HTTP_SERVER_TOTAL_RESPONSES}")),
None => Cow::Borrowed(HTTP_SERVER_TOTAL_RESPONSES),
})
.with_description("Measures the total number of HTTP responses have been seen.")
.init();
.build();

let http_server_total_failures = meter
.u64_counter(match &prefix {
Expand All @@ -84,7 +84,7 @@ impl Metrics {
.with_description(
"Measures the total number of failed HTTP requests that have been seen.",
)
.init();
.build();

Metrics {
http_server_total_requests,
Expand Down Expand Up @@ -169,11 +169,14 @@ impl Default for RequestMetricsLayer {
}

fn get_versioned_meter() -> Meter {
global::meter_with_version(
const_format::formatcp!("{}-network-http", rama_utils::info::NAME),
Some(rama_utils::info::VERSION),
Some(semantic_conventions::SCHEMA_URL),
None,
global::meter_with_scope(
InstrumentationScope::builder(const_format::formatcp!(
"{}-network-http",
rama_utils::info::NAME
))
.with_version(rama_utils::info::VERSION)
.with_schema_url(semantic_conventions::SCHEMA_URL)
.build(),
)
}

Expand Down
19 changes: 11 additions & 8 deletions rama-net/src/stream/layer/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rama_core::telemetry::opentelemetry::semantic_conventions::trace::{
use rama_core::telemetry::opentelemetry::{
global,
metrics::{Counter, Histogram, Meter},
semantic_conventions, KeyValue,
semantic_conventions, InstrumentationScope, KeyValue,
};
use rama_core::telemetry::opentelemetry::{AttributesFactory, MeterOptions, ServiceInfo};
use rama_core::{Context, Layer, Service};
Expand Down Expand Up @@ -41,7 +41,7 @@ impl Metrics {
})
.with_description("Measures the duration of inbound network connections.")
.with_unit("s")
.init();
.build();

let network_total_connections = meter
.u64_counter(match &prefix {
Expand All @@ -51,7 +51,7 @@ impl Metrics {
.with_description(
"measures the number of total network connections that have been established so far",
)
.init();
.build();

Metrics {
network_connection_duration,
Expand Down Expand Up @@ -134,11 +134,14 @@ impl Default for NetworkMetricsLayer {
}

fn get_versioned_meter() -> Meter {
global::meter_with_version(
const_format::formatcp!("{}-network-transport", rama_utils::info::NAME),
Some(rama_utils::info::VERSION),
Some(semantic_conventions::SCHEMA_URL),
None,
global::meter_with_scope(
InstrumentationScope::builder(const_format::formatcp!(
"{}-network-transport",
rama_utils::info::NAME
))
.with_version(rama_utils::info::VERSION)
.with_schema_url(semantic_conventions::SCHEMA_URL)
.build(),
)
}

Expand Down

0 comments on commit c1a9af8

Please sign in to comment.