Skip to content

Instantly share code, notes, and snippets.

@tomasbasham
Created May 6, 2021 08:45
Show Gist options
  • Save tomasbasham/2f7b66664a1a699a93a1af057918f7bc to your computer and use it in GitHub Desktop.
Save tomasbasham/2f7b66664a1a699a93a1af057918f7bc to your computer and use it in GitHub Desktop.
require 'base64'
class SpanContext
def initialize(trace_id: '', span_id: '')
raise RuntimeError 'trace_id must be 16 bytes' unless trace_id.size == 16
raise RuntimeError 'span_id must be 8 bytes' unless span_id.size == 8
@version = [0]
@trace_id = [0] + trace_id.bytes # field id 0 + trace_id
@span_id = [1] + span_id.bytes # field id 1 + span_id
@options = [2, 1] # field id 2 + trace options
end
def to_bytes
@version + @trace_id + @span_id + @options
end
def pack
to_bytes.pack('C*')
end
end
span_context = SpanContext.new(trace_id: '@ABCDEFGHIJKLMNO', span_id: 'abcdefgh')
print Base64.urlsafe_encode64(span_context.pack, padding: false)
print "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment