-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f46d168
commit 3ceabc9
Showing
12 changed files
with
356 additions
and
6 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
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,126 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'event' | ||
require_relative 'http/transport' | ||
require_relative '../utils/sequence' | ||
require_relative '../utils/forking' | ||
|
||
module Datadog | ||
module Core | ||
module Telemetry | ||
module Metric | ||
# Base class for Metric | ||
class Base | ||
class << self | ||
def request_type | ||
raise NotImplementedError | ||
end | ||
end | ||
|
||
attr_reader :values | ||
|
||
def initialize(name, tags) | ||
@name = name | ||
@tags = tags | ||
@values = nil | ||
end | ||
|
||
def update_value(value) | ||
raise NotImplementedError | ||
end | ||
|
||
def metric_type | ||
raise NotImplementedError | ||
end | ||
|
||
def to_h | ||
{ | ||
tags: @tags, | ||
type: metric_type, | ||
common: true, | ||
} | ||
end | ||
end | ||
|
||
# GenerateMetricType | ||
class GenerateMetricType < Base | ||
class << self | ||
def request_type | ||
'generate-metrics' | ||
end | ||
end | ||
end | ||
|
||
# DistributionsMetricType | ||
class DistributionsMetricType < Base | ||
class << self | ||
def request_type | ||
'distributions' | ||
end | ||
end | ||
end | ||
|
||
# Count metric sup all the submitted values in a time interval | ||
class Count < GenerateMetricType | ||
def update_value(value) | ||
if @values | ||
@values[0][1] += value | ||
else | ||
@values = [[Time.now, value]] | ||
end | ||
end | ||
|
||
def metric_type | ||
'count' | ||
end | ||
end | ||
|
||
# Rate metric type takes the count and divides it by the length of the time interval. This is useful if you’re | ||
# interested in the number of hits per second. | ||
class Rate < GenerateMetricType | ||
class << self | ||
attr_accessor :interval | ||
end | ||
|
||
def initialize(name, tags) | ||
super(name, tags) | ||
@count = 0.0 | ||
end | ||
|
||
def update_value(value) | ||
@count += value | ||
rate = self.class.interval ? (@count / self.class.interval) : 0.0 | ||
@values = [[Time.now, rate]] | ||
end | ||
|
||
def metric_type | ||
'rate' | ||
end | ||
end | ||
|
||
# Gauge metric takes the last value reported during the interval. | ||
class Gauge < GenerateMetricType | ||
def update_value(value) | ||
@values = [[Time.now, value]] | ||
end | ||
|
||
def metric_type | ||
'gauge' | ||
end | ||
end | ||
|
||
# Distribution metric are a metric type that aggregate values during the interval. | ||
class Distribution < DistributionsMetricType | ||
def update_value(value) | ||
@values ||= [] | ||
@values << value | ||
end | ||
|
||
def metric_type | ||
'distributions' | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module Core | ||
module Telemetry | ||
# Stores all the metrics information by request_type during a time interval | ||
class MetricQueue | ||
def initialize | ||
@metrics = { | ||
'generate-metrics' => {}, | ||
'distributions' => {}, | ||
} | ||
end | ||
|
||
def add_metric(namespace, name, value, tags, metric_klass) | ||
namespace_space = @metrics[metric_klass.request_type][namespace] ||= {} | ||
existing_metric = namespace_space[name] | ||
|
||
if existing_metric | ||
existing_metric.update_value(value) | ||
@metrics[metric_klass.request_type][namespace][name] = existing_metric | ||
else | ||
new_metric = metric_klass.new(name, tags) | ||
new_metric.update_value(value) | ||
@metrics[metric_klass.request_type][namespace][name] = new_metric | ||
end | ||
end | ||
|
||
def build_metrics_payload | ||
@metrics.each do |metric_type, namespace| | ||
next unless namespace | ||
|
||
payload = { | ||
namespace: namespace | ||
} | ||
namespace.each do |_namespace_key, metrics| | ||
payload[:series] = [] | ||
series = payload[:series] | ||
|
||
metrics.each do |metric_name, metric| | ||
series << { | ||
metric: metric_name, | ||
points: metric.values, | ||
**metric.to_h | ||
} | ||
end | ||
|
||
yield metric_type, payload | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../worker' | ||
require_relative '../workers/polling' | ||
|
||
module Datadog | ||
module Core | ||
module Telemetry | ||
# Periodically (every DEFAULT_INTERVAL_SECONDS) sends metrics to the telemetry API. | ||
class MetricWorker < Core::Worker | ||
include Core::Workers::Polling | ||
|
||
def initialize(heartbeat_interval_seconds:, enabled: true, &block) | ||
# Workers::Polling settings | ||
self.enabled = enabled | ||
# Workers::IntervalLoop settings | ||
self.loop_base_interval = heartbeat_interval_seconds | ||
self.fork_policy = Core::Workers::Async::Thread::FORK_POLICY_STOP | ||
super(&block) | ||
start | ||
end | ||
|
||
def loop_wait_before_first_iteration? | ||
true | ||
end | ||
|
||
private | ||
|
||
def start | ||
perform | ||
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
Oops, something went wrong.