Sha256: 4ee78f9fbb7564d8dbeff086d53f7d934ff5461c11df013d3625dafba3063489

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 Bytes

Contents

module Utrace
  class Otel
    def initialize(name: self.class.name)
      @tracer = OpenTelemetry.tracer_provider.tracer(name)
    end

    def new_span(name, &block)
      @tracer.in_span(name.to_s) { |_span| block.call }
    end

    def event(name, attrs: nil)
      ::OpenTelemetry::Trace.current_span.add_event(name, attributes: validate_attrs(attrs))
    end

    def error(err)
      ::OpenTelemetry::Trace.current_span.record_exception(err)
    end

    def attrs(attrs)
      attrs = validate_attrs(attrs)
      ::OpenTelemetry::Trace.current_span.add_attributes(attrs)
    end

    def trace_id
      ::OpenTelemetry::Trace.current_span.context.trace_id.unpack1('H*')
    end

    private

    def validate_attrs(attrs)
      return nil unless attrs

      new_attrs = {}
      attrs.each_pair do |key, value|
        new_attrs[key.to_s] = value if ::OpenTelemetry::SDK::Internal.valid_value?(value)
      end
      new_attrs
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
utrace-0.2.0 lib/utrace/otel.rb