Appearance
Language setup
Each example below uses the official OpenTelemetry instrumentation for that language. The endpoint settings are the same for every language:
sh
export OTEL_SERVICE_NAME=checkout
export OTEL_RESOURCE_ATTRIBUTES=deployment.environment=production
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_TRACES_EXPORTER=otlp OTEL_METRICS_EXPORTER=otlp OTEL_LOGS_EXPORTER=otlpsh
export OTEL_SERVICE_NAME=checkout
export OTEL_RESOURCE_ATTRIBUTES=deployment.environment=production
export OTEL_EXPORTER_OTLP_ENDPOINT=https://logcore.lsofito.com
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_HEADERS=x-logcore-api-key=lc_xxxxxxxx
export OTEL_TRACES_EXPORTER=otlp OTEL_METRICS_EXPORTER=otlp OTEL_LOGS_EXPORTER=otlpIn the rest of this page, "the variables above" means one of these two blocks.
Node.js
Zero-code auto-instrumentation covers HTTP, Express, Fastify, NestJS, pg, mysql, redis, MongoDB, gRPC and more.
sh
npm install @opentelemetry/api @opentelemetry/auto-instrumentations-nodeStart your app with the variables above plus:
sh
export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"
node server.jsWith PM2, put the variables in ecosystem.config.js:
js
module.exports = {
apps: [{
name: "checkout",
script: "server.js",
env: {
NODE_OPTIONS: "--require @opentelemetry/auto-instrumentations-node/register",
OTEL_SERVICE_NAME: "checkout",
OTEL_EXPORTER_OTLP_ENDPOINT: "http://localhost:4318",
OTEL_TRACES_EXPORTER: "otlp",
OTEL_METRICS_EXPORTER: "otlp",
OTEL_LOGS_EXPORTER: "otlp",
},
}],
};Your own log records: use the logs API from @opentelemetry/api, which is the one the auto-instrumentation registers:
js
const { logs, SeverityNumber } = require("@opentelemetry/api");
const logger = logs.getLogger("checkout");
logger.emit({ severityNumber: SeverityNumber.ERROR, severityText: "ERROR", body: "payment failed" });Logs from pino and winston are bridged automatically when those libraries are installed.
Python
Works with Flask, Django, FastAPI, requests, SQLAlchemy, psycopg, redis and more.
sh
pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install # installs instrumentations for the libraries you useRun your app through opentelemetry-instrument, with the variables above:
sh
export OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true # send standard logging records
opentelemetry-instrument python app.pyWith Gunicorn:
sh
opentelemetry-instrument gunicorn -w 4 app:appJava
Download the agent JAR once:
sh
curl -fsSLo opentelemetry-javaagent.jar \
https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jarStart the JVM with it, plus the variables above:
sh
java -javaagent:/opt/otel/opentelemetry-javaagent.jar -jar app.jarThis covers Spring Boot, Servlet containers, JDBC, Kafka, gRPC, and logs from Logback and Log4j. You can also set it in JAVA_TOOL_OPTIONS="-javaagent:/opt/otel/opentelemetry-javaagent.jar".
.NET
Automatic instrumentation requires no code changes:
sh
curl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh -O
sh ./otel-dotnet-auto-install.sh
. $HOME/.otel-dotnet-auto/instrument.sh
dotnet MyApp.dllpowershell
Install-Module OpenTelemetry.DotNet.Auto -Scope AllUsers
Import-Module OpenTelemetry.DotNet.Auto
Install-OpenTelemetryCore
Register-OpenTelemetryForIIS # for IIS sites; or Register-OpenTelemetryForWindowsServiceOr with the SDK in code:
sh
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.Instrumentation.Httpcsharp
builder.Services.AddOpenTelemetry()
.WithTracing(t => t.AddAspNetCoreInstrumentation().AddHttpClientInstrumentation().AddOtlpExporter())
.WithMetrics(m => m.AddAspNetCoreInstrumentation().AddOtlpExporter());
builder.Logging.AddOpenTelemetry(o => o.AddOtlpExporter());The OTLP exporter reads the OTEL_* variables above.
Go
Go has no zero-code agent, so you add the SDK in main:
sh
go get go.opentelemetry.io/otel \
go.opentelemetry.io/otel/sdk \
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp \
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttpgo
package main
import (
"context"
"net/http"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func main() {
ctx := context.Background()
exp, err := otlptracehttp.New(ctx) // reads OTEL_EXPORTER_OTLP_* variables
if err != nil {
panic(err)
}
tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exp))
defer tp.Shutdown(ctx)
otel.SetTracerProvider(tp)
mux := http.NewServeMux()
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hi")) })
http.ListenAndServe(":8080", otelhttp.NewHandler(mux, "server"))
}OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES are read by the SDK's default resource. Use otlptracegrpc for gRPC.
PHP, Ruby and others
Any OpenTelemetry SDK with an OTLP exporter works. Set the same variables.
| Language | Getting started |
|---|---|
| PHP | open-telemetry/opentelemetry-auto-* packages plus the opentelemetry PECL extension |
| Ruby | opentelemetry-sdk, opentelemetry-exporter-otlp, opentelemetry-instrumentation-all |
| Rust | opentelemetry, opentelemetry-otlp crates |
Useful extra variables
| Variable | Purpose |
|---|---|
OTEL_RESOURCE_ATTRIBUTES | Add attributes, for example deployment.environment=staging,service.version=1.4.2. |
OTEL_METRIC_EXPORT_INTERVAL | Metric export interval in milliseconds (default 60000). |
OTEL_TRACES_SAMPLER / OTEL_TRACES_SAMPLER_ARG | Sample a fraction of traces, for example parentbased_traceidratio with 0.1 keeps 10 %. |
OTEL_SDK_DISABLED=true | Turns OpenTelemetry off without removing it. |