Cloud Trace API exporter
The trace-api feature adds trace_api::GcpCloudTraceExporterBuilder, an exporter for the Cloud Trace API v2 (cloudtrace.googleapis.com).
It authenticates with ADC, see Authentication, and needs the roles/cloudtrace.agent role.
let exporter = GcpCloudTraceExporterBuilder::new(google_project_id)
.with_resource(
opentelemetry_sdk::Resource::builder()
.with_service_name("traces-cloud-trace-api-example")
.build(),
)
.with_span_events(true);
let tracer_provider = exporter.create_provider().await?;
let tracer = exporter.install(&tracer_provider);
create_provider and create_provider_from_builder build a tracer provider with a batch span processor, and install(&provider) returns a tracer.
install is synchronous.
with_resource sets the resource the exporter reports spans with.
Spans come from the OpenTelemetry API directly:
tracer.in_span("my_parent_work", |cx| {
let span = cx.span();
span.set_attribute(KeyValue::new("http.client_ip", "42.42.42.42"));
span.add_event(
"test-event",
vec![KeyValue::new("test_event_attr", "test-event-value")],
);
tracer.in_span("my_child_work", |cx| {
cx.span().add_event(
"test-child-event",
vec![KeyValue::new("test_event_attr", "test-event-value")],
);
})
});
or from tracing:
let subscriber = Registry::default().with(tracing_opentelemetry::layer().with_tracer(tracer));
tracing::subscriber::with_default(subscriber, || {
let root = span!(Level::INFO, "my_app", work_units = 2);
let _enter = root.enter();
span!(
Level::INFO,
"my_child",
work_units = 2,
"http.client_ip" = "42.42.42.42"
)
.in_scope(|| info!("Doing the child work."));
error!("This event is added to the root span.");
});
Export timeout
with_max_export_timeout bounds how long a batch export may take before the batch is dropped. It defaults to the OpenTelemetry SDK default of 30 seconds, or OTEL_BSP_EXPORT_TIMEOUT when that is set.
Shutdown exports every batch queued ahead of it, one after the other, so it can take the timeout once per 512 queued spans, rounded up.
Keep that total below the time the platform allows after SIGTERM.
Shutdown blocks the calling thread until the batch task is done, so it needs a multi-threaded Tokio runtime, as for the OTLP exporter.
Full example available here.