Verified Commit 9da6d410 authored by Hercules Merscher's avatar Hercules Merscher 🌴
Browse files

feat: Abstracting connection string recognition in Tracing module

parent f6fde9d4
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -36,6 +36,14 @@ module Labkit
      ENV["GITLAB_TRACING"]
    end

    def self.otlp_connection?(connection_string = ENV["GITLAB_TRACING"])
      connection_string.to_s.start_with?("#{OpenTelemetryFactory::OTLP_SCHEME}://")
    end

    def self.opentracing_connection?(connection_string = ENV["GITLAB_TRACING"])
      connection_string.to_s.start_with?("#{OpenTracingFactory::OPENTRACING_SCHEME}://")
    end

    def self.tracing_url_template
      ENV["GITLAB_TRACING_URL"]
    end
+2 −12
Original line number Diff line number Diff line
@@ -9,9 +9,9 @@ module Labkit
      def self.create_tracer(service_name, connection_string)
        return unless connection_string.present?

        if otlp_connection?(connection_string)
        if Tracing.otlp_connection?(connection_string)
          OpenTelemetryFactory.create_tracer(service_name, connection_string)
        elsif opentracing_connection?(connection_string)
        elsif Tracing.opentracing_connection?(connection_string)
          OpenTracingFactory.create_tracer(service_name, connection_string)
        else
          raise "Unknown protocol"
@@ -20,16 +20,6 @@ module Labkit
        warn "Unable to instantiate tracer: #{e}"
        nil
      end

      def self.otlp_connection?(connection_string)
        connection_string.to_s.start_with?("#{OpenTelemetryFactory::OTLP_SCHEME}://")
      end
      private_class_method :otlp_connection?

      def self.opentracing_connection?(connection_string)
        connection_string.to_s.start_with?("#{OpenTracingFactory::OPENTRACING_SCHEME}://")
      end
      private_class_method :opentracing_connection?
    end
  end
end
+51 −1
Original line number Diff line number Diff line
@@ -19,6 +19,57 @@ describe Labkit::Tracing do
    end
  end

  describe ".otlp_connection?" do
    where(:connection_string, :is_otlp) do
      nil | false
      "" | false
      "otlp://localhost:4318" | true
      "otlp://localhost:4317" | true
      "otlps://secure.host:4318" | false
      "opentracing://jaeger" | false
      "http://localhost:4318" | false
      "invalid" | false
    end

    with_them do
      it "returns correct detection result" do
        expect(described_class.otlp_connection?(connection_string)).to eq(is_otlp)
      end
    end

    context "when no argument provided" do
      it "uses ENV['GITLAB_TRACING']" do
        stub_const("ENV", ENV.to_hash.merge("GITLAB_TRACING" => "otlp://localhost:4318"))
        expect(described_class.otlp_connection?).to be(true)
      end
    end
  end

  describe ".opentracing_connection?" do
    where(:connection_string, :is_opentracing) do
      nil | false
      "" | false
      "opentracing://jaeger" | true
      "opentracing://zipkin" | true
      "otlp://localhost:4318" | false
      "http://localhost:4318" | false
      "invalid" | false
    end

    with_them do
      it "returns correct detection result" do
        expect(described_class.opentracing_connection?(connection_string)).to eq(is_opentracing)
      end
    end

    context "when no argument provided" do
      it "uses ENV['GITLAB_TRACING']" do
        stub_const("ENV", ENV.to_hash.merge("GITLAB_TRACING" => "opentracing://jaeger"))
        expect(described_class.opentracing_connection?).to be(true)
      end
    end
  end

  describe ".tracing_url_enabled?" do
    using RSpec::Parameterized::TableSyntax

@@ -76,7 +127,6 @@ describe Labkit::Tracing do
    end

    it "should generate a backtrace when backtrace is enabled" do
      double("span")
      fake_adapter = double("span adapter")
      fake_tracer = double("tracer")