-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create MetricsPatch modules to hold metrics
Like the Metrics ConfigurationPatch, include the MetricsPatch modules if Metrics is enabled/present
- Loading branch information
1 parent
ed4a354
commit a38aa87
Showing
6 changed files
with
108 additions
and
72 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
21 changes: 21 additions & 0 deletions
21
instrumentation/base/lib/opentelemetry/instrumentation/metrics_patch.rb
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,21 @@ | ||
module OpenTelemetry | ||
module Instrumentation | ||
module MetricsPatch | ||
def create_meter | ||
@meter = OpenTelemetry::Metrics::Meter.new | ||
end | ||
|
||
def install_meter | ||
@meter = OpenTelemetry.meter_provider.meter(name, version: version) if metrics_enabled? | ||
end | ||
|
||
def metrics_enabled? | ||
return @metrics_enabled if defined?(@metrics_enabled) | ||
|
||
@metrics_enabled ||= defined?(OpenTelemetry::Metrics) && @config[:send_metrics] | ||
end | ||
end | ||
end | ||
end | ||
|
||
OpenTelemetry::Instrumentation::Base.prepend(OpenTelemetry::Instrumentation::MetricsPatch) |
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
58 changes: 58 additions & 0 deletions
58
instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/metrics_patch.rb
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,58 @@ | ||
module OpenTelemetry | ||
module Instrumentation | ||
module Rack | ||
module Middlewares | ||
module MetricsPatch | ||
# Don't check in here to see if metrics is enabled, trust that if it's required, metrics will be enabled | ||
def meter | ||
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.meter | ||
end | ||
|
||
def http_server_request_duration_histogram | ||
# TODO: Add Advice to set small explicit histogram bucket boundaries | ||
# TODO: Does this need to be memoized? | ||
@http_server_request_duration_histogram ||= meter.create_histogram('http.server.request.duration', unit: 's', description: 'Duration of HTTP server requests.') | ||
end | ||
|
||
def record_http_server_request_duration_metric(span) | ||
# find span duration | ||
# end - start / a billion to convert nanoseconds to seconds | ||
duration = (span.end_timestamp - span.start_timestamp) / Float(10**9) | ||
# Create attributes | ||
# | ||
attrs = {} | ||
# pattern below goes | ||
# stable convention | ||
# current span convention | ||
|
||
# attrs['http.request.method'] | ||
attrs['http.method'] = span.attributes['http.method'] | ||
|
||
# attrs['url.scheme'] | ||
attrs['http.scheme'] = span.attributes['http.scheme'] | ||
|
||
# same in stable semconv | ||
attrs['http.route'] = span.attributes['http.route'] | ||
|
||
# attrs['http.response.status.code'] | ||
attrs['http.status_code'] = span.attributes['http.status_code'] | ||
|
||
# attrs['server.address'] ??? | ||
# attrs['server.port'] ??? | ||
# span includes host and port | ||
attrs['http.host'] = span.attributes['http.host'] | ||
|
||
# attrs not currently in span payload | ||
# attrs['network.protocol.version'] | ||
# attrs['network.protocol.name'] | ||
attrs['error.type'] = span.status.description if span.status.code == OpenTelemetry::Trace::Status::ERROR | ||
|
||
http_server_request_duration_histogram.record(duration, attributes: attrs) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
OpenTelemetry::Instrumentation::Rack::Middlewares::EventHandler.prepend(OpenTelemetry::Instrumentation::Rack::Middlewares::MetricsPatch) |