Skip to content

Commit

Permalink
Add Open Telemetry initializer for Rails
Browse files Browse the repository at this point in the history
This initializes Open Telemetry for Rails applications. This can be used
to send trace information to a collector. This functionality is only
enabled if the env var ENABLE_OPEN_TELEMETRY is set to "true".
  • Loading branch information
theseanything committed Jul 3, 2023
1 parent 6e15638 commit 8576638
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ end

`GovukError.configure` has the same options as the Sentry client, Raven. See [the Raven docs for all configuration options](https://docs.sentry.io/clients/ruby/config).

## Open Telemetry

To enable Open Telemetry instrumentation for Rails set the ENABLE_OPEN_TELEMETRY="true" environment variable.

## Prometheus monitoring

Expand Down
1 change: 1 addition & 0 deletions lib/govuk_app_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "govuk_app_config/govuk_error"
require "govuk_app_config/govuk_proxy/static_proxy"
require "govuk_app_config/govuk_healthcheck"
require "govuk_app_config/govuk_open_telemetry"
require "govuk_app_config/govuk_prometheus_exporter"

if defined?(Rails)
Expand Down
18 changes: 18 additions & 0 deletions lib/govuk_app_config/govuk_open_telemetry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module GovukOpenTelemetry
def self.should_configure?
ENV["ENABLE_OPEN_TELEMETRY"] == "true"
end

def self.configure(service_name)
return unless should_configure?

require "opentelemetry/sdk"
require "opentelemetry/exporter/otlp"
require "opentelemetry/instrumentation/all"

OpenTelemetry::SDK.configure do |config|
config.service_name = service_name
config.use_all # enables all instrumentation!
end
end
end
4 changes: 4 additions & 0 deletions lib/govuk_app_config/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Railtie < Rails::Railtie
end
end

initializer "govuk_app_config.configure_open_telemetry" do |app|
GovukOpenTelemetry.configure(app.class.module_parent_name.underscore)
end

config.before_initialize do
GovukLogging.configure if Rails.env.production?
end
Expand Down

0 comments on commit 8576638

Please sign in to comment.