Class: SolarWindsAPM::API::CurrentTraceInfo::TraceInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/solarwinds_apm/api/current_trace_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTraceInfo

Returns a new instance of TraceInfo.



59
60
61
62
63
# File 'lib/solarwinds_apm/api/current_trace_info.rb', line 59

def initialize
  @trace_id, @span_id, @trace_flags, @tracestring = current_span
  @service_name = ENV.fetch('OTEL_SERVICE_NAME', nil)
  @do_log = log? # true if the tracecontext should be added to logs
end

Instance Attribute Details

#do_logBoolean

Returns the current value of do_log.

Returns:

  • (Boolean)

    the current value of do_log



53
54
55
# File 'lib/solarwinds_apm/api/current_trace_info.rb', line 53

def do_log
  @do_log
end

#span_idString

Returns the current value of span_id.

Returns:

  • (String)

    the current value of span_id



53
54
55
# File 'lib/solarwinds_apm/api/current_trace_info.rb', line 53

def span_id
  @span_id
end

#trace_flagsString

Returns the current value of trace_flags.

Returns:

  • (String)

    the current value of trace_flags



53
54
55
# File 'lib/solarwinds_apm/api/current_trace_info.rb', line 53

def trace_flags
  @trace_flags
end

#trace_idString

Returns the current value of trace_id.

Returns:

  • (String)

    the current value of trace_id



53
54
55
# File 'lib/solarwinds_apm/api/current_trace_info.rb', line 53

def trace_id
  @trace_id
end

#tracestringString

Returns the current value of tracestring.

Returns:

  • (String)

    the current value of tracestring



53
54
55
# File 'lib/solarwinds_apm/api/current_trace_info.rb', line 53

def tracestring
  @tracestring
end

Instance Method Details

#for_logObject

for_log returns a string in the format ‘trace_id=<trace_id> span_id=<span_id> trace_flags=<trace_flags>’ or empty string.

An empty string is returned depending on the setting for SolarWindsAPM::Config[:log_traceId], which can be :never, :sampled, :traced, or :always.

Example:

trace = SolarWindsAPM::API.current_trace_info
trace.for_log  # 'trace_id=7435a9fe510ae4533414d425dadf4e18 span_id=49e60702469db05f trace_flags=01 resource.service.name=otel_service_name' or '' depends on Config

Returns:

  • String



80
81
82
# File 'lib/solarwinds_apm/api/current_trace_info.rb', line 80

def for_log
  @for_log ||= @do_log ? "trace_id=#{@trace_id} span_id=#{@span_id} trace_flags=#{@trace_flags} resource.service.name=#{@service_name}" : ''
end

#hash_for_logObject

Construct the trace_id, span_id, trace_flags and resource.service.name for log insertion.

Example:

trace = SolarWindsAPM::API.current_trace_info
trace.hash_for_log   # { trace_id: '7435a9fe510ae4533414d425dadf4e18',
                         span_id: '49e60702469db05f',
                         trace_flags: 01,
                         resource.service.name: 'otel_service_name' }  or {} depends on Config

# For lograge:
Lograge.custom_options = lambda do |event|
  SolarWindsAPM::API.current_trace_info.hash_for_log
end

Returns:

  • Hash



102
103
104
105
106
107
108
109
# File 'lib/solarwinds_apm/api/current_trace_info.rb', line 102

def hash_for_log
  @hash_for_log = if @do_log
                    { 'trace_id' => @trace_id, 'span_id' => @span_id, 'trace_flags' => @trace_flags,
                      'resource.service.name' => @service_name }
                  else
                    {}
                  end
end