Sha256: 0a5f64de2c719a4f207e8b6f58b0a84f5bc33288d79e81cc69fd422442bf9bb9

Contents?: true

Size: 1008 Bytes

Versions: 7

Compression:

Stored size: 1008 Bytes

Contents

# frozen_string_literal: true

module YgreneIstioTracing
  module Tracing
    class NotSetError < ::StandardError
      def initialize
        super('Context is not set for this thread')
      end
    end

    class Context
      VAR_NAME = :_istio_tracing_context_

      class << self
        def current
          Thread.current.thread_variable_get(VAR_NAME) || raise(NotSetError)
        end

        def with_given_context(ctx)
          Thread.current.thread_variable_set(VAR_NAME, ctx)
          yield
        ensure
          remove_current
        end

        def exists_current?
          !!Thread.current.thread_variable_get(VAR_NAME)
        end

        def build_current(headers)
          Thread.current.thread_variable_set(VAR_NAME, Context.new(headers))
        end

        def remove_current
          Thread.current.thread_variable_set(VAR_NAME, nil)
        end
      end

      attr_reader :headers

      def initialize(headers)
        @headers = headers
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ygrene_istio_tracing-1.0.11 lib/ygrene_istio_tracing/tracing/context.rb
ygrene_istio_tracing-1.0.10 lib/ygrene_istio_tracing/tracing/context.rb
ygrene_istio_tracing-1.0.9 lib/ygrene_istio_tracing/tracing/context.rb
ygrene_istio_tracing-1.0.8 lib/ygrene_istio_tracing/tracing/context.rb
ygrene_istio_tracing-1.0.7 lib/ygrene_istio_tracing/tracing/context.rb
ygrene_istio_tracing-1.0.4 lib/ygrene_istio_tracing/tracing/context.rb
ygrene_istio_tracing-1.0.3 lib/ygrene_istio_tracing/tracing/context.rb