Skip to content

Instantly share code, notes, and snippets.

@tobert
Last active March 5, 2021 22:55
Show Gist options
  • Save tobert/fec4b4265c91a8c1e5c5db065234f26b to your computer and use it in GitHub Desktop.
Save tobert/fec4b4265c91a8c1e5c5db065234f26b to your computer and use it in GitHub Desktop.
a quick & dirty test script for testing some otel code
#!/usr/bin/env ruby
# run the otel collector with otlp/http enabled and turn on debug logging
# so you can see it print spans as they go through...
#
# configuration:
# export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:55681
#
# first iteration I thought I had an easy repro because I had set
# OTEL_RUBY_BSP_START_THREAD_ON_BOOT=false
# so don't do that :) It's almost never what you want.
require 'bundler/setup'
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
OpenTelemetry::SDK.configure
tracer = OpenTelemetry.tracer_provider.tracer("testtrace", "0.1")
# in my app I recover trace & span ids in hex from a database record
# for now just use the first span to test this can work...
hex_trace_id = ''
hex_span_id = ''
# just a regular span, nothing to see here...
tracer.in_span("testing123") do |span|
puts pp span
hex_trace_id = span.context.hex_trace_id
hex_span_id = span.context.hex_span_id
sleep 0.1
end
# my application talks to a number of fixed endpoints. it uses rails
# workers to make that asyncrhonous, and extends the rails forking
# to fork one worker per target endpoint (long story, can't change just now)
#
# first, simulate outer worker
fork do
puts " outer worker started"
# simulate "inner" worker, a weird thing I'm dealing with
# locally that forks a worker per target endpoint
fork do
puts " inner worker started, sleeping 2 seconds"
sleep 2
# recover the parent span context, converting hex back to binary
# since otel context seems to expect the binary string instead of hex
tid = [hex_trace_id].pack('H*')
sid = [hex_span_id].pack('H*')
# https://github.com/open-telemetry/opentelemetry-ruby/discussions/633
span_context = OpenTelemetry::Trace::SpanContext.new(trace_id: tid, span_id: sid, remote: true)
parent_span = OpenTelemetry::Trace::Span.new(span_context: span_context)
parent_context = OpenTelemetry::Trace.context_with_span(parent_span)
res = tracer.in_span("perform_task", kind: :consumer, with_parent: parent_context) do |span|
puts " inner worker started, in_span..."
puts " inner trace id: '#{span.context.hex_trace_id}'"
puts " inner span id: '#{span.context.hex_span_id}'"
span.set_attribute('device', 'ee1eb216-49e7-4cec-8b47-9d61237b492c')
sleep 1
puts ""
puts "-------"
puts pp tracer
puts " inner sleeping 30 seconds..."
sleep 30
end
end
puts " outer sleeping 60 seconds..."
sleep 60
end
puts "parent sleeping 100 seconds..."
sleep 100
#!/usr/bin/env ruby
# run the otel collector with otlp/http enabled and turn on debug logging
# so you can see it print spans as they go through...
#
# configuraiton:
# export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:55681
require 'bundler/setup'
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
OpenTelemetry::SDK.configure
tracer = OpenTelemetry.tracer_provider.tracer("testtrace", "0.1")
tracer.in_span("testing123") do |span|
puts pp span
sleep 0.1
end
puts "-------"
puts pp tracer
puts "sleeping 100 seconds..."
sleep 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment