Module: Pigeon::Tracing

Defined in:
lib/pigeon/tracing.rb,
lib/pigeon/tracing/core.rb,
lib/pigeon/tracing/messaging.rb

Overview

Tracing module for Pigeon using OpenTelemetry

Defined Under Namespace

Modules: Core, Messaging

Class Method Summary collapse

Class Method Details

.available?Boolean

Check if OpenTelemetry is available

Returns:

  • (Boolean)

    Whether OpenTelemetry is available



21
22
23
# File 'lib/pigeon/tracing.rb', line 21

def available?
  Core.available?
end

.extract_context(headers) ⇒ OpenTelemetry::Context?

Extract trace context from headers

Parameters:

  • headers (Hash)

    Headers containing trace context

Returns:

  • (OpenTelemetry::Context, nil)

    Extracted context or nil if not available



54
55
56
# File 'lib/pigeon/tracing.rb', line 54

def extract_context(headers)
  Core.extract_context(headers)
end

.init(service_name: "pigeon", exporter: nil) ⇒ Boolean

Initialize OpenTelemetry tracing

Parameters:

  • service_name (String) (defaults to: "pigeon")

    Service name for traces

  • exporter (OpenTelemetry::Exporter::OTLP::Exporter, nil) (defaults to: nil)

    Optional custom exporter

Returns:

  • (Boolean)

    Whether initialization was successful



29
30
31
# File 'lib/pigeon/tracing.rb', line 29

def init(service_name: "pigeon", exporter: nil)
  Core.init(service_name: service_name, exporter: exporter)
end

.inject_context(headers = {}, context = nil) ⇒ Hash

Inject trace context into headers

Parameters:

  • headers (Hash) (defaults to: {})

    Headers to inject trace context into

  • context (OpenTelemetry::Context, nil) (defaults to: nil)

    Context to inject (defaults to current)

Returns:

  • (Hash)

    Headers with injected trace context



62
63
64
# File 'lib/pigeon/tracing.rb', line 62

def inject_context(headers = {}, context = nil)
  Core.inject_context(headers, context)
end

.trace_batch_process(batch_size) {|span| ... } ⇒ Object

Create a span for batch processing

Parameters:

  • batch_size (Integer)

    Number of messages to process

Yields:

  • (span)

    Block to execute within the span

Yield Parameters:

  • span (OpenTelemetry::Span)

    Active span

Returns:

  • (Object)

    Result of the block



102
103
104
# File 'lib/pigeon/tracing.rb', line 102

def trace_batch_process(batch_size, &)
  Messaging.trace_batch_process(batch_size, &)
end

.trace_process(message) {|span| ... } ⇒ Object

Create a span for processing a message

Parameters:

Yields:

  • (span)

    Block to execute within the span

Yield Parameters:

  • span (OpenTelemetry::Span)

    Active span

Returns:

  • (Object)

    Result of the block



93
94
95
# File 'lib/pigeon/tracing.rb', line 93

def trace_process(message, &)
  Messaging.trace_process(message, &)
end

.trace_publish(topic:, payload:, key: nil, headers: nil, correlation_id: nil) {|span, context, headers| ... } ⇒ Object

Create a span for publishing a message

Parameters:

  • topic (String)

    Kafka topic

  • payload (Hash, String)

    Message payload

  • key (String, nil) (defaults to: nil)

    Optional message key

  • headers (Hash, nil) (defaults to: nil)

    Optional message headers

  • correlation_id (String, nil) (defaults to: nil)

    Optional correlation ID

Yields:

  • (span, context, headers)

    Block to execute within the span

Yield Parameters:

  • span (OpenTelemetry::Span)

    Active span

  • context (OpenTelemetry::Context)

    Span context

  • headers (Hash)

    Headers with injected trace context

Returns:

  • (Object)

    Result of the block



77
78
79
80
81
82
83
84
85
86
# File 'lib/pigeon/tracing.rb', line 77

def trace_publish(topic:, payload:, key: nil, headers: nil, correlation_id: nil, &)
  Messaging.trace_publish(
    topic: topic,
    payload: payload,
    key: key,
    headers: headers,
    correlation_id: correlation_id,
    &
  )
end

.tracer(name = "pigeon") ⇒ OpenTelemetry::Tracer?

Get the OpenTelemetry tracer

Parameters:

  • name (String) (defaults to: "pigeon")

    Tracer name

Returns:

  • (OpenTelemetry::Tracer, nil)

    Tracer or nil if OpenTelemetry is not available



36
37
38
# File 'lib/pigeon/tracing.rb', line 36

def tracer(name = "pigeon")
  Core.tracer(name)
end

.with_span(name, attributes: {}, kind: :internal, parent_context: nil) { ... } ⇒ Object

Create a span for a block of code

Parameters:

  • name (String)

    Span name

  • attributes (Hash) (defaults to: {})

    Span attributes

  • kind (Symbol) (defaults to: :internal)

    Span kind (:internal, :server, :client, :producer, :consumer)

  • parent_context (OpenTelemetry::Context, nil) (defaults to: nil)

    Optional parent context

Yields:

  • Block to execute within the span

Returns:

  • (Object)

    Result of the block



47
48
49
# File 'lib/pigeon/tracing.rb', line 47

def with_span(name, attributes: {}, kind: :internal, parent_context: nil, &)
  Core.with_span(name, attributes: attributes, kind: kind, parent_context: parent_context, &)
end