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: Use Rack::Events for instrumentation #342

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8698eb3
feat: Use Rack::Events for instrumentation
arielvalentin Feb 18, 2023
24ea563
squash: additional feature parity
arielvalentin Feb 18, 2023
06e4fe3
squash: add allowed response headers
arielvalentin Feb 20, 2023
725b722
squash: url quantization
arielvalentin Feb 20, 2023
8a2dd29
squash: Now with new Lemon Scented response headers
arielvalentin Feb 20, 2023
752a739
squash: we are now at parity
arielvalentin Feb 21, 2023
aba820e
squash: use instrumetation config
arielvalentin Feb 21, 2023
999594e
squash: Use declarative config options
arielvalentin Feb 22, 2023
5261676
squash: fix bad refactoring
arielvalentin Feb 25, 2023
e66a188
convert proxy span to an event
arielvalentin Mar 2, 2023
700f58f
refactor: move configurations to instrumentation install
arielvalentin Mar 2, 2023
7154ec7
squash: add test converage
arielvalentin Mar 2, 2023
69e60eb
squash: make response headers a little more resilient
arielvalentin Mar 2, 2023
006cea0
squash: Ensures event middleware will not cause the application to cr…
arielvalentin Mar 2, 2023
12c0e0f
squash: fix linter error
arielvalentin Mar 2, 2023
ee1c296
Merge branch 'main' into use-events-instead-of-middleware
arielvalentin Mar 2, 2023
c37d534
Merge branch 'main' into use-events-instead-of-middleware
arielvalentin Mar 2, 2023
ef52a8c
feat: Add middleware args helper for ActionPack and Sinatra
arielvalentin Mar 5, 2023
c53efcf
fix: test case
arielvalentin Mar 5, 2023
3aee1c3
fix: Rack Events is autoloaded so if the parent module is present so …
arielvalentin Mar 5, 2023
fb5e826
fix: More precise error handling
arielvalentin Mar 5, 2023
49f0bfe
fix: Ensure config is cleared/setup during installation
arielvalentin Mar 5, 2023
b7cad3f
fix: Sinatra 1.4 compatability
arielvalentin Mar 6, 2023
ce02dce
Merge branch 'main' into use-events-instead-of-middleware
arielvalentin Mar 7, 2023
0e50bba
Merge branch 'main' into use-events-instead-of-middleware
arielvalentin Mar 13, 2023
26c5a30
Merge branch 'main' into use-events-instead-of-middleware
arielvalentin Mar 13, 2023
f134ec2
Merge branch 'main' into use-events-instead-of-middleware
arielvalentin Mar 18, 2023
16a4896
Merge branch 'main' into use-events-instead-of-middleware
arielvalentin Mar 19, 2023
205cd66
fix: bad merge
arielvalentin Mar 19, 2023
e9e837e
fix: invalid responses in test case
arielvalentin Mar 19, 2023
0a961a1
squash: Added a few more test cases
arielvalentin Mar 19, 2023
19d7c79
Merge branch 'main' into use-events-instead-of-middleware
arielvalentin Mar 31, 2023
e88b745
Merge branch 'main' into use-events-instead-of-middleware
arielvalentin Apr 5, 2023
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: move configurations to instrumentation install
  • Loading branch information
arielvalentin committed Mar 2, 2023
commit 700f58f51faf6e498bc64026025aeb9af185f914
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
require_dependencies

retain_middleware_names if config[:retain_middleware_names]
configure_defaults
end

present do
Expand Down Expand Up @@ -62,6 +63,29 @@ def call(env)
next_middleware.instance_variable_get('@app')
end
end

def configure_defaults
config[:allowed_rack_request_headers] = config[:allowed_request_headers].compact.each_with_object({}) do |header, memo|
key = header.to_s.upcase.gsub(/[-\s]/, '_')
case key
when 'CONTENT_TYPE', 'CONTENT_LENGTH'
memo[key] = build_attribute_name('http.request.header.', header)
else
memo["HTTP_#{key}"] = build_attribute_name('http.request.header.', header)
end
end

config[:allowed_rack_response_headers] = config[:allowed_response_headers].each_with_object({}) do |header, memo|
memo[header] = build_attribute_name('http.response.header.', header)
memo[header.to_s.upcase] = build_attribute_name('http.response.header.', header)
end

config[:untraced_endpoints]&.compact!
end

def build_attribute_name(prefix, suffix)
prefix + suffix.to_s.downcase.gsub(/[-\s]/, '_')
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,8 @@ def on_start(request, _)
return if untraced_request?(request.env)

parent_context = extract_remote_context(request)

span = tracer.start_span(
create_request_span_name(request),
with_parent: parent_context,
kind: :server,
attributes: request_span_attributes(request.env)
)
request_start_time = OpenTelemetry::Instrumentation::Rack::Util::QueueTime.get_request_start(request.env)
span.add_event('http.proxy.request.started', timestamp: request_start_time) unless request_start_time.nil?

ctx = OpenTelemetry::Trace.context_with_span(span)
rack_ctx = OpenTelemetry::Instrumentation::Rack.context_with_span(span, parent_context: ctx)

contexts = [ctx, rack_ctx]
contexts.compact!

tokens = contexts.map { |context| OpenTelemetry::Context.attach(context) }

request.env[TOKENS_KEY] = tokens
span = create_span(parent_context, request)
request.env[TOKENS_KEY] = register_current_span(span)
end

# Optionally adds debugging response headers injected from {response_propagators}
Expand Down Expand Up @@ -207,16 +190,12 @@ def request_span_attributes(env)
attributes
end

def build_attribute_name(prefix, suffix)
prefix + suffix.to_s.downcase.gsub(/[-\s]/, '_')
end

def record_frontend_span?
config[:record_frontend_span] == true
end

def untraced_endpoints
config[:untraced_endpoints].compact
config[:untraced_endpoints]
end

def untraced_requests
Expand All @@ -232,24 +211,11 @@ def response_propagators
end

def allowed_request_headers
config[:allowed_request_headers]
.compact
.each_with_object({}) do |header, memo|
key = header.to_s.upcase.gsub(/[-\s]/, '_')
case key
when 'CONTENT_TYPE', 'CONTENT_LENGTH'
memo[key] = build_attribute_name('http.request.header.', header)
else
memo["HTTP_#{key}"] = build_attribute_name('http.request.header.', header)
end
end
config[:allowed_rack_request_headers]
end

def allowed_response_headers
config[:allowed_response_headers].each_with_object({}) do |header, memo|
memo[header] = build_attribute_name('http.response.header.', header)
memo[header.to_s.upcase] = build_attribute_name('http.response.header.', header)
end
config[:allowed_rack_response_headers]
end

def tracer
Expand All @@ -259,6 +225,27 @@ def tracer
def config
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.config
end

def register_current_span(span)
ctx = OpenTelemetry::Trace.context_with_span(span)
rack_ctx = OpenTelemetry::Instrumentation::Rack.context_with_span(span, parent_context: ctx)

contexts = [ctx, rack_ctx]
contexts.compact!
contexts.map { |context| OpenTelemetry::Context.attach(context) }
end

def create_span(parent_context, request)
span = tracer.start_span(
create_request_span_name(request),
with_parent: parent_context,
kind: :server,
attributes: request_span_attributes(request.env)
)
request_start_time = OpenTelemetry::Instrumentation::Rack::Util::QueueTime.get_request_start(request.env)
span.add_event('http.proxy.request.started', timestamp: request_start_time) unless request_start_time.nil?
span
end
end
end
end
Expand Down