Class: Labkit::Tracing::OpenTelemetryFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/labkit/tracing/open_telemetry_factory.rb

Overview

OpenTelemetryFactory will configure OpenTelemetry distributed tracing

Constant Summary collapse

OTLP_SCHEME =
"otlp"
DEFAULT_HTTP_ENDPOINT =

The default endpoint for OTLP HTTP exporter

"http://localhost:4318/v1/traces"

Class Method Summary collapse

Class Method Details

.create_tracer(service_name, connection_string) {|config| ... } ⇒ Tracer?

Returns The configured tracer or nil if initialization fails.

Parameters:

  • service_name (String)

    The service name for the tracer

  • connection_string (String)

    The connection string (e.g., “otlp://localhost:4318”)

Yields:

  • (config)

    Optional configuration block for OpenTelemetry SDK customization

Yield Parameters:

  • config (OpenTelemetry::SDK::Configurator)

    The SDK configurator

Returns:

  • (Tracer, nil)

    The configured tracer or nil if initialization fails



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/labkit/tracing/open_telemetry_factory.rb', line 26

def create_tracer(service_name, connection_string, &)
  return unless connection_string.present?

  options = parse_otlp_connection_string(connection_string)
  # The service_name parameter from GITLAB_TRACING takes precedence over the application one
  service_name = options[:service_name] if options[:service_name]

  # parse exporter headers as necessary
  headers = build_headers(options)

  # Get sampler and exporter from GITLAB_TRACING
  sampler = get_sampler(options[:sampler], options[:sampler_param])
  exporter = get_exporter(options[:http_endpoint], options[:udp_endpoint], headers)

  # Build base resource
  base_resource = OpenTelemetry::SDK::Resources::Resource.create(
    OpenTelemetry::SemanticConventions::Resource::SERVICE_NAME => service_name
  )

  configure(service_name, base_resource, sampler, exporter, &)

  extra_params = options.except(
    :sampler,
    :sampler_param,
    :http_endpoint,
    :udp_endpoint,
    :strict_parsing,
    :debug,
    :service_name
  )

  if extra_params.present?
    message = "opentelemetry tracer: invalid option: #{extra_params.keys.join(', ')}"

    raise message if options[:strict_parsing]

    warn message
  end

  OpenTelemetry.tracer_provider.tracer(service_name)
end