Skip to content

Commit

Permalink
feat: add build method to crate tracer provider and meter provider
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>
  • Loading branch information
hlts2 committed Jul 25, 2024
1 parent 38c410f commit dd54819
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion rust/libs/observability/src/observability.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
//
// Copyright (C) 2019-2024 vdaas.org vald team <vald@vdaas.org>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
use std::sync::Arc;

use anyhow::{Ok, Result};
use opentelemetry::global::shutdown_tracer_provider;
use opentelemetry::global::{self, shutdown_tracer_provider};
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::metrics::SdkMeterProvider;
use opentelemetry_sdk::propagation::TraceContextPropagator;
use opentelemetry_sdk::trace::TracerProvider;
use opentelemetry_sdk::Resource;

use crate::config::Config;

Expand Down Expand Up @@ -34,6 +52,43 @@ impl ObservabilityImpl {

impl Observability for ObservabilityImpl {
fn build(&mut self) -> Result<()> {
if !self.config.enabled {
return Ok(());
}

if self.config.meter.enabled {
let provider = opentelemetry_otlp::new_pipeline()
.metrics(opentelemetry_sdk::runtime::Tokio)
.with_period(self.config.meter.export_duration)
.with_resource(Resource::from(self.config()))
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_endpoint(self.config.meter.endpoint.as_str()),
)
.build()?;
self.meter_provider = Some(provider.clone());
global::set_meter_provider(provider.clone());
}

if self.config.tracer.enabled {
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_endpoint(self.config.tracer.endpoint.as_str()),
)
.with_trace_config(
opentelemetry_sdk::trace::config()
.with_sampler(opentelemetry_sdk::trace::Sampler::AlwaysOn)
.with_resource(Resource::from(self.config()))
.with_id_generator(opentelemetry_sdk::trace::RandomIdGenerator::default()),
)
.install_batch(opentelemetry_sdk::runtime::Tokio)?;
global::set_text_map_propagator(TraceContextPropagator::new());
global::set_tracer_provider(tracer.provider().unwrap());
}
Ok(())
}

Expand Down

0 comments on commit dd54819

Please sign in to comment.