-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored: Runtime::Metrics into Metrics class.
- Loading branch information
Showing
12 changed files
with
234 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
require 'ddtrace/ext/metrics' | ||
|
||
require 'set' | ||
require 'ddtrace/utils/time' | ||
require 'ddtrace/runtime/identity' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Datadog | ||
module Runtime | ||
# Retrieves garbage collection statistics | ||
module GC | ||
module_function | ||
|
||
def stat | ||
::GC.stat | ||
end | ||
|
||
def available? | ||
defined?(::GC) && ::GC.respond_to?(:stat) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,92 @@ | ||
require 'ddtrace/ext/runtime' | ||
|
||
require 'ddtrace/metrics' | ||
require 'ddtrace/runtime/class_count' | ||
require 'ddtrace/runtime/heap_size' | ||
require 'ddtrace/runtime/gc' | ||
require 'ddtrace/runtime/identity' | ||
require 'ddtrace/runtime/thread_count' | ||
|
||
module Datadog | ||
module Runtime | ||
# For generating runtime metrics | ||
module Metrics | ||
module_function | ||
class Metrics < Datadog::Metrics | ||
def initialize(*args) | ||
super | ||
|
||
# Initialize service list | ||
@services = Set.new | ||
@service_tags = nil | ||
end | ||
|
||
# Flush all runtime metrics to a Datadog::Metrics instance. | ||
def flush(metrics = Datadog.metrics) | ||
try_flush { metrics.gauge(Ext::Runtime::METRIC_CLASS_COUNT, ClassCount.value) if ClassCount.available? } | ||
try_flush { metrics.gauge(Ext::Runtime::METRIC_HEAP_SIZE, HeapSize.value) if HeapSize.available? } | ||
try_flush { metrics.gauge(Ext::Runtime::METRIC_THREAD_COUNT, ThreadCount.value) if ThreadCount.available? } | ||
def associate_with_span(span) | ||
return if span.nil? | ||
|
||
# Register service as associated with metrics | ||
register_service(span.service) unless span.service.nil? | ||
|
||
# Tag span with language and runtime ID for association with metrics | ||
span.set_metric(Ext::Runtime::TAG_LANG, Runtime::Identity.lang) | ||
span.set_metric(Ext::Runtime::TAG_RUNTIME_ID, Runtime::Identity.id) | ||
end | ||
|
||
# Associate service with runtime metrics | ||
def register_service(service) | ||
return if service.nil? | ||
|
||
service = service.to_s | ||
|
||
unless @services.include?(service) | ||
# Add service to list and update services tag | ||
services << service | ||
|
||
# Recompile the service tags | ||
compile_service_tags! | ||
end | ||
end | ||
|
||
# Flush all runtime metrics to Statsd client | ||
def flush | ||
try_flush { gauge(Ext::Runtime::Metrics::METRIC_CLASS_COUNT, ClassCount.value) if ClassCount.available? } | ||
try_flush { gauge(Ext::Runtime::Metrics::METRIC_THREAD_COUNT, ThreadCount.value) if ThreadCount.available? } | ||
try_flush { gc_metrics.each { |metric, value| gauge(metric, value) } if GC.available? } | ||
end | ||
|
||
def gc_metrics | ||
Hash[ | ||
GC.stat.map do |k, v| | ||
["#{Ext::Runtime::Metrics::METRIC_GC_PREFIX}.#{k}", v] | ||
end | ||
] | ||
end | ||
|
||
def try_flush | ||
yield | ||
rescue StandardError => e | ||
Datadog::Tracer.log.error("Error while sending runtime metric. Cause: #{e.message}") | ||
end | ||
|
||
def default_metric_options | ||
# 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 | ||
|
||
# Add services dynamically because they might change during runtime. | ||
options[:tags].concat(service_tags) unless service_tags.nil? | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader \ | ||
:service_tags, | ||
:services | ||
|
||
def compile_service_tags! | ||
@service_tags = services.to_a.collect do |service| | ||
"#{Ext::Runtime::Metrics::TAG_SERVICE}:#{service}".freeze | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
require 'ddtrace/runtime/gc' | ||
|
||
RSpec.describe Datadog::Runtime::GC do | ||
describe '::stat' do | ||
subject(:stat) { described_class.stat } | ||
it { is_expected.to be_a_kind_of(Hash) } | ||
end | ||
|
||
describe '::available?' do | ||
subject(:available?) { described_class.available? } | ||
it { is_expected.to be true } | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.