-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Telemetry service and middleware
- Loading branch information
Showing
5 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'ditty/services/open_telemetry' | ||
|
||
module Ditty | ||
module Middleware | ||
class Telemetry | ||
attr_reader :env, :telemetry | ||
|
||
def initialize(app, telemetry = nil) | ||
@app = app | ||
@telemetry = telemetry || Services::OpenTelemetry.new | ||
end | ||
|
||
def call(env) | ||
@env = env | ||
request = Rack::Request.new(env) | ||
attribs = { | ||
::OpenTelemetry::SemanticConventions::Trace::HTTP_URL => request.url, | ||
::OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => request.request_method, | ||
} | ||
|
||
telemetry.instrumented("#{request.request_method} #{request.path}", attribs) do |span| | ||
# Not sure if we should try and catch exceptions? | ||
result = @app.call(env) | ||
span.add_attributes( | ||
::OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE => result[0] | ||
) | ||
result | ||
end | ||
end | ||
|
||
class << self | ||
def method_missing(method, *args, &block) | ||
instance.send(method, *args, &block) | ||
end | ||
|
||
def respond_to_missing?(method, _include_private) | ||
return true if instance.respond_to?(method) | ||
|
||
super | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ditty | ||
module Services | ||
class OpenTelemetry | ||
def tracer | ||
# ::OpenTelemetry::Instrumentation::Sinatra::Instrumentation.instance.tracer | ||
@tracer ||= ::OpenTelemetry.tracer_provider.tracer('Ditty', ::Ditty::VERSION) | ||
end | ||
|
||
def instrumented? | ||
(ENV['DITTY_TELEMETRY_ENABLED'] || 0).to_i == 1 | ||
end | ||
|
||
def instrumented(span_name, attribs = {}) | ||
return yield unless instrumented? | ||
|
||
tracer.in_span(span_name, attributes: base_attributes.merge(attribs)) do |span| | ||
yield(span) | ||
end | ||
end | ||
|
||
def application_error(error, opts = {}) | ||
error ||= env['sinatra.error'] | ||
return unless error.is_a? StandardError | ||
return unless @request_span | ||
|
||
request = opts[:target].request | ||
@request_span&.record_exception( | ||
error, | ||
attributes: base_attributes.merge( | ||
::OpenTelemetry::SemanticConventions::Trace::HTTP_URL => request.url, | ||
::OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => request.request_method, | ||
::OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE => opts[:error_code] || 500 | ||
) | ||
) | ||
if (opts[:error_code] || 500) >= 500 | ||
@request_span&.status = ::OpenTelemetry::Trace::Status.error("Application Error: #{error.class}") | ||
end | ||
@request_span&.finish | ||
end | ||
|
||
private | ||
|
||
def base_attributes | ||
{ | ||
'ditty.version' => ::Ditty::VERSION, | ||
} | ||
end | ||
end | ||
end | ||
end | ||
|
||
Wisper.subscribe(::Ditty::Services::OpenTelemetry.new, on: %i[application_error]) unless ENV['RACK_ENV'] == 'test' |