Created
November 1, 2022 15:32
-
-
Save wperron/d2566b65785a5aa2b670d07ea4cbc07a to your computer and use it in GitHub Desktop.
Server example for TraceResponse in opentelemetry-ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
source "https://rubygems.org" | |
gem "faraday", "~> 0.16.1" | |
gem "opentelemetry-api" | |
gem "opentelemetry-common" | |
gem "opentelemetry-sdk-experimental", path: '../../sdk_experimental' | |
gem "sinatra", "~> 2.0" | |
gem "puma" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
# Copyright The OpenTelemetry Authors | |
# | |
# SPDX-License-Identifier: Apache-2.0 | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'sinatra/base' | |
# Require otel-ruby | |
require 'opentelemetry-sdk-experimental' | |
# Export traces to console by default | |
ENV['OTEL_TRACES_EXPORTER'] ||= 'console' | |
# configure SDK with defaults | |
OpenTelemetry::SDK.configure | |
# Rack middleware to extract span context, create child span, and add | |
# attributes/events to the span | |
class OpenTelemetryMiddleware | |
def initialize(app) | |
@app = app | |
@tracer = OpenTelemetry.tracer_provider.tracer('sinatra', '1.0') | |
end | |
def call(env) | |
# Extract context from request headers | |
context = OpenTelemetry.propagation.extract( | |
env, | |
getter: OpenTelemetry::Common::Propagation.rack_env_getter | |
) | |
status, headers, response_body = 200, {}, '' | |
# Span name SHOULD be set to route: | |
span_name = env['PATH_INFO'] | |
# For attribute naming, see | |
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md#http-server | |
# Activate the extracted context | |
OpenTelemetry::Context.with_current(context) do | |
# Span kind MUST be `:server` for a HTTP server span | |
@tracer.in_span( | |
span_name, | |
attributes: { | |
'component' => 'http', | |
'http.method' => env['REQUEST_METHOD'], | |
'http.route' => env['PATH_INFO'], | |
'http.url' => env['REQUEST_URI'], | |
}, | |
kind: :server | |
) do |span| | |
# Run application stack | |
status, headers, response_body = @app.call(env) | |
span.set_attribute('http.status_code', status) | |
response_propagator = OpenTelemetry::Trace::Propagation::TraceContext::ResponseTextMapPropagator.new | |
response_propagator.inject(headers) | |
end | |
end | |
[status, headers, response_body] | |
end | |
end | |
class App < Sinatra::Base | |
set :bind, '0.0.0.0' | |
use OpenTelemetryMiddleware | |
get '/hello' do | |
'Hello World!' | |
end | |
run! if app_file == $0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment