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
5 changes: 5 additions & 0 deletions .changeset/ruby-logs-telemetry-sdk-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-rails": patch
---

Stamp `telemetry.sdk.name = "posthog-ruby"` on forwarded logs so PostHog can attribute log volume to the Ruby SDK. Previously these records carried the OpenTelemetry SDK default (`opentelemetry`), so they could not be split out per-SDK the way the mobile SDKs are.
5 changes: 5 additions & 0 deletions posthog-rails/lib/posthog/rails/logs/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ def resource_attributes
# The posthog-rails name/version travel with each record via the
# instrumentation scope (see LoggerProvider#logger above).
{
# Identifies these records as the PostHog Ruby SDK so PostHog's usage
# report can attribute log volume to Ruby (mirrors posthog-ios/android/
# flutter/react-native). Overrides the OpenTelemetry SDK default of
# 'opentelemetry'; the posthog-rails integration name stays on the scope.
'telemetry.sdk.name' => 'posthog-ruby',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@turnipdabeets we should extract this to a const and reuse since its duplicated now
see

'$lib' => 'posthog-ruby',
'$lib_version' => PostHog::VERSION.to_s

'service.name' => service_name,
'deployment.environment' => ::Rails.env.to_s
}
Expand Down
15 changes: 14 additions & 1 deletion spec/posthog/rails/logs/setup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@

context 'when the OpenTelemetry gems are available' do
let(:exporter_args) { {} }
let(:resource_attrs) { {} }
let(:otel_logger) { double('otel_logger') }
let(:provider) { double('provider', add_log_record_processor: nil, logger: otel_logger) }

Expand All @@ -82,8 +83,12 @@
resource_class.define_singleton_method(:create) { |attrs| attrs }

provider_double = provider
captured_resource = resource_attrs
provider_class = Class.new
provider_class.define_singleton_method(:new) { |**| provider_double }
provider_class.define_singleton_method(:new) do |**kwargs|
captured_resource.merge!(kwargs[:resource] || {})
provider_double
end

captured = exporter_args
exporter_class = Class.new
Expand Down Expand Up @@ -111,6 +116,14 @@
expect(exporter_args[:headers]).to eq('Authorization' => 'Bearer phc_token')
end

it 'stamps telemetry.sdk.name so PostHog attributes the records to the Ruby SDK' do
described_class.remember_client_options(api_key: 'phc_token', host: 'https://us.i.posthog.com')

described_class.install

expect(resource_attrs['telemetry.sdk.name']).to eq('posthog-ruby')
end

it 'supports string-keyed init options and strips a trailing slash from the host' do
described_class.remember_client_options('api_key' => 'phc_token', 'host' => 'https://eu.i.posthog.com/')

Expand Down
Loading