Class: LogBrew::TelemetryContextBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/logbrew/telemetry_context.rb

Overview

Builder for one immutable, privacy-bounded shared telemetry context.

Instance Method Summary collapse

Constructor Details

#initializeTelemetryContextBuilder

Returns a new instance of TelemetryContextBuilder.



242
243
244
# File 'lib/logbrew/telemetry_context.rb', line 242

def initialize
  @value = { "schemaVersion" => TelemetryContext::SCHEMA_VERSION }
end

Instance Method Details

#buildObject



302
303
304
# File 'lib/logbrew/telemetry_context.rb', line 302

def build
  TelemetryContext.from_hash(@value)
end

#with_resource(resource) ⇒ Object



246
247
248
249
250
251
252
# File 'lib/logbrew/telemetry_context.rb', line 246

def with_resource(resource)
  unless resource.is_a?(TelemetryResource)
    raise TelemetryContextValue.invalid("resource must be a LogBrew::TelemetryResource")
  end
  @value["resource"] = resource.to_h
  self
end

#with_session(id:, previous_id: nil) ⇒ Object



275
276
277
278
279
280
# File 'lib/logbrew/telemetry_context.rb', line 275

def with_session(id:, previous_id: nil)
  session = { "id" => id }
  session["previousId"] = previous_id unless previous_id.nil?
  @value["session"] = session
  self
end

#with_subject(id:, kind:) ⇒ Object



282
283
284
285
# File 'lib/logbrew/telemetry_context.rb', line 282

def with_subject(id:, kind:)
  @value["subject"] = { "id" => id, "kind" => kind }
  self
end

#with_tag(key, value) ⇒ Object



287
288
289
290
291
292
293
294
# File 'lib/logbrew/telemetry_context.rb', line 287

def with_tag(key, value)
  normalized_key = TelemetryContextValue.tag_key(key.to_s)
  normalized_value = TelemetryContextValue.required_string(value, "tag value for #{normalized_key}")
  tags = (@value["tags"] ||= {})
  tags[normalized_key] = normalized_value
  TelemetryContextValue.require_tag_count(tags.length)
  self
end

#with_tags(tags) ⇒ Object



296
297
298
299
300
# File 'lib/logbrew/telemetry_context.rb', line 296

def with_tags(tags)
  object = TelemetryContextValue.object(tags, "telemetry context tags")
  object.each { |key, value| with_tag(key, value) }
  self
end

#with_trace(trace) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/logbrew/telemetry_context.rb', line 254

def with_trace(trace)
  unless defined?(LogBrew::TraceContext) && trace.is_a?(LogBrew::TraceContext)
    raise TelemetryContextValue.invalid("trace must be a LogBrew::TraceContext")
  end
  with_trace_ids(
    trace_id: trace.trace_id,
    span_id: trace.span_id,
    parent_span_id: trace.parent_span_id,
    sampled: trace.sampled
  )
end

#with_trace_ids(trace_id:, span_id: nil, parent_span_id: nil, sampled: nil) ⇒ Object



266
267
268
269
270
271
272
273
# File 'lib/logbrew/telemetry_context.rb', line 266

def with_trace_ids(trace_id:, span_id: nil, parent_span_id: nil, sampled: nil)
  trace = { "traceId" => trace_id }
  trace["spanId"] = span_id unless span_id.nil?
  trace["parentSpanId"] = parent_span_id unless parent_span_id.nil?
  trace["sampled"] = sampled unless sampled.nil?
  @value["trace"] = trace
  self
end