Skip to content

Commit

Permalink
Update to opentelemetry 0.20 (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
divergentdave authored Oct 6, 2023
1 parent 1fe3394 commit d854dd8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 50 deletions.
27 changes: 15 additions & 12 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ futures-lite = "1.13.0"
git-version = "0.3.5"
janus_messages = "0.5.22"
log = "0.4.20"
opentelemetry = { version = "0.19.0", features = ["metrics"] }
opentelemetry-prometheus = { version = "0.12.0", features = [
opentelemetry = { version = "0.20.0", features = ["metrics"] }
opentelemetry-prometheus = { version = "0.13.0", features = [
"prometheus-encoding",
] }
prio = "0.15.3"
prometheus = "0.13.3"
querystrong = "0.3.0"
rand = "0.8.5"
serde = { version = "1.0.188", features = ["derive"] }
Expand Down
63 changes: 27 additions & 36 deletions src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,45 @@ use git_version::git_version;
use opentelemetry::{
global,
metrics::MetricsError,
sdk::{
export::metrics::aggregation::stateless_temporality_selector,
metrics::{controllers, processors, selectors::simple::histogram},
},
Context, KeyValue,
sdk::{metrics::MeterProvider, Resource},
KeyValue,
};
use std::sync::Arc;
use prometheus::Registry;

/// Install a Prometheus metrics provider and exporter. The
/// OpenTelemetry global API can be used to create and update
/// instruments, and they will be sent through this exporter.
pub fn metrics_exporter() -> Result<impl trillium::Handler, MetricsError> {
let exporter = Arc::new(
opentelemetry_prometheus::exporter(
controllers::basic(processors::factory(
histogram([
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
]),
stateless_temporality_selector(),
))
.build(),
)
.try_init()?,
);
let registry = Registry::new();
let exporter = opentelemetry_prometheus::exporter()
.with_registry(registry.clone())
.build()?;

// Note that the implementation of `Default` pulls in attributes set via environment variables.
let default_resource = Resource::default();

// Record the binary's version information in a build info metric.
let meter = global::meter("divviup-api");
let gauge = meter
.u64_observable_gauge("divviup_api_build_info")
.with_description("Build-time version information")
.init();
let mut git_revision: &str = git_version!(fallback = "unknown");
if git_revision == "unknown" {
if let Some(value) = option_env!("GIT_REVISION") {
git_revision = value;
}
}
gauge.observe(
&Context::current(),
1,
&[
KeyValue::new("version", env!("CARGO_PKG_VERSION")),
KeyValue::new("revision", git_revision),
KeyValue::new("rust_version", env!("RUSTC_SEMVER")),
],
);
let version_info_resource = Resource::new([
KeyValue::new(
"service.version",
format!("{}-{}", env!("CARGO_PKG_VERSION"), git_revision),
),
KeyValue::new("process.runtime.name", "Rust"),
KeyValue::new("process.runtime.version", env!("RUSTC_SEMVER")),
]);

let resource = version_info_resource.merge(&default_resource);

let meter_provider = MeterProvider::builder()
.with_reader(exporter)
.with_resource(resource)
.build();
global::set_meter_provider(meter_provider);

Ok(trillium_prometheus::text_format_handler(
exporter.registry().clone(),
))
Ok(trillium_prometheus::text_format_handler(registry))
}

0 comments on commit d854dd8

Please sign in to comment.