Commit ac39ddcc authored by Bob Van Landuyt's avatar Bob Van Landuyt 💬
Browse files

Merge branch 'ef-building-on-fields-package' into 'master'

Builds on the proposed Fields package in Labkit

See merge request !187

Merged-by: Bob Van Landuyt's avatarBob Van Landuyt <bob@gitlab.com>
Approved-by: Bob Van Landuyt's avatarBob Van Landuyt <bob@gitlab.com>
Reviewed-by: Bob Van Landuyt's avatarBob Van Landuyt <bob@gitlab.com>
Co-authored-by: Elliot Forbes's avatare_forbes <eforbes@gitlab.com>
parents cf82358b ee786aae
Loading
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@ require "active_support/core_ext/string/starts_ends_with"
require "active_support/core_ext/string/inflections"
require "active_support/core_ext/object/blank"

require_relative "fields"

module Labkit
  # A context can be used to provide structured information on what resources
  # GitLab is working on within a service.
@@ -24,9 +26,14 @@ module Labkit
  #   end
  #
  class Context
    # The meta log key is used to effectively aggregate
    # the attributes that we should be associating with
    # the logs that we emit.
    # These fields will get propagated across all services
    # which will allow us to correlate logs across these
    # different services.
    LOG_KEY = "meta"
    CORRELATION_ID_KEY = "correlation_id"
    RAW_KEYS = [CORRELATION_ID_KEY].freeze
    RAW_KEYS = [Fields::CORRELATION_ID].freeze

    class << self
      def with_context(attributes = {})
@@ -92,7 +99,7 @@ module Labkit
    end

    def correlation_id
      data[CORRELATION_ID_KEY]
      data[Fields::CORRELATION_ID]
    end

    def get_attribute(attribute)
@@ -113,7 +120,7 @@ module Labkit

      # Assign a correlation if it was missing in the first context or when
      # explicitly removed
      data[CORRELATION_ID_KEY] ||= new_id
      data[Fields::CORRELATION_ID] ||= new_id

      data
    end
+3 −3
Original line number Diff line number Diff line
@@ -5,11 +5,11 @@ module Labkit
    # CorrelationId module provides access the Correlation-ID
    # of the current request
    module CorrelationId
      LOG_KEY = Labkit::Context::CORRELATION_ID_KEY

      class << self
        def use_id(correlation_id)
          Labkit::Context.with_context(LOG_KEY => correlation_id) do |context|
          Labkit::Context.with_context(
            Labkit::Fields::CORRELATION_ID => correlation_id
          ) do |context|
            yield(context.correlation_id)
          end
        end
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ module Labkit
        data[:time] = timestamp.utc.iso8601(3)

        if self.class.exclude_context?
          data[Labkit::Correlation::CorrelationId::LOG_KEY] = Labkit::Correlation::CorrelationId.current_id
          data[Labkit::Fields::CORRELATION_ID] = Labkit::Correlation::CorrelationId.current_id
        else
          data.merge!(Labkit::Context.current.to_h)
        end
+1 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ module Labkit
      end

      def call(env)
        Labkit::Context.with_context(Labkit::Context::CORRELATION_ID_KEY => correlation_id(env)) do |context|
        Labkit::Context.with_context(Labkit::Fields::CORRELATION_ID => correlation_id(env)) do |context|
          status, headers, response = @app.call(env)

          headers[HEADER] = context_to_json(context)
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ module Labkit
            # will always generate a new correlation_id and we'd rather carry
            # through the correlation_id from the previous job if it is
            # present (eg. for retries).
            attributes[Labkit::Context::CORRELATION_ID_KEY] = job["correlation_id"] if job["correlation_id"]
            attributes[Labkit::Fields::CORRELATION_ID] = job["correlation_id"] if job["correlation_id"]

            Labkit::Context.with_context(attributes) do |context|
              job.merge!(context.to_h)
Loading