#open-telemetry #api-bindings #grpc-api #protobuf #api #grpc #tremor

tremor-otelapis

OpenTelemetry v1 API binding based on tonic/prost

10 releases

0.5.0 Sep 26, 2024
0.3.0 Dec 19, 2022
0.2.5 Jul 5, 2022
0.2.4 Apr 21, 2022
0.1.1 Mar 12, 2021

#271 in Network programming

Download history 11/week @ 2024-08-16 22/week @ 2024-08-23 62/week @ 2024-08-30 20/week @ 2024-09-06 12/week @ 2024-09-13 162/week @ 2024-09-20 267/week @ 2024-09-27 65/week @ 2024-10-04 108/week @ 2024-10-11 233/week @ 2024-10-18 94/week @ 2024-10-25 18/week @ 2024-11-01 14/week @ 2024-11-08 30/week @ 2024-11-15 70/week @ 2024-11-22 28/week @ 2024-11-29

143 downloads per month

Apache-2.0

51KB
552 lines

CNCF Early Stage Sandbox Project

CNCF Streaming & Messaging


Quality Checks License Checks Security Checks CII Best Practices GitHub

OpenTelemetry support for Tremor

Status

This library exists to support OpenTelemetry based connectivity within tremor.

Tremor can expose client and server OpenTelemetry endpoints based on the gRPC protocol specification for OpenTelemetry.

About

A tonic-build based data-binding and utility code for OpenTelemetry v1 primarily for use in the tremor project for OpenTelemetry v1 interoperability, integration and interworking.


The code in the gen folder was seeded by tonic-build.

The code in the src folder extends the generated source with utility code to allow for convenient usage and definition of tonic-based gRPC servers. Specifically, this library is designed for use by the Tremor Project but has no dependencies on tremor and can be used standalone.

This library does not provide an API or SDK designed for use as a tracing facility. The official OpenTelemetry Rust project is a complete OpenTelemetry SDK designed for that purpose. It uses the same underlying protocol buffer definitions and will be a better target for projects that require OpenTelemetry based observability instrumentation and iteroperability with the wider observability ecosystem through third party crates.

This library is designed for system integration and interoperability and is not recommended for use as a tracing SDK or for instrumentation as that is well covered already by the OpenTelemetry Rust crate. For instrumentation, use the official crate.

For those projects that need basic interworking, interoperability or integration with OpenTelemetry based systems at a wire level, this project may be useful.

Minimal dependencies for Cargo.toml

[dependencies]
tremor-otelapis = { version = "0.1", features = ["otel-all"] }
tonic = { version = "0.4", features = ["tls"] }
prost = "0.7"
prost-types = "0.7"
tokio = { version = "1.1", features = ["rt-multi-thread", "time", "fs", "macros"] }

Example OpenTelemetry Log client.

Note that clients simply use the generated client stub code from tonic-build. This library adds no extra utility or convenience.

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let channel = Endpoint::from_static("http://0.0.0.0:4316")
        .connect()
        .await?;

    let mut client = LogsServiceClient::new(channel);

    let resource_logs = ResourceLogs {
        ...
    };

    client
        .export(ExportLogsServiceRequest {
            resource_logs: vec![resource_logs],
        })
        .await?;

    Ok(())
}

Example OpenTelemetry Log Server.

Note that we use utility code to expose the server side functionality. We pass through the generated Protocol Buffer message data binding generated code unadorned. The data bindings for protocol buffer messages are generated by tonic-build.

The tonic-build in turns builds on prost-build.

fn on_logs(
    request: tonic::Request<ExportLogsServiceRequest>,
) -> Result<tonic::Response<ExportLogsServiceResponse>, tonic::Status> {
    println!("Got a request from {:?}", request.remote_addr());
    let reply = ExportLogsServiceResponse::default();
    Ok(tonic::Response::new(reply))
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let addr = "0.0.0.0:4317".parse()?;
    let svc = otelapis::logs::make_service(Box::new(on_logs));
    Server::builder().add_service(svc).serve(addr).await?;

    Ok(())
}

Example async-channel based OpenTelemetry server

For ease of integration with async runtimes such as tremor:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let addr = "0.0.0.0:4317".parse()?;
    let (tx, rx) = bounded(128);
    tremor_otelapis::all::make(addr, tx).await?;

    // ...

    loop {
        match rx.try_recv() {
            Ok(OpenTelemetryEvents::Metrics(metrics)) => {
                // Do something with metrics request
            }
            Ok(OpenTelemetryEvents::Logs(log)) => {
                // Do something with log request
            }
            Ok(OpenTelemetryEvents::Trace(trace)) => {
                // Do something with trace request
            }
            _ => error!("Unsupported"),
        };
   }

   // ...
}

Dependencies

~5–15MB
~204K SLoC