Verified Commit 20e808fa authored by Hercules Merscher's avatar Hercules Merscher 🌴
Browse files

fix: OpenTelemetry rejects nil attributes

parent 3f304a94
Loading
Loading
Loading
Loading
+4 −13
Original line number Diff line number Diff line
@@ -12,20 +12,11 @@ module Labkit
        def tags(payload)
          # Duration is calculated by start and end time
          # Exception is already captured in lib/labkit/tracing/tracing_utils.rb
          tags = {
            "component" => "external_http",
            "method" => payload[:method],
            "code" => payload[:code],
            "host" => payload[:host],
            "port" => payload[:port],
            "path" => payload[:path],
            "scheme" => payload[:scheme],
          }
          tags = payload.slice(:method, :code, :host, :port, :path, :scheme, :proxy_host, :proxy_port)
          tags.transform_keys!(&:name)
          tags["component"] = "external_http"

          unless payload[:proxy_host].nil?
            tags["proxy_host"] = payload[:proxy_host]
            tags["proxy_port"] = payload[:proxy_port]
          end
          tags.compact!

          tags
        end
+6 −1
Original line number Diff line number Diff line
@@ -16,7 +16,12 @@ module Labkit
          end

          def tags(payload)
            { "component" => COMPONENT_TAG, "template.id" => payload[:identifier], "template.layout" => payload[:layout] }
            tags = { "component" => COMPONENT_TAG }
            # OpenTelemetry rejects nil attributes, and error templates may not
            # have a layout (e.g., public/406-unsupported-browser.html).
            tags["template.id"] = payload[:identifier] if payload[:identifier]
            tags["template.layout"] = payload[:layout] if payload[:layout]
            tags
          end
        end
      end
+9 −4
Original line number Diff line number Diff line
@@ -19,15 +19,20 @@ module Labkit
              fingerprint = Labkit::Logging::Sanitizer.sql_fingerprint(sql)
            end

            {
            tags = {
              "component" => COMPONENT_TAG,
              "span.kind" => "client",
              "db.type" => "sql",
              "db.connection_id" => payload[:connection_id],
              "db.cached" => payload[:cached] || false,
              "db.statement" => sql,
              "db.statement_fingerprint" => fingerprint,
            }

            # OpenTelemetry rejects nil attributes, so only add statement fields
            # when ActiveRecord supplies SQL (schema events may omit it).
            tags["db.statement"] = sql if sql
            tags["db.statement_fingerprint"] = fingerprint if fingerprint
            tags["db.connection_id"] = payload[:connection_id] if payload[:connection_id]

            tags
          end
        end
      end
+21 −0
Original line number Diff line number Diff line
@@ -72,4 +72,25 @@ describe Labkit::Tracing::ExternalHttp::RequestInstrumenter do
      }
    end
  end

  it_behaves_like "a tracing instrumenter" do
    let(:expected_span_name) { "external_http:request" }
    let(:payload) do
      {
        method: "GET", host: "gitlab.com", port: 80,
        path: "/gitlab-org/gitlab", scheme: "https"
      }
    end

    let(:expected_tags) do
      {
        "component" => "external_http",
        "method" => "GET",
        "host" => "gitlab.com",
        "port" => 80,
        "path" => "/gitlab-org/gitlab",
        "scheme" => "https"
      }
    end
  end
end
+6 −0
Original line number Diff line number Diff line
@@ -12,6 +12,12 @@ describe Labkit::Tracing::Rails::ActionView::RenderTemplateInstrumenter do
    end
  end

  it "omits nil layout and template id" do
    instrumenter = described_class.new

    expect(instrumenter.tags({})).to eq({ "component" => "ActionView" })
  end

  describe "#span_name" do
    context "when the template identifier is nil do" do
      before do
Loading