Sha256: 99b8105367f5c3b76dec99be71cb9080692530146e56fc0c4ec6fb2593fa90f1

Contents?: true

Size: 1.87 KB

Versions: 22

Compression:

Stored size: 1.87 KB

Contents

# example on how to extend the unleash client with opentelemetry by monkey patching it.
# to be added before initializing the client.
# in rails it could be added, for example, at:
# config/initializers/unleash.rb

require 'opentelemetry-api'
require 'unleash'

module UnleashExtensions
  module OpenTelemetry
    TRACER = ::OpenTelemetry.tracer_provider.tracer('Unleash-Client', Unleash::VERSION)

    module Client
      def initialize(*opts)
        UnleashExtensions::OpenTelemetry::TRACER.in_span("#{self.class.name}##{__method__}") do |_span|
          super(*opts)
        end
      end

      def is_enabled?(feature, *args)
        UnleashExtensions::OpenTelemetry::TRACER.in_span("#{self.class.name}##{__method__}") do |span|
          result = super(feature, *args)

          # OpenTelemetry::SemanticConventions::Trace::FEATURE_FLAG_* is not in the `opentelemetry-semantic_conventions` gem yet
          span.add_attributes({
            'feature_flag.provider_name' => 'Unleash',
            'feature_flag.key' => feature,
            'feature_flag.variant' => result
          })

          result
        end
      end
    end
  end

  module MetricsReporter
    def post
      UnleashExtensions::OpenTelemetry::TRACER.in_span("#{self.class.name}##{__method__}") do |_span|
        super
      end
    end
  end

  module ToggleFetcher
    def fetch
      UnleashExtensions::OpenTelemetry::TRACER.in_span("#{self.class.name}##{__method__}") do |_span|
        super
      end
    end

    def save!
      UnleashExtensions::OpenTelemetry::TRACER.in_span("#{self.class.name}##{__method__}") do |_span|
        super
      end
    end
  end
end

# MonkeyPatch here:
::Unleash::Client.prepend UnleashExtensions::OpenTelemetry::Client
::Unleash::MetricsReporter.prepend UnleashExtensions::OpenTelemetry::MetricsReporter
::Unleash::ToggleFetcher.prepend UnleashExtensions::OpenTelemetry::ToggleFetcher

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
unleash-6.0.9 examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.8 examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.8.beta.1 examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.7 examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.6 examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.5 examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.0 examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.5.pre examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.4.pre examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.3.pre examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.2.pre examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.1.pre examples/extending_unleash_with_opentelemetry.rb
unleash-6.0.0.pre examples/extending_unleash_with_opentelemetry.rb
unleash-5.1.1 examples/extending_unleash_with_opentelemetry.rb
unleash-5.1.0 examples/extending_unleash_with_opentelemetry.rb
unleash-5.0.7 examples/extending_unleash_with_opentelemetry.rb
unleash-5.0.6 examples/extending_unleash_with_opentelemetry.rb
unleash-5.0.5 examples/extending_unleash_with_opentelemetry.rb
unleash-5.0.4 examples/extending_unleash_with_opentelemetry.rb
unleash-5.0.3 examples/extending_unleash_with_opentelemetry.rb