Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs/example: add example on how to add opentelemetry headers/support #161

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ Layout/BeginEndAlignment:
Enabled: true
Layout/EmptyLinesAroundAttributeAccessor:
Enabled: true
Layout/FirstHashElementIndentation:
EnforcedStyle: consistent
Layout/SpaceAroundMethodCallOperator:
Enabled: true
Layout/MultilineMethodCallIndentation:
Expand Down
63 changes: 63 additions & 0 deletions examples/extending_unleash_with_opentelemetry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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