class ElasticAPM::OpenTracing::Span

@api private

def context

def context
  @span_context
end

def finish(clock_end: Util.monotonic_micros, end_time: nil)

rubocop:enable Lint/UnusedMethodArgument
def finish(clock_end: Util.monotonic_micros, end_time: nil)
  return unless (agent = ElasticAPM.agent)
  if end_time
    warn '[ElasticAPM] DEPRECATED: Setting a custom end time as a ' \
      '`Time` is deprecated. Use `clock_end:` and monotonic time instead.'
    clock_end = end_time
  end
  elastic_span.done clock_end: clock_end
  case elastic_span
  when ElasticAPM::Transaction
    agent.instrumenter.current_transaction = nil
  when ElasticAPM::Span
    agent.instrumenter.current_spans.delete(elastic_span)
  end
  agent.enqueue elastic_span
end

def get_baggage_item(_key)

def get_baggage_item(_key)
  ElasticAPM.agent.config.logger.warn(
    'Baggage is not supported by ElasticAPM'
  )
  nil
end

def initialize(elastic_span, span_context)

def initialize(elastic_span, span_context)
  @elastic_span = elastic_span
  @span_context = span_context
end

def log_kv(timestamp: nil, **fields)

rubocop:disable Lint/UnusedMethodArgument
def log_kv(timestamp: nil, **fields)
  if (exception = fields[:'error.object'])
    ElasticAPM.report exception
  elsif (message = fields[:message])
    ElasticAPM.report_message message
  end
end

def operation_name=(name)

def operation_name=(name)
  elastic_span.name = name
end

def set_baggage_item(_key, _value)

def set_baggage_item(_key, _value)
  ElasticAPM.agent.config.logger.warn(
    'Baggage is not supported by ElasticAPM'
  )
end

def set_label(key, val)

def set_label(key, val)
  if elastic_span.is_a?(Transaction)
    case key.to_s
    when 'type'
      elastic_span.type = val
    when 'result'
      elastic_span.result = val
    when /user\.(\w+)/
      set_user_value($1, val)
    else
      elastic_span.context.labels[key] = val
    end
  else
    elastic_span.context.labels[key] = val
  end
end

def set_user_value(key, value)

def set_user_value(key, value)
  return unless elastic_span.is_a?(Transaction)
  setter = :"#{key}="
  return unless elastic_span.context.user.respond_to?(setter)
  elastic_span.context.user.send(setter, value)
end