**Labkit is a thin wrapper around OpenTelemetry** - it handles initialization and provides sensible defaults, but you should use OpenTelemetry APIs directly for instrumentation.
### What Labkit Provides Out-of-the-Box
-**Automatic tracer initialization** from `GITLAB_TRACING` environment variable
-**Connection string parsing** for OTLP endpoints, samplers, and exporters
-**Default service name** (`labkit-service`) with query parameter override
-**Automatic instrumentation** for Rails, Redis, and external HTTP requests
-**Correlation ID injection** into all spans automatically
-**Security sanitization** for SQL queries, URLs, and Redis commands
### What You Should Use Directly
For all span creation and manipulation, use OpenTelemetry APIs:
- Full access to OpenTelemetry capabilities (events, exceptions, status, links)
- Works with all OpenTelemetry documentation and examples
- Compatible with other OTel libraries and tools
- Future-proof as OpenTelemetry evolves
### Context Sharing
Labkit and OpenTelemetry share the same global tracer provider and context propagation mechanism. This means spans created by Labkit are visible to OpenTelemetry APIs and vice versa.
This seamless integration means you can:
- Use Labkit for initialization and defaults
- Use OpenTelemetry APIs directly for instrumentation
- Mix both approaches in the same codebase
- Trust that context propagates correctly across both
## Usage
### Automatic Initialization (Default Behavior)
@@ -192,35 +241,50 @@ end
### Manual Span Creation
Use `Labkit::Tracing.with_tracing` to create custom spans:
Labkit provides setup and defaults, but you should use OpenTelemetry APIs directly for creating spans and adding instrumentation:
**Creating spans with the tracer:**
```ruby
Labkit::Tracing.with_tracing(
operation_name: "process_data",
tags: {"user_id"=>user.id,"data_size"=>data.size}
)do|span|
# Your code here
# Get the tracer (configured by Labkit with connection string settings)
# Child span is automatically created within parent context
tracer.in_span("child_operation")do|child_span|
child_span.set_attribute("type","background")
perform_operation
end
end
```
### Initialization Order and Precedence
@@ -254,37 +318,13 @@ end
### Checking if Current Request is Sampled
```ruby
ifLabkit::Tracing.sampled?
# Current request is being traced
# Safe to add expensive tracing operations
end
```
### Direct Access to Underlying Tracer
When LabKit's abstraction doesn't provide the functionality you need, you can access the underlying tracer implementation directly using `Labkit::Tracing.tracer`:
```ruby
# Access the native tracer API
tracer=Labkit::Tracing.tracer
# Use OpenTelemetry-specific features when using OTLP connection
ifLabkit::Tracing.otlp_connection?
# tracer is an OpenTelemetry::SDK::Trace::Tracer instance