A simple hello world example demonstrating core gintelemetry features.
- Basic telemetry setup with
Start() - Automatic request tracing via middleware
- Logging with trace correlation
- Counter metrics with attributes
- Manual span creation
- Error recording in spans
- Custom helper functions (
measureDuration) - Unified attribute API
This example uses Docker Compose to run both the OpenTelemetry Collector and Jaeger:
- OpenTelemetry Collector - Receives all telemetry (traces, metrics, logs) from your app
- Jaeger - Stores and visualizes traces
cd examples/basic
docker compose up -dThis starts:
- OpenTelemetry Collector on ports 4317 (gRPC) and 4318 (HTTP)
- Jaeger UI on port 16686
docker compose downcd examples/basic
go run main.goThe server will start on port 8080.
curl http://localhost:8080/helloThis demonstrates:
- Basic logging
- Counter metrics
- Automatic trace from middleware
curl http://localhost:8080/process/123This demonstrates:
- Manual span creation with attributes
- Nested spans (parent -> child)
- Error handling and recording
- Log/trace correlation
curl http://localhost:8080/measureThis demonstrates:
- Custom
measureDurationhelper function - Automatic duration recording
- Open http://localhost:16686
- Select "basic-example" from the service dropdown
- Click "Find Traces"
- Click on any trace to see spans, logs, and attributes
You'll see:
- Request traces with all spans
- Span attributes (endpoint, method, item.id, etc.)
- Timing information
- Error details (if you trigger an error)
Logs and metrics are sent to the OpenTelemetry Collector and output to its console:
# View collector logs (includes metrics and logs)
docker compose logs -f otel-collectorYou'll see your application logs and metrics in the collector output.
- Trace Correlation: Logs include trace/span IDs automatically
- Nested Spans: The
process/:idendpoint creates a parent span, then a child "database.query" span - Attributes: All spans include relevant attributes (ids, endpoints, etc.)
- Metrics: Counters and histograms are recorded with attributes
- Error Recording: Errors are automatically captured in spans
- Custom Helpers: The example shows how to build your own helper functions on top of the simple API
The example uses:
- gRPC protocol (default)
- Localhost endpoint
- Info log level
- Insecure connection (for local development)
For production, you'd configure TLS through OpenTelemetry SDK environment variables or use secure endpoints.
Check out other examples:
examples/database/- Database query tracing with custom helpersexamples/worker/- Background jobs and workersexamples/testing/- How to test instrumented code