OpenTelemetry-API
The OpenTelemetry API for Crystal provides tracing interfaces, context, and propagation. Libraries can use it on its own to create spans and forward incoming trace context. An application installs the SDK to record and export spans.
The API implements the stable Trace API, Context, Baggage, and propagation from OpenTelemetry Specification v1.63.0. The API and SDK are versioned independently. SDK 1.0.0 pairs with API ~> 1.0. The SDK implements OTLP/HTTP using OTLP proto v1.11.1, and the instrumentation shard uses Semantic Conventions v1.45.0.
Traces are stable. Logs and metrics are unimplemented in 1.0. See Upgrading from 0.x.
Installation
Add the API to your shard.yml and run shards install.
dependencies:
opentelemetry-api:
github: wyhaines/opentelemetry-api.cr
version: ~> 1.0
Usage
Give each library or instrument a tracer named for its instrumentation scope. A tracer obtained before SDK configuration follows subsequent provider registrations.
require "opentelemetry-api"
module Inventory
TRACER = OpenTelemetry.tracer("example.inventory")
def self.count : Int32
TRACER.in_span("inventory.count", attributes: {"warehouse" => "west"}) do |span|
span.add_event("inventory checked")
12
end
end
end
puts Inventory.count
Until the application installs a provider, starting a span creates a non-recording span. A valid parent SpanContext can still be carried through the no-op tracer. To send and receive context without the SDK, install a global propagator or use a TextMap propagator directly.
require "opentelemetry-api"
propagator = OpenTelemetry::Propagation::TraceContext.new
incoming = {"traceparent" => "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"}
parent = propagator.extract(incoming, OpenTelemetry::Context.root)
outgoing = {} of String => String
tracer = OpenTelemetry.tracer("example.gateway")
tracer.in_span("forward", context: parent, kind: :client) do
propagator.inject(outgoing)
end
See the generated API reference and the SDK setup guide. The Crystal OpenTelemetry community is on Discord.
Contributing
- Fork it (https://github.com/wyhaines/opentelemetry-api.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Kirk Haines - creator and maintainer