06 – Generating Traces (Hands-On with Applications)
Learning Objectives
- Auto-instrument a sample application in your preferred language.
- Configure OTel SDK exporters to the Collector.
- Verify traces arrive in Grafana Explore.
Language Options (choose one to start)
Node.js (Express)
npm init -y && npm i express @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-trace-otlp-grpc
tracing.js:
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const exporter = new OTLPTraceExporter({ url: 'grpc://localhost:4317' });
const sdk = new NodeSDK({
traceExporter: exporter,
instrumentations: [getNodeAutoInstrumentations()],
resource: undefined,
});
sdk.start();
app.js:
require('./tracing');
const express = require('express');
const app = express();
app.get('/hello', async (_req, res) => res.send('world'));
app.listen(8080, () => console.log('app on :8080'));
Python (Flask)
pip install flask opentelemetry-sdk opentelemetry-exporter-otlp opentelemetry-instrumentation-flask
app.py:
from flask import Flask
app = Flask(__name__)
@app.get('/hello')
def hello():
return 'world'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Run with auto instrumentation:
opentelemetry-instrument \
--traces_exporter otlp \
--exporter_otlp_endpoint http://localhost:4318 \
--service_name flask-app python app.py
Go (net/http)
go get go.opentelemetry.io/otel/sdk@latest \
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp@latest
main.go (minimal example sending OTLP HTTP to collector):
package main
// initialize tracer provider with OTLP HTTP exporter to http://localhost:4318/v1/traces
// implement /hello handler; ensure spans are created via HTTP instrumentation or manual spans.
Java (agent)
Download the agent JAR (opentelemetry-javaagent) and run:
JAVA_TOOL_OPTIONS="-javaagent:/path/opentelemetry-javaagent.jar" \
OTEL_TRACES_EXPORTER=otlp \
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 \
OTEL_SERVICE_NAME=spring-app \
java -jar app.jar
Hands-on Lab
- Start the stack from Section 5.
- Run your app; generate traffic (
curl http://localhost:8080/hello). - In Grafana → Explore → Data Source: Tempo → search by service name or use TraceQL.
Deliverables
- One running app emitting traces visible in Grafana Explore.
Quiz (Self-check)
- What environment variables configure the OTLP endpoint for your SDK/agent?
- What’s the difference between auto and manual instrumentation?
Resources
- Language-specific OTel instrumentation libraries
- OTel Semantic Conventions