OpenTelemetry CI GitHub release GitHub commits since latest release (by SemVer)

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

  1. Fork it (https://github.com/wyhaines/opentelemetry-api.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

GitHub code size in bytes GitHub issues