Skip to content

Commit

Permalink
Implement
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Aug 23, 2024
1 parent 55b082b commit 879abc9
Show file tree
Hide file tree
Showing 28 changed files with 186 additions and 162 deletions.
3 changes: 2 additions & 1 deletion lib/datadog/core/environment/cgroup.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative 'ext'
require_relative '../telemetry/logging'

module Datadog
module Core
Expand Down Expand Up @@ -33,10 +34,10 @@ def descriptors(process = 'self')
end
end
rescue StandardError => e
# TODO: add telemetry logs
Datadog.logger.error(
"Error while parsing cgroup. Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
)
Telemetry::Logging.report(e, description: 'Error while parsing cgroup')
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/datadog/core/environment/container.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative 'cgroup'
require_relative '../telemetry/logging'

module Datadog
module Core
Expand Down Expand Up @@ -35,6 +36,7 @@ def task_uid
descriptor.task_uid
end

# rubocop:disable Metrics/MethodLength
def descriptor
@descriptor ||= Descriptor.new.tap do |descriptor|
begin
Expand Down Expand Up @@ -78,14 +80,15 @@ def descriptor
break
end
rescue StandardError => e
# TODO: add telemetry logs
Datadog.logger.error(
"Error while parsing container info. Cause: #{e.class.name} #{e.message} " \
"Location: #{Array(e.backtrace).first}"
)
Telemetry::Logging.report(e, description: 'Error while parsing container info')
end
end
end
# rubocop:enable Metrics/MethodLength
end
end
end
Expand Down
12 changes: 7 additions & 5 deletions lib/datadog/core/metrics/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative '../utils/time'
require_relative '../utils/only_once'
require_relative '../telemetry/logging'
require_relative '../configuration/ext'

require_relative 'ext'
Expand Down Expand Up @@ -97,10 +98,10 @@ def count(stat, value = nil, options = nil, &block)

statsd.count(stat, value, metric_options(options))
rescue StandardError => e
# TODO: add telemetry logs
Datadog.logger.error(
"Failed to send count stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
)
Telemetry::Logging.report(e, description: 'Failed to send count stat')
end

def distribution(stat, value = nil, options = nil, &block)
Expand All @@ -111,10 +112,10 @@ def distribution(stat, value = nil, options = nil, &block)

statsd.distribution(stat, value, metric_options(options))
rescue StandardError => e
# TODO: add telemetry logs
Datadog.logger.error(
"Failed to send distribution stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
)
Telemetry::Logging.report(e, description: 'Failed to send distribution stat')
end

def increment(stat, options = nil)
Expand All @@ -124,10 +125,10 @@ def increment(stat, options = nil)

statsd.increment(stat, metric_options(options))
rescue StandardError => e
# TODO: add telemetry logs
Datadog.logger.error(
"Failed to send increment stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
)
Telemetry::Logging.report(e, description: 'Failed to send increment stat')
end

def gauge(stat, value = nil, options = nil, &block)
Expand All @@ -138,10 +139,10 @@ def gauge(stat, value = nil, options = nil, &block)

statsd.gauge(stat, value, metric_options(options))
rescue StandardError => e
# TODO: add telemetry logs
Datadog.logger.error(
"Failed to send gauge stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
)
Telemetry::Logging.report(e, description: 'Failed to send gauge stat')
end

def time(stat, options = nil)
Expand All @@ -157,10 +158,11 @@ def time(stat, options = nil)
distribution(stat, ((finished - start) * 1000), options)
end
rescue StandardError => e
# TODO: add telemetry logs
# TODO: Likely to be redundant, since `distribution` handles its own errors.
Datadog.logger.error(
"Failed to send time stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
)
Telemetry::Logging.report(e, description: 'Failed to send time stat')
end
end

Expand Down
3 changes: 3 additions & 0 deletions lib/datadog/core/telemetry/event.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require_relative '../utils/forking'
require_relative '../utils/sequence'

module Datadog
module Core
module Telemetry
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/core/telemetry/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Telemetry
module Logging
extend self

def report(exception, level:, description: nil)
def report(exception, level: :error, description: nil)
# Annoymous exceptions to be logged as <Class:0x00007f8b1c0b3b40>
message = +''
message << (exception.class.name || exception.class.inspect)
Expand Down
21 changes: 8 additions & 13 deletions lib/datadog/tracing/contrib/action_cable/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,17 @@ module Instrumentation
module ActionCableConnection
def on_open
Tracing.trace(Ext::SPAN_ON_OPEN) do |span, trace|
begin
span.resource = "#{self.class}#on_open"
span.type = Tracing::Metadata::Ext::AppTypes::TYPE_WEB
span.resource = "#{self.class}#on_open"
span.type = Tracing::Metadata::Ext::AppTypes::TYPE_WEB

span.set_tag(Ext::TAG_ACTION, 'on_open')
span.set_tag(Ext::TAG_CONNECTION, self.class.to_s)
span.set_tag(Ext::TAG_ACTION, 'on_open')
span.set_tag(Ext::TAG_CONNECTION, self.class.to_s)

span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_ON_OPEN)
span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_ON_OPEN)

# Set the resource name of the trace
trace.resource = span.resource
rescue StandardError => e
# TODO: Report Telemetry logs
Datadog.logger.error("Error preparing span for ActionCable::Connection: #{e}")
end
# Set the resource name of the trace
trace.resource = span.resource

super
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative '../../configuration/resolver'
require_relative 'makara_resolver'
require_relative '../../../../core/telemetry/logging'

module Datadog
module Tracing
Expand Down Expand Up @@ -73,11 +74,11 @@ def resolve(db_config)
# `db_config` input may contain sensitive information such as passwords,
# hence provide a succinct summary for the error logging.
#
# TODO: Report Telemetry logs
Datadog.logger.error(
'Failed to resolve ActiveRecord database configuration. '\
"Cause: #{e.class.name} Source: #{Array(e.backtrace).first}"
)
Core::Telemetry::Logging.report(e, description: 'Failed to resolve ActiveRecord database configuration')

nil
end
Expand All @@ -93,11 +94,11 @@ def parse_matcher(matcher)

normalized
rescue => e
# TODO: Report Telemetry logs
Datadog.logger.error(
"Failed to resolve key #{matcher.inspect}. " \
"Cause: #{e.class.name} Source: #{Array(e.backtrace).first}"
)
Core::Telemetry::Logging.report(e, description: 'Failed to resolve key')

nil
end
Expand Down
16 changes: 1 addition & 15 deletions lib/datadog/tracing/contrib/httpclient/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,14 @@ module Httpclient
module Patcher
include Contrib::Patcher

PATCH_ONLY_ONCE = Core::Utils::OnlyOnce.new

module_function

def patched?
PATCH_ONLY_ONCE.ran?
end

def target_version
Integration.version
end

# patch applies our patch
def patch
# TODO: Refactor to remove redundancy
PATCH_ONLY_ONCE.run do
begin
::HTTPClient.include(Instrumentation)
rescue StandardError => e
Datadog.logger.error("Unable to apply httpclient integration: #{e}")
end
end
::HTTPClient.include(Instrumentation)
end
end
end
Expand Down
16 changes: 1 addition & 15 deletions lib/datadog/tracing/contrib/httprb/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,14 @@ module Httprb
module Patcher
include Contrib::Patcher

PATCH_ONLY_ONCE = Core::Utils::OnlyOnce.new

module_function

def patched?
PATCH_ONLY_ONCE.ran?
end

def target_version
Integration.version
end

# patch applies our patch
def patch
# TODO: Refactor to remove redundancy
PATCH_ONLY_ONCE.run do
begin
::HTTP::Client.include(Instrumentation)
rescue StandardError => e
Datadog.logger.error("Unable to apply httprb integration: #{e}")
end
end
::HTTP::Client.include(Instrumentation)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/datadog/tracing/contrib/lograge/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def patch
if defined?(::ActiveSupport::TaggedLogging::Formatter) &&
::Lograge::LogSubscribers::ActionController
.logger&.formatter.is_a?(::ActiveSupport::TaggedLogging::Formatter)
# TODO: Change to warn
Datadog.logger.error(
Datadog.logger.warn(
'Lograge and ActiveSupport::TaggedLogging (the default Rails log formatter) are not compatible: ' \
'Lograge does not account for Rails log tags, creating polluted logs and breaking log formatting. ' \
'Traces and Logs correlation may not work. ' \
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/tracing/contrib/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative '../../core/utils/only_once'
require_relative '../../core/telemetry/logging'

module Datadog
module Tracing
Expand Down Expand Up @@ -49,9 +50,8 @@ def patch
# Processes patching errors. This default implementation logs the error and reports relevant metrics.
# @param e [Exception]
def on_patch_error(e)
# Log the error
# TODO: Report Telemetry logs
Datadog.logger.error("Failed to apply #{patch_name} patch. Cause: #{e} Location: #{Array(e.backtrace).first}")
Datadog::Core::Telemetry::Logging.report(e, description: "Failed to apply #{patch_name} patch")

@patch_error_result = {
type: e.class.name,
Expand Down
15 changes: 1 addition & 14 deletions lib/datadog/tracing/contrib/presto/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,10 @@ module Presto
module Patcher
include Contrib::Patcher

PATCH_ONLY_ONCE = Core::Utils::OnlyOnce.new

module_function

def patched?
PATCH_ONLY_ONCE.ran?
end

def patch
# TODO: Refactor to remove redundancy
PATCH_ONLY_ONCE.run do
begin
::Presto::Client::Client.include(Instrumentation::Client)
rescue StandardError => e
Datadog.logger.error("Unable to apply Presto integration: #{e}")
end
end
::Presto::Client::Client.include(Instrumentation::Client)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/datadog/tracing/sampling/rate_sampler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def initialize(sample_rate = 1.0, decision: nil)
super()

unless sample_rate >= 0.0 && sample_rate <= 1.0
# TODO: Change to warning
Datadog.logger.error('sample rate is not between 0 and 1, falling back to 1')
Datadog.logger.warn('sample rate is not between 0 and 1, falling back to 1')
sample_rate = 1.0
end

Expand Down
3 changes: 2 additions & 1 deletion lib/datadog/tracing/sampling/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative 'matcher'
require_relative 'rate_sampler'
require_relative '../../core/telemetry/logging'

module Datadog
module Tracing
Expand Down Expand Up @@ -32,10 +33,10 @@ def initialize(matcher, sampler, provenance)
def match?(trace)
@matcher.match?(trace)
rescue => e
# TODO: Report Telemetry logs
Datadog.logger.error(
"Matcher failed. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
)
Datadog::Core::Telemetry::Logging.report(e, description: 'Matcher failed')
nil
end

Expand Down
7 changes: 5 additions & 2 deletions lib/datadog/tracing/sampling/rule_sampler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative 'ext'
require_relative 'rate_limiter'
require_relative 'rule'
require_relative '../../core/telemetry/logging'

module Datadog
module Tracing
Expand Down Expand Up @@ -80,10 +81,10 @@ def self.parse(rules, rate_limit, default_sample_rate)

new(parsed_rules, rate_limit: rate_limit, default_sample_rate: default_sample_rate)
rescue => e
# TODO: Change to warning
Datadog.logger.error do
Datadog.logger.warn do
"Could not parse trace sampling rules '#{rules}': #{e.class.name} #{e.message} at #{Array(e.backtrace).first}"
end

nil
end

Expand Down Expand Up @@ -142,6 +143,8 @@ def sample_trace(trace)
Datadog.logger.error(
"Rule sampling failed. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
)
Datadog::Core::Telemetry::Logging.report(e, description: 'Rule sampling failed')

yield(trace)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/tracing/transport/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def send_request(request, &block)
if stats.consecutive_errors > 0
Datadog.logger.debug(message)
else
# Not to report Telemetry logs
# Not to report telemetry logs
Datadog.logger.error(message)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/tracing/transport/io/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def send_request(request)
if stats.consecutive_errors > 0
Datadog.logger.debug(message)
else
# Not to report Telemetry logs
# Not to report telemetry logs
Datadog.logger.error(message)
end

Expand Down
Loading

0 comments on commit 879abc9

Please sign in to comment.