github.com/grafana/pyroscope@v1.18.0/examples/tracing/ruby/lib/server.rb (about)

     1  require "sinatra"
     2  require "thin"
     3  require "pyroscope"
     4  require "pyroscope/otel"
     5  require "opentelemetry-sdk"
     6  require 'opentelemetry-exporter-otlp'
     7  require 'opentelemetry/trace/propagation/trace_context'
     8  require_relative 'scooter/scooter'
     9  require_relative 'bike/bike'
    10  require_relative 'car/car'
    11  
    12  app_name = ENV.fetch("PYROSCOPE_APPLICATION_NAME", "rideshare.ruby.push.app")
    13  pyroscope_server_address = ENV.fetch("PYROSCOPE_SERVER_ADDRESS", "http://pyroscope:4040")
    14  
    15  Pyroscope.configure do |config|
    16    config.app_name = app_name
    17    config.server_address = pyroscope_server_address
    18    config.tags = {
    19      "region": ENV["REGION"],
    20    }
    21  end
    22  
    23  OpenTelemetry::SDK.configure do |c|
    24    c.add_span_processor Pyroscope::Otel::SpanProcessor.new("#{app_name}.cpu", pyroscope_server_address)
    25  
    26    c.add_span_processor(
    27      OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
    28        OpenTelemetry::Exporter::OTLP::Exporter.new(
    29          endpoint: 'http://tempo:4318/v1/traces'
    30        )
    31      )
    32    )
    33  
    34  end
    35  
    36  # Extract trace context from load generator requests to link our handler spans with the parent
    37  # load generator trace, creating a complete distributed trace across both services.
    38  before do
    39    if (traceparent = request.env['HTTP_TRACEPARENT'])
    40      # Parse traceparent: version-traceid-spanid-flags
    41      _version, trace_id_hex, parent_span_id_hex, _flags = traceparent.split('-')
    42  
    43      # Get the propagator and carrier
    44      carrier = { 'traceparent' => traceparent }
    45  
    46      # Extract context using the propagator
    47      @extracted_context = OpenTelemetry.propagation.extract(carrier)
    48    end
    49  end
    50  
    51  tracer = OpenTelemetry.tracer_provider.tracer('my-tracer')
    52  
    53  get "/bike" do
    54    OpenTelemetry::Context.with_current(@extracted_context) do
    55      tracer.in_span("BikeHandler") do |span|
    56        order_bike(0.4)
    57        "<p>Bike ordered</p>"
    58      end
    59    end
    60  end
    61  
    62  get "/scooter" do
    63    OpenTelemetry::Context.with_current(@extracted_context) do
    64      tracer.in_span("ScooterHandler") do |span|
    65        order_scooter(0.6)
    66        "<p>scooter ordered</p>"
    67      end
    68    end
    69  end
    70  
    71  get "/car" do
    72    OpenTelemetry::Context.with_current(@extracted_context) do
    73      tracer.in_span("CarHandler") do |span|
    74        order_car(0.8)
    75        "<p>car ordered</p>"
    76      end
    77    end
    78  end
    79  
    80  set :bind, '0.0.0.0'
    81  set :port, ENV["RIDESHARE_LISTEN_PORT"] || 5000
    82  
    83  run Sinatra::Application.run!