-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/improve_appsignal_to_support_tagging
- Loading branch information
Showing
8 changed files
with
334 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path("../lib", __FILE__) | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require "event_tracer/version" | ||
require 'event_tracer/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "event_tracer" | ||
spec.name = 'event_tracer' | ||
spec.version = EventTracer::VERSION | ||
spec.authors = ["melvrickgoh"] | ||
spec.email = ["[email protected]"] | ||
spec.authors = ['melvrickgoh'] | ||
spec.email = ['[email protected]'] | ||
|
||
spec.summary = %q{Thin wrapper for formatted logging/ metric services to be used as a single service} | ||
spec.description = %q{Thin wrapper for formatted logging/ metric services to be used as a single service. External service(s) supported: Appsignal} | ||
spec.homepage = "https://github.com/melvrickgoh/event_tracer" | ||
spec.license = "MIT" | ||
spec.summary = 'Thin wrapper for formatted logging/ metric services to be used as a single service' | ||
spec.description = 'Thin wrapper for formatted logging/ metric services to be used as a single service. External service(s) supported: Appsignal' | ||
spec.homepage = 'https://github.com/melvrickgoh/event_tracer' | ||
spec.license = 'MIT' | ||
|
||
spec.files = Dir['lib/**/*.rb'] | ||
spec.bindir = "exe" | ||
spec.bindir = 'exe' | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib/event_tracer", "lib"] | ||
spec.require_paths = %w[lib/event_tracer lib] | ||
|
||
spec.add_development_dependency "bundler", "~> 2.1.4" | ||
spec.add_development_dependency "rake", "~> 10.0" | ||
|
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,75 @@ | ||
require_relative '../event_tracer' | ||
require_relative './basic_decorator' | ||
# NOTES | ||
# Datadog interface to send our usual actions | ||
# BasicDecorator adds a transparent interface on top of the datadog interface | ||
# | ||
# Usage: EventTracer.register :datadog, EventTracer::DataDogLogger.new(DataDog) | ||
# data_dog_logger.info datadog: { count: { counter_1: 1, counter_2: 2 }, set: { gauge_1: 1 } } | ||
# data_dog_logger.info datadog: { count: { counter_1: { value: 1, tags: ['tag1, tag2']} } } | ||
|
||
module EventTracer | ||
class DatadogLogger < BasicDecorator | ||
|
||
class InvalidTagError < StandardError; end | ||
|
||
SUPPORTED_METRICS ||= %i[count set distribution gauge histogram].freeze | ||
|
||
LOG_TYPES.each do |log_type| | ||
define_method log_type do |**args| | ||
return LogResult.new(false, 'Invalid datadog config') unless args[:datadog]&.is_a?(Hash) | ||
|
||
applied_metrics(args[:datadog]).each do |metric| | ||
metric_args = args[:datadog][metric] | ||
return LogResult.new(false, "Datadog metric #{metric} invalid") unless metric_args.is_a?(Hash) | ||
|
||
send_metric metric, metric_args | ||
end | ||
|
||
LogResult.new(true) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :datadog, :decoratee | ||
alias_method :datadog, :decoratee | ||
|
||
def applied_metrics(datadog_args) | ||
datadog_args.keys.select { |metric| SUPPORTED_METRICS.include?(metric) } | ||
end | ||
|
||
def send_metric(metric, payload) | ||
payload.each do |increment, attribute| | ||
if attribute.is_a?(Hash) | ||
begin | ||
datadog.public_send( | ||
metric, | ||
increment, | ||
attribute.fetch(:value), | ||
build_options(attribute[:tags]) | ||
) | ||
rescue KeyError | ||
raise InvalidTagError, "Datadog payload { #{increment}: #{attribute} } invalid" | ||
end | ||
else | ||
datadog.public_send(metric, increment, attribute) | ||
end | ||
end | ||
end | ||
|
||
def build_options(tags) | ||
return {} unless tags | ||
|
||
formattted_tags = | ||
if tags.is_a?(Array) | ||
tags | ||
else | ||
tags.inject([]) do |acc, (tag, value)| | ||
acc << "#{tag}:#{value}" | ||
end | ||
end | ||
{ tags: formattted_tags } | ||
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module EventTracer | ||
VERSION = "0.1.3" | ||
VERSION = '0.2.0'.freeze | ||
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,21 @@ | ||
class MockDatadog < Struct.new(:_) | ||
def increment(*_args) | ||
'increment' | ||
end | ||
|
||
def distribution(*_args) | ||
'distribution' | ||
end | ||
|
||
def set(*_args) | ||
'set' | ||
end | ||
|
||
def gauge(*_args) | ||
'gauge' | ||
end | ||
|
||
def histogram(*_args) | ||
'histogram' | ||
end | ||
end |
Oops, something went wrong.