generated from Patagia/template-nix
WIP: Add otel tracing
This commit is contained in:
parent
00128e39ce
commit
a6115128c3
3 changed files with 500 additions and 7 deletions
controller/src
|
@ -7,9 +7,20 @@ use dropshot::HttpError;
|
|||
use dropshot::HttpResponseOk;
|
||||
use dropshot::RequestContext;
|
||||
use dropshot::ServerBuilder;
|
||||
use opentelemetry::{trace::TracerProvider as _, KeyValue};
|
||||
use opentelemetry_otlp::WithExportConfig;
|
||||
use opentelemetry_sdk::{
|
||||
trace::{RandomIdGenerator, Sampler},
|
||||
Resource,
|
||||
};
|
||||
use opentelemetry_semantic_conventions::{
|
||||
attribute::{SERVICE_NAME, SERVICE_VERSION},
|
||||
SCHEMA_URL,
|
||||
};
|
||||
use schemars::JsonSchema;
|
||||
use serde::Serialize;
|
||||
use slog::Drain;
|
||||
use tracing_opentelemetry::OpenTelemetryLayer;
|
||||
use tracing_slog::TracingSlogDrain;
|
||||
use tracing_subscriber::prelude::*;
|
||||
|
||||
|
@ -33,7 +44,10 @@ struct VersionInfo {
|
|||
method = GET,
|
||||
path = "/version",
|
||||
}]
|
||||
async fn api_version(rqctx: RequestContext<Arc<()>>) -> Result<HttpResponseOk<VersionInfo>, HttpError> {
|
||||
#[tracing::instrument()]
|
||||
async fn api_version(
|
||||
rqctx: RequestContext<Arc<()>>,
|
||||
) -> Result<HttpResponseOk<VersionInfo>, HttpError> {
|
||||
let ver = VersionInfo {
|
||||
name: String::from("patagia-controller"),
|
||||
};
|
||||
|
@ -56,19 +70,65 @@ async fn main() -> Result<(), String> {
|
|||
slog::Logger::root(async_drain, slog::o!())
|
||||
};
|
||||
|
||||
let otlp_exporter = opentelemetry_otlp::SpanExporter::builder()
|
||||
.with_tonic()
|
||||
.with_endpoint("https://localhost:4317")
|
||||
.build()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let resource = Resource::from_schema_url(
|
||||
[
|
||||
// KeyValue::new(SERVICE_NAME, env!("CARGO_PKG_NAME")),
|
||||
KeyValue::new(SERVICE_NAME, "patagia-controller"),
|
||||
KeyValue::new(SERVICE_VERSION, env!("CARGO_PKG_VERSION")),
|
||||
],
|
||||
SCHEMA_URL,
|
||||
);
|
||||
|
||||
let tracer_provider = opentelemetry_sdk::trace::TracerProvider::builder()
|
||||
.with_config(
|
||||
opentelemetry_sdk::trace::Config::default()
|
||||
.with_sampler(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(
|
||||
1.0,
|
||||
))))
|
||||
.with_id_generator(RandomIdGenerator::default())
|
||||
.with_resource(resource),
|
||||
)
|
||||
.with_batch_exporter(otlp_exporter, opentelemetry_sdk::runtime::Tokio)
|
||||
.build();
|
||||
|
||||
let tracer = tracer_provider.tracer("patagia-controller");
|
||||
|
||||
// let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(tracing_subscriber::EnvFilter::from_default_env())
|
||||
.with(fmt_layer)
|
||||
// .with(telemetry)
|
||||
.with(OpenTelemetryLayer::new(tracer))
|
||||
.init();
|
||||
|
||||
tracing::info!("Patagia Controller");
|
||||
|
||||
foo().await;
|
||||
|
||||
let mut api = ApiDescription::new();
|
||||
api.register(api_version).map_err(|e| e.to_string())?;
|
||||
|
||||
let server = ServerBuilder::new(api, Arc::new(()), dropshot_logger)
|
||||
.config(config_dropshot)
|
||||
.start()
|
||||
.map_err(|e| e.to_string())?;
|
||||
server.await
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
async fn foo() {
|
||||
tracing::info!(
|
||||
monotonic_counter.foo = 1_u64,
|
||||
key_1 = "bar",
|
||||
key_2 = 10,
|
||||
"This is the Foo!",
|
||||
);
|
||||
|
||||
tracing::info!(histogram.baz = 10, "histogram example",);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue