Skip to content

Commit

Permalink
Added: #runtime_metrics_enabled flag to configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Apr 5, 2019
1 parent 0b04e8a commit ecea8e1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
7 changes: 6 additions & 1 deletion docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -1748,8 +1748,13 @@ require 'datadog/statsd'
require 'ddtrace'

Datadog.configure do |c|
# To enable runtime metrics collection, set `true`. Defaults to `false`
# You can also set DD_RUNTIME_METRICS_ENABLED=true to configure this.
c.runtime_metrics_enabled = true

# Optionally, you can configure the Statsd instance used for sending runtime metrics.
# Statsd is automatically configured with default settings if `dogstatsd-ruby` is available.
# Otherwise, you can configure with host and port of Datadog agent; defaults to 'localhost:8125'.
# You can configure with host and port of Datadog agent; defaults to 'localhost:8125'.
c.runtime_metrics statsd: Datadog::Statsd.new
end
```
Expand Down
5 changes: 5 additions & 0 deletions lib/ddtrace/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'ddtrace/ext/analytics'
require 'ddtrace/ext/runtime'
require 'ddtrace/configuration/options'

require 'ddtrace/environment'
Expand All @@ -16,6 +17,10 @@ class Settings
default: -> { env_to_bool(Ext::Analytics::ENV_TRACE_ANALYTICS_ENABLED, nil) },
lazy: true

option :runtime_metrics_enabled,
default: -> { env_to_bool(Ext::Runtime::Metrics::ENV_ENABLED, false) },
lazy: true

option :tracer, default: Tracer.new

def initialize(options = {})
Expand Down
2 changes: 2 additions & 0 deletions lib/ddtrace/ext/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module Runtime

# Metrics
module Metrics
ENV_ENABLED = 'DD_RUNTIME_METRICS_ENABLED'.freeze

METRIC_CLASS_COUNT = 'runtime.ruby.class_count'.freeze
METRIC_GC_PREFIX = 'runtime.ruby.gc'.freeze
METRIC_THREAD_COUNT = 'runtime.ruby.thread_count'.freeze
Expand Down
2 changes: 2 additions & 0 deletions lib/ddtrace/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def send_services(services, transport)
end

def send_runtime_metrics
return unless Datadog.configuration.runtime_metrics_enabled

runtime_metrics.flush
end

Expand Down
34 changes: 34 additions & 0 deletions spec/ddtrace/writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,40 @@
end
end

describe '#send_runtime_metrics' do
subject(:send_runtime_metrics) { writer.send_runtime_metrics }

context 'when runtime metrics are' do
context 'enabled' do
around do |example|
Datadog.configuration.runtime_metrics_enabled = Datadog.configuration.runtime_metrics_enabled.tap do
Datadog.configuration.runtime_metrics_enabled = true
example.run
end
end

it do
expect(writer.runtime_metrics).to receive(:flush)
send_runtime_metrics
end
end

context 'disabled' do
around do |example|
Datadog.configuration.runtime_metrics_enabled = Datadog.configuration.runtime_metrics_enabled.tap do
Datadog.configuration.runtime_metrics_enabled = false
example.run
end
end

it do
expect(writer.runtime_metrics).to_not receive(:flush)
send_runtime_metrics
end
end
end
end

describe '#sampling_updater' do
subject(:result) { writer.send(:sampling_updater, action, response, api) }
let(:options) { { priority_sampler: sampler } }
Expand Down

0 comments on commit ecea8e1

Please sign in to comment.