Module: Buildkite::TestCollector::OTel

Defined in:
lib/buildkite/test_collector/otel.rb,
lib/buildkite/test_collector/otel/execution_child_forwarder.rb,
lib/buildkite/test_collector/otel/root_span_metrics_reporter.rb

Constant Summary collapse

DEFAULT_ENDPOINT =
"https://tests-otlp.buildkite.com/v1/traces"
RESULT_ATTRIBUTE =
"test.case.result.status"
RESULT_STATUSES =

OpenTelemetry has no standard value for skipped tests.

{
  "passed" => "pass",
  "failed" => "fail",
  "skipped" => "skipped",
}.freeze
PROCESSOR_TIMEOUT_SECONDS =
30
TRACER_NAME =
"buildkite-test-collector"
ROOT_SPAN_NAME =
"test.execution"
ROOT_MAX_QUEUE_SIZE =
8_192
ROOT_MAX_EXPORT_BATCH_SIZE =
512
ROOT_SCHEDULE_DELAY_MILLISECONDS =
1_000

Class Method Summary collapse

Class Method Details

.annotate(content) ⇒ Object

Records a point-in-time annotation as an event on whichever span is current, which during a test is the test's own trace. Safe to call when export is off or nothing is recording: it just does nothing.



178
179
180
181
182
183
184
185
186
187
# File 'lib/buildkite/test_collector/otel.rb', line 178

def annotate(content)
  return unless enabled?

  span = OpenTelemetry::Trace.current_span
  return unless span.recording?

  span.add_event("test.annotation", attributes: { "buildkite.annotation" => content.to_s })
rescue StandardError => e
  warn "[buildkite-test_collector] Could not annotate OpenTelemetry test span: #{e.class}: #{e.message}"
end

.configure!(endpoint: DEFAULT_ENDPOINT, api_token: nil, run_env: {}, instrumentations: nil, resource_attributes: {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/buildkite/test_collector/otel.rb', line 59

def configure!(endpoint: DEFAULT_ENDPOINT, api_token: nil, run_env: {}, instrumentations: nil, resource_attributes: {})
  if enabled?
    # One process serves one run: the exporters and providers live for
    # the whole process, so run identity is fixed at first configure.
    # Only credentials may change within that lifetime - a warm worker
    # re-running a suite can bring a fresh (e.g. expiring OIDC) token
    # that the exporters' snapshotted Authorization headers would
    # otherwise never learn about. A different run key means a new run,
    # which needs a new process; warn rather than misattribute silently.
    warn_run_mismatch(run_env)
    refresh_authorization(api_token)
    return
  end

  # Non-empty selections are reserved for future :all and preset support.
  # Raising fails open by design: the rescue below reports the reserved
  # value and disables export rather than crashing the suite.
  unless instrumentations.nil? || instrumentations == []
    raise ArgumentError, "otel_instrumentations must be omitted or []"
  end

  require "opentelemetry/sdk"
  require "opentelemetry/exporter/otlp"
  require "opentelemetry/trace/propagation/trace_context"

  exempt_from_vcr(endpoint)

  @api_token = api_token
  @run_key = run_env["key"]
  # Passing collector headers to the exporter bypasses its environment
  # defaults, so merge the standard OTLP headers here instead.
  environment_headers = otlp_headers_from_environment
  @authorization_from_environment = environment_headers.keys.any? do |key|
    key.casecmp?("Authorization")
  end
  headers = request_headers(run_env, api_token, environment_headers)

  # Run-level detail travels as the resource of the providers we create,
  # so every exported span carries it without repeating it per span.
  resource = run_resource(run_env, resource_attributes)

  @execution_provider = build_execution_provider(endpoint, headers, resource)
  @tracer = @execution_provider.tracer(TRACER_NAME, Buildkite::TestCollector::VERSION)
  configure_child_export(endpoint, headers, instrumentations, resource: resource)
  register_shutdown_at_exit
rescue LoadError, StandardError => e
  warn "[buildkite-test_collector] OpenTelemetry span export disabled: #{e.class}: #{e.message}"
  shutdown
end

.current_timestampObject

"Now" as the SDK would stamp it: the realtime clock, in seconds. Not Time.now, which suites that freeze time (Timecop) fake out.



134
135
136
# File 'lib/buildkite/test_collector/otel.rb', line 134

def current_timestamp
  Rational(Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond), 1_000_000_000)
end

.enabled?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/buildkite/test_collector/otel.rb', line 55

def enabled?
  !@tracer.nil?
end

.finish_test_span(span, test: nil, end_timestamp: nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/buildkite/test_collector/otel.rb', line 138

def finish_test_span(span, test: nil, end_timestamp: nil)
  return unless span

  begin
    if test
      test.otel_attributes.each do |key, value|
        span.set_attribute(key, value) unless value.nil?
      end

      result = test.otel_result
      status = RESULT_STATUSES[result]
      span.set_attribute(RESULT_ATTRIBUTE, status) if status

      if result == "failed"
        # The failure summary rides as the span status description, and
        # each individual failure as a semconv exception event - the
        # native OTel shapes, which the server maps back to the
        # execution's failure_reason and failure_expanded.
        reason = test.respond_to?(:otel_failure_reason) ? test.otel_failure_reason : nil
        span.status = OpenTelemetry::Trace::Status.error(reason.to_s)

        if test.respond_to?(:otel_exception_events)
          test.otel_exception_events.each do |attributes|
            span.add_event("exception", attributes: attributes)
          end
        end
      end
    end
  rescue StandardError => e
    warn "[buildkite-test_collector] Could not describe OpenTelemetry test span: #{e.class}: #{e.message}"
  ensure
    finish_span(span, end_timestamp)
  end

  span_duration(span)
end

.force_flushObject

Pushes any finished spans out now without stopping export. Used at the end of a suite when the process (and maybe another suite run) lives on. Both queues share one budget, roots first, like shutdown_exports, so an unreachable endpoint cannot block the suite twice over.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/buildkite/test_collector/otel.rb', line 193

def force_flush
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + PROCESSOR_TIMEOUT_SECONDS
  error = nil

  [@execution_provider, @execution_child_processor].compact.each do |component|
    remaining = [deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC), 0].max
    begin
      component.force_flush(timeout: remaining)
    rescue StandardError => e
      error ||= e
    end
  end

  if error
    warn "[buildkite-test_collector] Could not flush OpenTelemetry spans: #{error.class}: #{error.message}"
  end
end

.shutdownObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/buildkite/test_collector/otel.rb', line 211

def shutdown
  forwarder_error = deactivate_child_forwarder(@execution_child_forwarder)
  export_error = shutdown_exports(PROCESSOR_TIMEOUT_SECONDS)
  error = forwarder_error || export_error
  if error
    warn "[buildkite-test_collector] Could not shut down OpenTelemetry span export: #{error.class}: #{error.message}"
  end
ensure
  @execution_provider = nil
  @execution_child_processor = nil
  @execution_child_forwarder = nil
  @exporters = nil
  @api_token = nil
  @authorization_from_environment = nil
  @run_key = nil
  @tracer = nil
end

.start_test_spanObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/buildkite/test_collector/otel.rb', line 109

def start_test_span
  return [nil, nil] unless enabled?

  span = @tracer.start_span(
    ROOT_SPAN_NAME,
    with_parent: OpenTelemetry::Context.empty,
    links: job_span_links,
    kind: :internal,
  )
  [span, trace_id(span)]
rescue StandardError => e
  warn "[buildkite-test_collector] Could not start OpenTelemetry test span: #{e.class}: #{e.message}"
  [nil, nil]
end

.with_test_span(span) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/buildkite/test_collector/otel.rb', line 124

def with_test_span(span)
  return yield unless span

  OpenTelemetry::Context.with_value(execution_context_key, span.context.trace_id) do
    OpenTelemetry::Trace.with_span(span) { yield }
  end
end