Use Alternative Clients
Several languages have a dedicated page. Send data with the standard OpenTelemetry SDK from Go, .NET, or Java, or use a first-party SDK for Python, Rust, or TypeScript. If your language is not listed, this page shows the generic pattern that works anywhere.
Logfire uses the OpenTelemetry standard. This means that you can configure standard OpenTelemetry SDKs in many languages to export to the Logfire backend, including those outside our first-class supported languages. Depending on your SDK, you may need to set only these environment variables:
OTEL_EXPORTER_OTLP_ENDPOINT=https://logfire-us.pydantic.devfor traces, metrics, and logs, or set one signal at a time:OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://logfire-us.pydantic.dev/v1/tracesfor just tracesOTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://logfire-us.pydantic.dev/v1/metricsfor just metricsOTEL_EXPORTER_OTLP_LOGS_ENDPOINT=https://logfire-us.pydantic.dev/v1/logsfor just logs
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobufso the SDK exports over HTTP (see the warning below).OTEL_EXPORTER_OTLP_HEADERS='Authorization=your-write-token'- see Create Write Tokens to obtain a write token and replaceyour-write-tokenwith it.OTEL_SERVICE_NAME=your-service-nameto set the name your service is grouped under in Logfire. Without it, your data appears under(unknown)in the Services view.
First, run these commands:
pip install opentelemetry-exporter-otlp
export OTEL_EXPORTER_OTLP_ENDPOINT=https://logfire-us.pydantic.dev
export OTEL_EXPORTER_OTLP_HEADERS='Authorization=your-write-token'
export OTEL_SERVICE_NAME=my-service
Then run this script with python:
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
exporter = OTLPSpanExporter()
span_processor = BatchSpanProcessor(exporter)
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(span_processor)
tracer = tracer_provider.get_tracer('my_tracer')
tracer.start_span('Hello World').end()
Then navigate to the Live view for your project in your browser. You should see a trace with a single span named Hello World.
To configure the exporter without environment variables:
exporter = OTLPSpanExporter(
endpoint='https://logfire-us.pydantic.dev/v1/traces',
headers={'Authorization': 'your-write-token'},
)
See also our JS/TS SDK which supports many JS environments, including NodeJS, web browsers, and Cloudflare Workers.
Create a main.js file containing the following:
import {NodeSDK} from "@opentelemetry/sdk-node";
import {OTLPTraceExporter} from "@opentelemetry/exporter-trace-otlp-proto";
import {BatchSpanProcessor} from "@opentelemetry/sdk-trace-node";
import {trace} from "@opentelemetry/api";
import {Resource} from "@opentelemetry/resources";
import {ATTR_SERVICE_NAME} from "@opentelemetry/semantic-conventions";
const traceExporter = new OTLPTraceExporter();
const spanProcessor = new BatchSpanProcessor(traceExporter);
const resource = new Resource({[ATTR_SERVICE_NAME]: "my_service"});
const sdk = new NodeSDK({spanProcessor, resource});
sdk.start();
const tracer = trace.getTracer("my_tracer");
tracer.startSpan("Hello World").end();
sdk.shutdown().catch(console.error);
Then run these commands:
export OTEL_EXPORTER_OTLP_ENDPOINT=https://logfire-us.pydantic.dev
export OTEL_EXPORTER_OTLP_HEADERS='Authorization=your-write-token'
npm init es6 -y # creates package.json with type module
npm install @opentelemetry/sdk-node
node main.js
See the dedicated Rust page for the first-party
logfirecrate, which gives you a native Rust API over OpenTelemetry. The example below uses the raw OpenTelemetry SDK instead.
First, set up a new Cargo project:
cargo new --bin otel-example && cd otel-example
export OTEL_EXPORTER_OTLP_ENDPOINT=https://logfire-us.pydantic.dev
export OTEL_EXPORTER_OTLP_HEADERS='Authorization=your-write-token'
export OTEL_SERVICE_NAME=my-service
Update the Cargo.toml and main.rs files with the following contents:
[package]
name = "otel-example"
version = "0.1.0"
edition = "2021"
[dependencies]
opentelemetry = { version = "*", default-features = false, features = ["trace"] }
# Note: `reqwest-rustls` feature is necessary else you'll have a cryptic failure to export;
# see https://github.com/open-telemetry/opentelemetry-rust/issues/2169
opentelemetry-otlp = { version = "*", default-features = false, features = ["trace", "http-proto", "reqwest-blocking-client", "reqwest-rustls"] }
use opentelemetry::{
global::ObjectSafeSpan,
trace::{Tracer, TracerProvider},
};
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let otlp_exporter = opentelemetry_otlp::new_exporter()
.http()
.with_protocol(opentelemetry_otlp::Protocol::HttpBinary)
// If you don't want to export environment variables, you can also configure
// programmatically like so:
//
// (You'll need to add `use opentelemetry_otlp::WithExportConfig;` to the top of the
// file to access the `.with_endpoint` method.)
//
// .with_endpoint("https://logfire-us.pydantic.dev/v1/traces")
// .with_headers({
// let mut headers = std::collections::HashMap::new();
// headers.insert(
// "Authorization".into(),
// "your-write-token".into(),
// );
// headers
// })
;
let tracer_provider = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(otlp_exporter)
.install_simple()?;
let tracer = tracer_provider.tracer("my_tracer");
tracer.span_builder("Hello World").start(&tracer).end();
Ok(())
}
Finally, use cargo run to execute.
Run your program, then open the Live view for your project. You should see a trace with a single span named Hello World.
| Symptom | Likely cause | Fix |
|---|---|---|
| Nothing arrives, with a connection error or no error at all | The SDK is exporting over its default gRPC protocol | Set OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf (or the equivalent in code). Logfire receives data over HTTP only. |
Requests rejected with 401 or 403 | Missing or invalid write token | Set OTEL_EXPORTER_OTLP_HEADERS='Authorization=your-write-token' with a valid write token. |
Data appears under (unknown) in the Services view | No service name set | Set OTEL_SERVICE_NAME=your-service-name. |
| Connection or TLS errors | The endpoint points at the wrong region | Use https://logfire-us.pydantic.dev (US) or https://logfire-eu.pydantic.dev (EU). |