Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ def span_name(endpoint)
end

def attributes_from_grape_endpoint(endpoint)
{
OpenTelemetry::SemanticConventions::Trace::CODE_NAMESPACE => endpoint.options[:for]&.instance_variable_get(:@base)&.to_s,
attributes = {
OpenTelemetry::SemanticConventions::Trace::HTTP_ROUTE => path(endpoint)
}
code_namespace = code_namespace(endpoint)
attributes[OpenTelemetry::SemanticConventions::Trace::CODE_NAMESPACE] = code_namespace if code_namespace
attributes
end

# ActiveSupport::Notifications will attach a `:exception_object` to the payload if there was
Expand All @@ -89,6 +91,14 @@ def request_method(endpoint)
endpoint.options[:method]&.first
end

def code_namespace(endpoint)
owner = endpoint.options[:for]
return unless owner

base = owner.instance_variable_get(:@base)
[owner.name, base&.to_s, owner.to_s].find { |value| value && !value.empty? }
end

def path(endpoint)
return '' unless endpoint.routes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ class BasicAPI < Grape::API
end
end

describe 'when a Grape::API::Instance endpoint receives a request' do
class InstanceAPI < Grape::API::Instance
format :json
get :hello do
{ message: 'Hello, world!' }
end
end

let(:app) { build_rack_app(InstanceAPI) }
let(:request_path) { '/hello' }
let(:expected_span_name) { 'GET /hello' }

before { app.get request_path }

it 'sets code.namespace from the endpoint class name' do
_(span.name).must_equal expected_span_name
_(span.attributes['code.namespace']).must_equal 'InstanceAPI'
_(span.attributes['http.route']).must_equal '/hello'
end
end

describe 'when an API endpoint with a route param receives a request' do
class RouteParamAPI < Grape::API
format :json
Expand Down
Loading