Verified Commit 3a5a8232 authored by Hercules Merscher's avatar Hercules Merscher 🌴
Browse files

fix: Tracing configured service name

parent b1e4598d
Loading
Loading
Loading
Loading
+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
@@ -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?
+2 −4
Original line number Diff line number Diff line
@@ -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

+5 −0
Original line number Diff line number Diff line
@@ -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)
@@ -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
+5 −1
Original line number Diff line number Diff line
@@ -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
@@ -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)
+3 −3
Original line number Diff line number Diff line
@@ -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
@@ -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