Skip to content

Commit

Permalink
Add service tag to health metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed Jul 14, 2022
1 parent 1835642 commit 1a8ecc8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/datadog/core/configuration/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ module Configuration
class Components
class << self
def build_health_metrics(settings)
settings = settings.diagnostics.health_metrics
options = { enabled: settings.enabled }
options[:statsd] = settings.statsd unless settings.statsd.nil?
health_settings = settings.diagnostics.health_metrics

Core::Diagnostics::Health::Metrics.new(**options)
Core::Diagnostics::Health::Metrics.new(
service: settings.service,
enabled: health_settings.enabled,
statsd: health_settings.statsd
)
end

def build_logger(settings)
Expand Down
29 changes: 28 additions & 1 deletion lib/datadog/core/diagnostics/health.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: strict
# typed: true

require 'datadog/core/diagnostics/ext'
require 'datadog/core/metrics/client'
Expand All @@ -10,6 +10,13 @@ module Diagnostics
module Health
# Health metrics for diagnostics
class Metrics < Core::Metrics::Client
def initialize(service:, enabled: true, statsd: nil)
super(enabled: enabled, statsd: statsd)
@service = service

@tags = compile_tags!
end

count :api_errors, Ext::Health::Metrics::METRIC_API_ERRORS
count :api_requests, Ext::Health::Metrics::METRIC_API_REQUESTS
count :api_responses, Ext::Health::Metrics::METRIC_API_RESPONSES
Expand All @@ -30,6 +37,26 @@ class Metrics < Core::Metrics::Client
gauge :queue_max_length, Ext::Health::Metrics::METRIC_QUEUE_MAX_LENGTH
gauge :queue_spans, Ext::Health::Metrics::METRIC_QUEUE_SPANS
gauge :sampling_service_cache_length, Ext::Health::Metrics::METRIC_SAMPLING_SERVICE_CACHE_LENGTH

def default_metric_options
return super unless @tags

# Return dupes, so that the constant isn't modified,
# and defaults are unfrozen for mutation in Statsd.
super.tap do |options|
options[:tags] = options[:tags].dup
options[:tags] << @tags
end
end

private

# Cache tag strings to avoid recreating them on every flush operation.
def compile_tags!
return unless @service

"#{Environment::Ext::TAG_SERVICE}:#{@service}".freeze
end
end
end
end
Expand Down
20 changes: 19 additions & 1 deletion spec/datadog/core/configuration/components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@
context 'given settings' do
shared_examples_for 'new health metrics' do
let(:health_metrics) { instance_double(Datadog::Core::Diagnostics::Health::Metrics) }
let(:default_options) { { enabled: settings.diagnostics.health_metrics.enabled } }
let(:default_options) do
{
service: settings.service,
enabled: settings.diagnostics.health_metrics.enabled,
statsd: settings.diagnostics.health_metrics.statsd,
}
end
let(:options) { {} }

before do
Expand Down Expand Up @@ -138,6 +144,18 @@
let(:options) { { statsd: statsd } }
end
end

context 'with :service' do
let(:service) { double('service') }

before do
allow(settings).to receive(:service).and_return(service)
end

it_behaves_like 'new health metrics' do
let(:options) { { service: service } }
end
end
end
end

Expand Down
25 changes: 24 additions & 1 deletion spec/datadog/core/diagnostics/health_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
require 'spec_helper'
require 'ddtrace'
require 'datadog/core/diagnostics/health'
require 'datadog/statsd'

RSpec.describe Datadog::Core::Diagnostics::Health::Metrics do
subject(:health_metrics) { described_class.new }
subject(:health_metrics) { described_class.new(service: service, statsd: statsd) }
let(:service) { nil }
let(:statsd) { instance_double(Datadog::Statsd) }

shared_examples_for 'a health metric' do |type, name, stat|
subject(:health_metric) { health_metrics.send(name, *args, &block) }
Expand Down Expand Up @@ -43,4 +46,24 @@
it_behaves_like 'a health metric', :gauge, :queue_spans, Datadog::Core::Diagnostics::Ext::Health::Metrics::METRIC_QUEUE_SPANS
it_behaves_like 'a health metric', :gauge, :sampling_service_cache_length, Datadog::Core::Diagnostics::Ext::Health::Metrics::METRIC_SAMPLING_SERVICE_CACHE_LENGTH
# rubocop:enable Layout/LineLength

describe '.new' do
context 'with service' do
let(:service) { 'srv' }

it 'sets the statsd service tag' do
expect(statsd).to receive(:count).with(any_args, tags: array_including('service:srv'))
health_metrics.api_requests(1)
end
end

context 'with no service' do
let(:service) { nil }

it 'does not set the statsd service tag' do
expect(statsd).to_not receive(:count).with(any_args, tags: array_including(include('service:')))
health_metrics.api_requests(1)
end
end
end
end

0 comments on commit 1a8ecc8

Please sign in to comment.