Class: Datadog::Workers::TraceWriter

Inherits:
Datadog::Worker show all
Defined in:
lib/ddtrace/workers/trace_writer.rb

Overview

Writes traces to transport synchronously

Direct Known Subclasses

AsyncTraceWriter

Defined Under Namespace

Classes: FlushCompleted

Instance Attribute Summary collapse

Attributes inherited from Datadog::Worker

#task

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TraceWriter

Returns a new instance of TraceWriter.



17
18
19
20
21
22
23
24
25
# File 'lib/ddtrace/workers/trace_writer.rb', line 17

def initialize(options = {})
  transport_options = options.fetch(:transport_options, {})

  transport_options[:agent_settings] = options[:agent_settings] if options.key?(:agent_settings)

  @transport = options.fetch(:transport) do
    Transport::HTTP.default(**transport_options)
  end
end

Instance Attribute Details

#transportObject (readonly)

Returns the value of attribute transport.



14
15
16
# File 'lib/ddtrace/workers/trace_writer.rb', line 14

def transport
  @transport
end

Instance Method Details

#flush_completedObject

TODO: Register Datadog::Diagnostics::EnvironmentLogger.log! TODO: as a flush_completed subscriber when the TraceWriter TODO: instantiation code is implemented.



72
73
74
# File 'lib/ddtrace/workers/trace_writer.rb', line 72

def flush_completed
  @flush_completed ||= FlushCompleted.new
end

#flush_traces(traces) ⇒ Object



54
55
56
57
58
# File 'lib/ddtrace/workers/trace_writer.rb', line 54

def flush_traces(traces)
  transport.send_traces(traces).tap do |response|
    flush_completed.publish(response)
  end
end

#inject_hostname!(traces) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/ddtrace/workers/trace_writer.rb', line 60

def inject_hostname!(traces)
  traces.each do |trace|
    next if trace.first.nil?

    hostname = Datadog::Core::Environment::Socket.hostname
    trace.first.set_tag(Ext::NET::TAG_HOSTNAME, hostname) unless hostname.nil? || hostname.empty?
  end
end

#perform(traces) ⇒ Object



27
28
29
# File 'lib/ddtrace/workers/trace_writer.rb', line 27

def perform(traces)
  write_traces(traces)
end

#process_traces(traces) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/ddtrace/workers/trace_writer.rb', line 44

def process_traces(traces)
  # Run traces through the processing pipeline
  traces = Pipeline.process!(traces)

  # Inject hostname if configured to do so
  inject_hostname!(traces) if Datadog.configuration.report_hostname

  traces
end

#write(trace) ⇒ Object



31
32
33
# File 'lib/ddtrace/workers/trace_writer.rb', line 31

def write(trace)
  write_traces([trace])
end

#write_traces(traces) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/ddtrace/workers/trace_writer.rb', line 35

def write_traces(traces)
  traces = process_traces(traces)
  flush_traces(traces)
rescue StandardError => e
  Datadog.logger.error(
    "Error while writing traces: dropped #{traces.length} items. Cause: #{e} Location: #{Array(e.backtrace).first}"
  )
end