21 releases (breaking)

new 0.16.0 Nov 15, 2024
0.14.0 Sep 7, 2024
0.12.0 Jul 21, 2024
0.9.0 Mar 5, 2024
0.2.0 Jul 31, 2022

#2033 in Web programming

Download history 100/week @ 2024-07-30 179/week @ 2024-08-06 212/week @ 2024-08-13 366/week @ 2024-08-20 241/week @ 2024-08-27 368/week @ 2024-09-03 533/week @ 2024-09-10 311/week @ 2024-09-17 513/week @ 2024-09-24 389/week @ 2024-10-01 262/week @ 2024-10-08 230/week @ 2024-10-15 300/week @ 2024-10-22 347/week @ 2024-10-29 266/week @ 2024-11-05 328/week @ 2024-11-12

1,284 downloads per month

Apache-2.0

27KB
399 lines

Cargo tests and formatting security audit

OpenTelemetry support for Google Cloud Trace

Quick start

Cargo.toml:

[dependencies]
opentelemetry-gcloud-trace = "0.16"

Compatibility matrix

opentelemetry-gcloud-trace version opentelemetry version tracing-opentelemetry gcloud-sdk
0.16 0.27 0.28 0.25
0.15 0.25 0.26 0.25
0.12 0.24 0.25 0.25
0.11 0.23 0.24 0.25
0.10 0.23 0.24 0.24

Example:


use opentelemetry::trace::*;
use opentelemetry_gcloud_trace::*;

let tracer = GcpCloudTraceExporterBuilder::for_default_project_id().await? // or GcpCloudTraceExporterBuilder::new(config_env_var("PROJECT_ID")?)
    .install()
    .await?;

tracer.in_span("doing_work_parent", |cx| {
  // ...
});

All examples are available at examples directory.

To run an example use with environment variables:

# PROJECT_ID=<your-google-project-id> cargo run --example enable-exporter

Google Cloud Console Example

[dependencies]
opentelemetry = { version = "*", features = [] }
opentelemetry_sdk = { version = "*", features = ["rt-tokio"] }
opentelemetry-gcloud-trace = "*"

Configuration

You can specify trace configuration using with_trace_config:

   GcpCloudTraceExporterBuilder::new(google_project_id).with_trace_config(
      trace::config()
         .with_sampler(Sampler::AlwaysOn)
         .with_id_generator(RandomIdGenerator::default())
   )

Limitations

  • This exporter doesn't support any other runtimes except Tokio.

Integration with logs

This crate intentionally doesn't export logs using API to avoid duplicate logs on GKE/GCE environments. You can use this crate in combination with tracing-stackdriver crate to produce JSON formatted logs and logs and traces will be correlated by trace_id and span_id fields automatically.

This is an example of this kind of configuration:


fn init_console_log(
    tracer: opentelemetry_sdk::trace::Tracer,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);

    let subscriber = tracing_subscriber::registry::Registry::default()
        .with(tracing_subscriber::fmt::layer())
        .with(tracing_subscriber::EnvFilter::from_str(
            "gcloud_sdk=debug",
        )?)
        .with(telemetry);

    tracing::subscriber::set_global_default(subscriber)?;

    Ok(())
}

fn init_stackdriver_log(
    gcp_project_id: &str,
    tracer: opentelemetry_sdk::trace::Tracer,
) -> Result<(), BoxedError> {
    let telemetry_layer = tracing_opentelemetry::layer().with_tracer(tracer);

    let stackdriver_layer = tracing_stackdriver::layer().with_cloud_trace(
        tracing_stackdriver::CloudTraceConfiguration {
            project_id: gcp_project_id.to_string(),
        },
    );

    let subscriber = tracing_subscriber::Registry::default()
        .with(telemetry_layer)
        .with(stackdriver_layer)
        .with(tracing_subscriber::EnvFilter::from_str(
            "gcloud_sdk=debug",
        )?);
    tracing::subscriber::set_global_default(subscriber)
        .expect("Could not set up global logger");

    Ok(())
}

async fn init_tracing(app_mode: &GlobalAppMode, 
                      gcp_project_id: &str) -> Result<(), BoxedError> {
    let tracer = GcpCloudTraceExporterBuilder::new(gcp_project_id.into())
        .install()
        .await?;

    match app_mode {
        GlobalAppMode::Production => init_stackdriver_log(gcp_project_id, tracer),
        GlobalAppMode::Development => init_console_log(tracer),
    }
}

Cargo provides support for different TLS features for dependencies:

  • tls-roots: default feature to support native TLS roots
  • tls-webpki-roots: feature to switch to webpki crate roots

Licence

Apache Software License (ASL)

Author

Abdulla Abdurakhmanov

Dependencies

~109MB
~1.5M SLoC