Add OTel span attribute to deprecated endpoints (#40030)

This commit is contained in:
Juan Hernández
2026-08-03 17:17:31 +02:00
committed by GitHub
parent 72235a9d61
commit dc6b681f2d
2 changed files with 59 additions and 0 deletions

View File

@@ -11,6 +11,11 @@ module DeprecationConcern
before_action(**kwargs) do
response.headers['Deprecation'] = deprecation_timestamp
response.headers['Sunset'] = sunset if sunset
span = OpenTelemetry::Trace.current_span
next unless span&.recording?
span.set_attribute('app.endpoint.deprecated', true)
end
end
end

View File

@@ -0,0 +1,54 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe DeprecationConcern do
render_views
controller(ApplicationController) do
include DeprecationConcern # rubocop:disable RSpec/DescribedClass
deprecate_api '2026-08-03', sunset: '2026-10-01'
def deprecated
render plain: 'deprecated'
end
end
before do
routes.draw do
get 'deprecated' => 'anonymous#deprecated'
end
end
context 'with response headers' do
it 'adds Deprecation header' do
get :deprecated
expect(response.body).to eq('deprecated')
expect(response.headers['Deprecation']).to eq('@1785715200')
end
it 'adds Sunset header' do
get :deprecated
expect(response.headers['Sunset']).to eq('Thu, 01 Oct 2026 00:00:00 GMT')
end
end
context 'with OpenTelemetry traces' do
let(:span) { instance_double(OpenTelemetry::Trace::Span) }
before do
allow(OpenTelemetry::Trace).to receive(:current_span).and_return(span)
allow(span).to receive(:recording?).and_return(true)
allow(span).to receive(:set_attribute)
end
it 'adds a deprecation attribute request span' do
get :deprecated
expect(span).to have_received(:set_attribute).with('app.endpoint.deprecated', true)
end
end
end