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

feat: Metrics event handler prototype #1213

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: Switch from prepending to conditions
kaylareopelle committed Oct 3, 2024
commit 3eef72a647bef5059382072d10425bd3f07b070d
1 change: 0 additions & 1 deletion instrumentation/base/lib/opentelemetry/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
require 'opentelemetry'
require 'opentelemetry-registry'
require 'opentelemetry/instrumentation/base'
require 'opentelemetry/instrumentation/metrics_patch' if defined?(OpenTelemetry::Metrics) # maybe also add Env var check?

module OpenTelemetry
# The instrumentation module contains functionality to register and install
18 changes: 8 additions & 10 deletions instrumentation/base/lib/opentelemetry/instrumentation/base.rb
Original file line number Diff line number Diff line change
@@ -4,8 +4,6 @@
#
# SPDX-License-Identifier: Apache-2.0

# TODO: Maybe add an env var for globally enabling metrics as second switch?
require_relative 'metrics_patch' if defined?(OpenTelemetry::Metrics)

module OpenTelemetry
module Instrumentation
@@ -208,13 +206,11 @@ def initialize(name, version, install_blk, present_blk,
@installed = false
@options = options
@tracer = OpenTelemetry::Trace::Tracer.new
create_meter
# check to see if the API is defined here because the config isn't available yet
@meter = OpenTelemetry::Metrics::Meter.new if defined?(OpenTelemetry::Metrics)
end
# rubocop:enable Metrics/ParameterLists

# no-op, overridden in metrics patch
def create_meter; end

# Install instrumentation with the given config. The present? and compatible?
# will be run first, and install will return false if either fail. Will
# return true if install was completed successfully.
@@ -232,12 +228,14 @@ def install(config = {})
@installed = true
end

# no-op, defined in metrics patch to use if metrics installed
def install_meter; end
def install_meter
@meter = OpenTelemetry.meter_provider.meter(name, version: version) if metrics_enabled?
end

# Metrics should not be enabled if we're trying to load them directly from base
def metrics_enabled?
false
return @metrics_enabled if defined?(@metrics_enabled)

@metrics_enabled ||= defined?(OpenTelemetry::Metrics) && @config[:send_metrics]
end

# Whether or not this instrumentation is installable in the current process. Will

This file was deleted.

Original file line number Diff line number Diff line change
@@ -48,16 +48,11 @@ def middleware_args
end
end

# def metrics_enabled?
# super
# # other conditions unique to Rack? Like Events also being available?
# end

private

def require_dependencies
require_relative 'middlewares/event_handler' if defined?(::Rack::Events)
require_relative 'middlewares/metrics_patch' if metrics_enabled?
require_relative 'middlewares/tracer_middleware'
end

Original file line number Diff line number Diff line change
@@ -158,8 +158,6 @@ def untraced_request?(env)
false
end

# no-op, defined in MetricsPatch and required if metrics enabled
def record_http_server_request_duration_metric(span); end

# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-http.md#name
#
@@ -251,12 +249,6 @@ def tracer
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.tracer
end

# no-op, overwritten by metrics patch if metrics is enabled
def meter; end

# no-op, overwritten by metrics patch if metrics is enabled
def http_server_request_duration_histogram; end

def config
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.config
end
@@ -272,6 +264,41 @@ def create_span(parent_context, request)
span.add_event('http.proxy.request.started', timestamp: request_start_time) unless request_start_time.nil?
span
end

# Metrics stuff
def metrics_enabled?
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.metrics_enabled?
end

def meter
return unless metrics_enabled?

OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.meter
end

def http_server_request_duration_histogram
return unless metrics_enabled?

@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)
return unless metrics_enabled?
# 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 = {}
attrs['http.method'] = span.attributes['http.method']
attrs['http.scheme'] = span.attributes['http.scheme']
attrs['http.route'] = span.attributes['http.route']
attrs['http.status_code'] = span.attributes['http.status_code']
attrs['http.host'] = span.attributes['http.host']
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

This file was deleted.