Skip to content

Commit bee73bb

Browse files
committed
more clean ups, address all open comments
1 parent a13a5a7 commit bee73bb

File tree

7 files changed

+151
-155
lines changed

7 files changed

+151
-155
lines changed

docs/OpenTelemetry.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
| Type | Documentation | datadog version | Gem version support |
66
| ------------- | ---------------------------------------------------- | --------------- | ------------------- |
77
| Tracing | https://github.com/open-telemetry/opentelemetry-ruby | 1.9.0+ | >= 1.1.0 |
8-
| Metrics SDK | https://rubygems.org/gems/opentelemetry-metrics-sdk | 2.25.0+ | >= 0.8 |
9-
| OTLP Metrics Exporter | https://rubygems.org/gems/opentelemetry-exporter-otlp-metrics | 2.25.0+ | >= 0.4 |
8+
| Metrics SDK | https://rubygems.org/gems/opentelemetry-metrics-sdk | 2.23.0+ | >= 0.8 |
9+
| OTLP Metrics Exporter | https://rubygems.org/gems/opentelemetry-exporter-otlp-metrics | 2.23.0+ | >= 0.4 |
1010

1111
## Configuring OpenTelemetry Tracing
1212

@@ -48,7 +48,7 @@
4848

4949
## Configuring OpenTelemetry Metrics
5050

51-
1. Add required gems to your Gemfile:
51+
1. Add the required gems to your Gemfile:
5252

5353
```ruby
5454
gem 'datadog'
@@ -64,23 +64,21 @@
6464
# Set environment variable before initializing metrics support
6565
ENV['DD_METRICS_OTEL_ENABLED'] = 'true'
6666
require 'opentelemetry/sdk'
67-
require 'opentelemetry-metrics-sdk'
68-
require 'opentelemetry/exporter/otlp_metrics'
6967
require 'datadog/opentelemetry'
7068
71-
# IMPORTANT: Call Datadog.configure before OpenTelemetry::SDK.configure
72-
# and keep both in the same file to ensure proper initialization order.
7369
Datadog.configure do |c|
7470
# Configure Datadog settings here
7571
end
7672
77-
# Initialize OpenTelemetry SDK (required for metrics)
73+
# Call after Datadog.configure to initialize metrics.
74+
# Can be called multiple times to pick up configuration changes.
75+
# Requires: opentelemetry/exporter/otlp_metrics and opentelemetry/exporter/otlp_metrics
7876
OpenTelemetry::SDK.configure
7977
```
8078

8179
1. Use the [OpenTelemetry Metrics API](https://opentelemetry.io/docs/languages/ruby/instrumentation/#metrics) to create and record metrics.
8280

83-
**Note:** Call `Datadog.configure` before `OpenTelemetry::SDK.configure` and keep both in the same file. Configuration changes require calling `OpenTelemetry::SDK.configure` again to take effect.
81+
**Note:** Call `OpenTelemetry::SDK.configure` after `Datadog.configure` and call it again whenever Datadog configuration changes to update the meter provider.
8482

8583
**Configuration Options:**
8684

lib/datadog/core/configuration/settings.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,6 @@ def initialize(*_)
10321032
# Keep this extension here for now to keep things working.
10331033
extend Datadog::Tracing::Configuration::Settings
10341034

1035-
# OpenTelemetry Metrics settings
10361035
extend Datadog::OpenTelemetry::Configuration::Settings
10371036
end
10381037
# standard:enable Metrics/BlockLength

lib/datadog/core/runtime/metrics.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def gc_metrics
9797
def try_flush
9898
yield
9999
rescue => e
100-
Datadog.logger.warn("Error while sending runtime metric. Cause: #{e.class.name} #{e.class}: #{e}")
100+
Datadog.logger.warn("Error while sending runtime metric. Cause: #{e.class.name} #{e.message}")
101101
end
102102

103103
def default_metric_options

lib/datadog/opentelemetry/configuration/settings.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def self.normalize_protocol(env_var_name)
3434
end
3535

3636
def self.headers_parser(env_var_name)
37-
proc do |value|
37+
lambda do |value|
3838
return {} if value.nil? || value.empty?
3939

4040
headers = {}

lib/datadog/opentelemetry/metrics.rb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@
44

55
module Datadog
66
module OpenTelemetry
7-
module Metrics
7+
class Metrics
88
EXPORTER_NONE = 'none'
99

1010
def self.initialize!(components)
11+
new(components).configure_metrics_sdk
12+
true
13+
rescue => exc
14+
components.logger.error("Failed to initialize OpenTelemetry metrics: #{exc.class}: #{exc}: #{exc.backtrace.join("\n")}")
15+
false
16+
end
17+
18+
def initialize(components)
1119
@logger = components.logger
1220
@settings = components.settings
1321
@agent_host = components.agent_settings.hostname
1422
@agent_ssl = components.agent_settings.ssl
15-
16-
configure_metrics_sdk
17-
true
18-
rescue => exc
19-
@logger.error("Failed to initialize OpenTelemetry metrics: #{exc.class}: #{exc}: #{exc.backtrace.join("\n")}")
20-
false
2123
end
2224

23-
def self.configure_metrics_sdk
25+
def configure_metrics_sdk
2426
provider = ::OpenTelemetry.meter_provider
2527
provider.shutdown if provider.is_a?(::OpenTelemetry::SDK::Metrics::MeterProvider)
2628

@@ -36,7 +38,7 @@ def self.configure_metrics_sdk
3638
::OpenTelemetry.meter_provider = provider
3739
end
3840

39-
def self.create_resource
41+
def create_resource
4042
resource_attributes = {}
4143
resource_attributes['host.name'] = Datadog::Core::Environment::Socket.hostname if @settings.tracing.report_hostname
4244

@@ -50,32 +52,32 @@ def self.create_resource
5052
resource_attributes[otel_key] = value
5153
end
5254

53-
resource_attributes['service.name'] = @settings.service_without_fallback || resource_attributes['service.name'] || ''
55+
resource_attributes['service.name'] = @settings.service_without_fallback || resource_attributes['service.name'] || Datadog::Core::Environment::Ext::FALLBACK_SERVICE_NAME
5456
resource_attributes['deployment.environment'] = @settings.env if @settings.env
5557
resource_attributes['service.version'] = @settings.version if @settings.version
5658

5759
::OpenTelemetry::SDK::Resources::Resource.create(resource_attributes)
5860
end
5961

60-
def self.configure_metric_reader(provider)
62+
def configure_metric_reader(provider)
6163
exporter_name = @settings.opentelemetry.metrics.exporter
62-
return if exporter_name == Datadog::OpenTelemetry::Metrics::EXPORTER_NONE
64+
return if exporter_name == EXPORTER_NONE
6365

6466
configure_otlp_exporter(provider)
6567
rescue => e
6668
@logger.warn("Failed to configure OTLP metrics exporter: #{e.class}: #{e}")
6769
end
6870

69-
def self.resolve_metrics_endpoint
71+
def resolve_metrics_endpoint
7072
metrics_config = @settings.opentelemetry.metrics
7173
exporter_config = @settings.opentelemetry.exporter
7274

73-
return metrics_config.endpoint.to_s if metrics_config.endpoint
74-
return exporter_config.endpoint.to_s if exporter_config.endpoint
75+
return metrics_config.endpoint if metrics_config.endpoint
76+
return exporter_config.endpoint if exporter_config.endpoint
7577
"#{@agent_ssl ? "https" : "http"}://#{@agent_host}:4318/v1/metrics"
7678
end
7779

78-
def self.configure_otlp_exporter(provider)
80+
def configure_otlp_exporter(provider)
7981
require 'opentelemetry/exporter/otlp_metrics'
8082
require_relative 'sdk/metrics_exporter'
8183

@@ -102,7 +104,7 @@ def self.configure_otlp_exporter(provider)
102104
@logger.warn("Could not load OTLP metrics exporter: #{e.class}: #{e}")
103105
end
104106

105-
private_class_method :configure_metrics_sdk, :create_resource, :configure_metric_reader, :resolve_metrics_endpoint, :configure_otlp_exporter
107+
private :create_resource, :configure_metric_reader, :resolve_metrics_endpoint, :configure_otlp_exporter
106108
end
107109
end
108110
end
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
module Datadog
22
module OpenTelemetry
3-
module Metrics
3+
class Metrics
44
EXPORTER_NONE: ::String
55

66
def self.initialize!: (Core::Configuration::Components components) -> bool
77

8-
private
8+
def initialize: (Core::Configuration::Components components) -> void
9+
10+
def configure_metrics_sdk: () -> void
911

10-
def self.configure_metrics_sdk: () -> void
12+
private
1113

12-
def self.create_resource: () -> untyped
14+
def create_resource: () -> untyped
1315

14-
def self.configure_metric_reader: (untyped provider) -> void
16+
def configure_metric_reader: (untyped provider) -> void
1517

16-
def self.resolve_metrics_endpoint: () -> ::String
18+
def resolve_metrics_endpoint: () -> ::String
1719

18-
def self.configure_otlp_exporter: (untyped provider) -> void
20+
def configure_otlp_exporter: (untyped provider) -> void
1921
end
2022
end
2123
end

0 commit comments

Comments
 (0)