Crates.io | opentelemetry-langfuse |
lib.rs | opentelemetry-langfuse |
version | 0.3.1 |
created_at | 2025-08-31 09:28:56.631329+00 |
updated_at | 2025-08-31 19:37:05.968917+00 |
description | OpenTelemetry components and utilities for Langfuse LLM observability |
homepage | https://github.com/genai-rs/opentelemetry-langfuse |
repository | https://github.com/genai-rs/opentelemetry-langfuse |
max_upload_size | |
id | 1818346 |
size | 174,905 |
OpenTelemetry integration for Langfuse, the open-source LLM observability platform.
This crate provides OpenTelemetry components and utilities for integrating with Langfuse, enabling comprehensive observability for LLM applications. For more information about OpenTelemetry support in Langfuse, see the official Langfuse OpenTelemetry documentation.
[dependencies]
opentelemetry-langfuse = "*"
use opentelemetry::global;
use opentelemetry_langfuse::exporter_from_env;
use opentelemetry_sdk::trace::SdkTracerProvider;
use opentelemetry_sdk::Resource;
use opentelemetry::KeyValue;
// Create the Langfuse exporter
let exporter = exporter_from_env()?;
// Build your tracer provider with the exporter
let provider = SdkTracerProvider::builder()
.with_batch_exporter(exporter)
.with_resource(Resource::builder().with_attributes(vec![
KeyValue::new("service.name", "my-service"),
]).build())
.build();
// Set as global provider and start tracing
global::set_tracer_provider(provider);
The exporter can be configured using environment variables. You have three options:
Use exporter_from_langfuse_env()
with these variables:
LANGFUSE_PUBLIC_KEY=pk-lf-... # Your public key
LANGFUSE_SECRET_KEY=sk-lf-... # Your secret key
LANGFUSE_HOST=https://cloud.langfuse.com # Optional: Defaults to cloud instance
Use exporter_from_otel_env()
following the OTLP Exporter specification:
# For endpoint (use ONE of these):
# Option A: Direct traces endpoint (recommended)
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://cloud.langfuse.com/api/public/otel/v1/traces
# Option B: Base endpoint that works with Langfuse
OTEL_EXPORTER_OTLP_ENDPOINT=https://cloud.langfuse.com/api/public/otel # /v1/traces will be appended
# This creates: https://cloud.langfuse.com/api/public/otel/v1/traces (which Langfuse accepts)
# For authentication
OTEL_EXPORTER_OTLP_HEADERS="Authorization=Basic <base64_encoded_credentials>"
⚠️ Important: Do NOT use OTEL_EXPORTER_OTLP_ENDPOINT=https://cloud.langfuse.com/api/public
as this would create /api/public/v1/traces
which Langfuse does not accept.
Use exporter_from_env()
for automatic fallback with sensible defaults. Priority order:
For endpoint:
LANGFUSE_HOST
(appends /api/public/otel/v1/traces
)OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
OTEL_EXPORTER_OTLP_ENDPOINT
(appends /v1/traces
)https://cloud.langfuse.com/api/public/otel/v1/traces
(when no endpoint variables are set)For authentication:
LANGFUSE_PUBLIC_KEY
+ LANGFUSE_SECRET_KEY
OTEL_EXPORTER_OTLP_TRACES_HEADERS
OTEL_EXPORTER_OTLP_HEADERS
You can also configure the exporter programmatically:
use opentelemetry_langfuse::ExporterBuilder;
use std::time::Duration;
let exporter = ExporterBuilder::new()
.with_host("https://cloud.langfuse.com")
.with_basic_auth("pk-lf-...", "sk-lf-...")
.with_timeout(Duration::from_secs(10))
.build()?;
By default, the exporter creates a new reqwest::Client
with rustls TLS support, which is suitable for most use cases and works with HTTPS endpoints out of the box.
You can provide your own client for advanced configurations:
use opentelemetry_langfuse::ExporterBuilder;
use std::time::Duration;
let custom_client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.proxy(reqwest::Proxy::http("http://proxy.example.com:8080")?)
.build()?;
let exporter = ExporterBuilder::new()
.with_host("https://cloud.langfuse.com")
.with_basic_auth("pk-lf-...", "sk-lf-...")
.with_http_client(custom_client)
.build()?;
Note on TLS: The crate includes rustls-tls
by default for HTTPS support. If you're building a custom client or have specific TLS requirements, ensure your reqwest
client is configured with appropriate TLS features.
See the examples directory for complete working examples:
basic.rs
- Simple usage with environment variablesmanual_config.rs
- Manual configuration without env varsotel_env.rs
- Using standard OpenTelemetry environment variablescustom_http_client.rs
- Using a custom HTTP client for proxy or advanced configurationsLicensed under either of:
See CONTRIBUTING.md for guidelines.