Loading lib/labkit/tracing.rb +8 −0 Original line number Diff line number Diff line # frozen_string_literal: true require "active_support/core_ext/module/attribute_accessors" module Labkit # Tracing provides distributed tracing functionality module Tracing Loading Loading @@ -28,6 +30,12 @@ module Labkit autoload :OpentracingTracer, "labkit/tracing/adapters/opentracing_tracer" end DEFAULT_SERVICE_NAME = :'labkit-service' # Module-level attribute for storing the configured service name # Set by Factory.create_tracer when a tracer is created mattr_accessor :configured_service_name, default: DEFAULT_SERVICE_NAME # Tracing is only enabled when the `GITLAB_TRACING` env var is configured. def self.enabled? connection_string.present? Loading lib/labkit/tracing/auto_initialize.rb +2 −4 Original line number Diff line number Diff line Loading @@ -6,15 +6,13 @@ require "active_support/core_ext/object/blank" module Labkit module Tracing module AutoInitialize DEFAULT_SERVICE_NAME = "labkit-service" def self.detect_service_name(connection_string) return DEFAULT_SERVICE_NAME unless connection_string return Labkit::Tracing::DEFAULT_SERVICE_NAME unless connection_string if connection_string =~ /[?&]service_name=([^&]+)/ CGI.unescape(Regexp.last_match(1)) else DEFAULT_SERVICE_NAME Labkit::Tracing::DEFAULT_SERVICE_NAME end end Loading lib/labkit/tracing/factory.rb +5 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ module Labkit def self.create_tracer(service_name, connection_string, &config_block) return unless connection_string.present? tracer = if Tracing.otlp_connection?(connection_string) OpenTelemetryFactory.create_tracer(service_name, connection_string, &config_block) elsif Tracing.opentracing_connection?(connection_string) Loading @@ -25,6 +26,10 @@ module Labkit else raise "Unknown protocol" end Tracing.configured_service_name = service_name tracer rescue StandardError => e warn "Unable to instantiate tracer: #{e}" nil Loading lib/labkit/tracing/tracing_utils.rb +5 −1 Original line number Diff line number Diff line Loading @@ -28,6 +28,10 @@ module Labkit # - OpenTelemetry (OTLP): When GITLAB_TRACING uses otlp:// scheme # - OpenTracing: When GITLAB_TRACING uses opentracing:// scheme or not set # # For OpenTelemetry, the tracer's instrumentation scope name is determined by: # - The service_name passed to Factory.create_tracer # - Falls back to DEFAULT_SERVICE_NAME if Factory was not used # # Both OpenTelemetry and OpenTracing provide no-op tracers by default: # - OpenTelemetry: Uses ProxyTracerProvider until SDK is configured # - OpenTracing: Uses default no-op global_tracer Loading @@ -39,7 +43,7 @@ module Labkit if Tracing.otlp_connection? require "opentelemetry/sdk" otel_tracer = OpenTelemetry.tracer_provider.tracer("gitlab-labkit") otel_tracer = OpenTelemetry.tracer_provider.tracer(Tracing.configured_service_name) Adapters::OpentelemetryTracer.new(otel_tracer) else Adapters::OpentracingTracer.new(OpenTracing.global_tracer) Loading spec/labkit/tracing/auto_initialize_spec.rb +3 −3 Original line number Diff line number Diff line Loading @@ -6,11 +6,11 @@ require "spec_helper" describe Labkit::Tracing::AutoInitialize do describe ".detect_service_name" do it "returns default service name when connection string is nil" do expect(described_class.detect_service_name(nil)).to eq("labkit-service") expect(described_class.detect_service_name(nil)).to eq(:'labkit-service') end it "returns default service name when service_name query param is missing" do expect(described_class.detect_service_name("otlp://localhost:4318")).to eq("labkit-service") expect(described_class.detect_service_name("otlp://localhost:4318")).to eq(:'labkit-service') end it "extracts service_name from query parameters" do Loading Loading @@ -95,7 +95,7 @@ describe Labkit::Tracing::AutoInitialize do described_class.initialize! expect(Labkit::Tracing::Factory).to have_received(:create_tracer) .with("labkit-service", "otlp://localhost:4318") .with(:'labkit-service', "otlp://localhost:4318") end it "passes a configuration block to Factory.create_tracer" do Loading Loading
lib/labkit/tracing.rb +8 −0 Original line number Diff line number Diff line # frozen_string_literal: true require "active_support/core_ext/module/attribute_accessors" module Labkit # Tracing provides distributed tracing functionality module Tracing Loading Loading @@ -28,6 +30,12 @@ module Labkit autoload :OpentracingTracer, "labkit/tracing/adapters/opentracing_tracer" end DEFAULT_SERVICE_NAME = :'labkit-service' # Module-level attribute for storing the configured service name # Set by Factory.create_tracer when a tracer is created mattr_accessor :configured_service_name, default: DEFAULT_SERVICE_NAME # Tracing is only enabled when the `GITLAB_TRACING` env var is configured. def self.enabled? connection_string.present? Loading
lib/labkit/tracing/auto_initialize.rb +2 −4 Original line number Diff line number Diff line Loading @@ -6,15 +6,13 @@ require "active_support/core_ext/object/blank" module Labkit module Tracing module AutoInitialize DEFAULT_SERVICE_NAME = "labkit-service" def self.detect_service_name(connection_string) return DEFAULT_SERVICE_NAME unless connection_string return Labkit::Tracing::DEFAULT_SERVICE_NAME unless connection_string if connection_string =~ /[?&]service_name=([^&]+)/ CGI.unescape(Regexp.last_match(1)) else DEFAULT_SERVICE_NAME Labkit::Tracing::DEFAULT_SERVICE_NAME end end Loading
lib/labkit/tracing/factory.rb +5 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ module Labkit def self.create_tracer(service_name, connection_string, &config_block) return unless connection_string.present? tracer = if Tracing.otlp_connection?(connection_string) OpenTelemetryFactory.create_tracer(service_name, connection_string, &config_block) elsif Tracing.opentracing_connection?(connection_string) Loading @@ -25,6 +26,10 @@ module Labkit else raise "Unknown protocol" end Tracing.configured_service_name = service_name tracer rescue StandardError => e warn "Unable to instantiate tracer: #{e}" nil Loading
lib/labkit/tracing/tracing_utils.rb +5 −1 Original line number Diff line number Diff line Loading @@ -28,6 +28,10 @@ module Labkit # - OpenTelemetry (OTLP): When GITLAB_TRACING uses otlp:// scheme # - OpenTracing: When GITLAB_TRACING uses opentracing:// scheme or not set # # For OpenTelemetry, the tracer's instrumentation scope name is determined by: # - The service_name passed to Factory.create_tracer # - Falls back to DEFAULT_SERVICE_NAME if Factory was not used # # Both OpenTelemetry and OpenTracing provide no-op tracers by default: # - OpenTelemetry: Uses ProxyTracerProvider until SDK is configured # - OpenTracing: Uses default no-op global_tracer Loading @@ -39,7 +43,7 @@ module Labkit if Tracing.otlp_connection? require "opentelemetry/sdk" otel_tracer = OpenTelemetry.tracer_provider.tracer("gitlab-labkit") otel_tracer = OpenTelemetry.tracer_provider.tracer(Tracing.configured_service_name) Adapters::OpentelemetryTracer.new(otel_tracer) else Adapters::OpentracingTracer.new(OpenTracing.global_tracer) Loading
spec/labkit/tracing/auto_initialize_spec.rb +3 −3 Original line number Diff line number Diff line Loading @@ -6,11 +6,11 @@ require "spec_helper" describe Labkit::Tracing::AutoInitialize do describe ".detect_service_name" do it "returns default service name when connection string is nil" do expect(described_class.detect_service_name(nil)).to eq("labkit-service") expect(described_class.detect_service_name(nil)).to eq(:'labkit-service') end it "returns default service name when service_name query param is missing" do expect(described_class.detect_service_name("otlp://localhost:4318")).to eq("labkit-service") expect(described_class.detect_service_name("otlp://localhost:4318")).to eq(:'labkit-service') end it "extracts service_name from query parameters" do Loading Loading @@ -95,7 +95,7 @@ describe Labkit::Tracing::AutoInitialize do described_class.initialize! expect(Labkit::Tracing::Factory).to have_received(:create_tracer) .with("labkit-service", "otlp://localhost:4318") .with(:'labkit-service', "otlp://localhost:4318") end it "passes a configuration block to Factory.create_tracer" do Loading