Skip to content

Commit

Permalink
Refactor with Logger module
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Sep 5, 2024
1 parent a4d99a1 commit 7b61550
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 99 deletions.
2 changes: 0 additions & 2 deletions lib/datadog/appsec/processor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require_relative '../core/telemetry/logging'

module Datadog
module AppSec
# Processor integrates libddwaf into datadog/appsec
Expand Down
1 change: 0 additions & 1 deletion lib/datadog/appsec/processor/rule_loader.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative '../assets'
require_relative '../../core/telemetry/logging'

module Datadog
module AppSec
Expand Down
1 change: 0 additions & 1 deletion lib/datadog/appsec/processor/rule_merger.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative '../assets'
require_relative '../../core/telemetry/logging'

module Datadog
module AppSec
Expand Down
1 change: 0 additions & 1 deletion lib/datadog/core/configuration/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require_relative '../diagnostics/health'
require_relative '../logger'
require_relative '../runtime/metrics'
require_relative '../telemetry'
require_relative '../telemetry/component'
require_relative '../workers/runtime_metrics'

Expand Down
24 changes: 0 additions & 24 deletions lib/datadog/core/telemetry.rb

This file was deleted.

26 changes: 26 additions & 0 deletions lib/datadog/core/telemetry/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Datadog
module Core
module Telemetry
# Module for sending telemetry logs to the global telemetry instance
module Logger
class << self
def report(exception, level: :error, description: nil)
instance&.report(exception, level: level, description: description)
end

def error(description)
instance&.error(description)
end

private

def instance
Datadog.send(:components).telemetry
end
end
end
end
end
end
1 change: 0 additions & 1 deletion lib/datadog/core/telemetry/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ module Telemetry
# - What information needed to make it actionable?
#
module Logging

# Extract datadog stack trace from the exception
module DatadogStackTrace
GEM_ROOT = Pathname.new("#{__dir__}/../../../..").cleanpath.to_s
Expand Down
6 changes: 3 additions & 3 deletions lib/datadog/profiling/http_transport.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative "../core/transport/ext"
require_relative "../core/telemetry/logging"
require_relative "../core/telemetry/logger"

module Datadog
module Profiling
Expand Down Expand Up @@ -61,14 +61,14 @@ def export(flush)
"Failed to report profiling data (#{config_without_api_key}): " \
"server returned unexpected HTTP #{result} status code"
)
Datadog::Core::Telemetry::Logging.error(
Datadog::Core::Telemetry::Logger.error(
"Failed to report profiling data: unexpected HTTP #{result} status code"
)
false
end
else
Datadog.logger.error("Failed to report profiling data (#{config_without_api_key}): #{result}")
Datadog::Core::Telemetry::Logging.error("Failed to report profiling data")
Datadog::Core::Telemetry::Logger.error("Failed to report profiling data")
false
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/profiling/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require_relative "../core/worker"
require_relative "../core/workers/polling"
require_relative "../core/telemetry/logging"
require_relative "../core/telemetry/logger"

module Datadog
module Profiling
Expand Down Expand Up @@ -135,7 +135,7 @@ def flush_events
Datadog.logger.error(
"Unable to report profile. Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
)
Datadog::Core::Telemetry::Logging.report(e, level: :error, description: "Unable to report profile")
Datadog::Core::Telemetry::Logger.report(e, level: :error, description: "Unable to report profile")
end

true
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/profiling/stack_recorder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "../core/telemetry/logging"
require_relative "../core/telemetry/logger"

module Datadog
module Profiling
Expand Down Expand Up @@ -44,7 +44,7 @@ def serialize
error_message = result

Datadog.logger.error("Failed to serialize profiling data: #{error_message}")
Datadog::Core::Telemetry::Logging.error("Failed to serialize profiling data")
Datadog::Core::Telemetry::Logger.error("Failed to serialize profiling data")

nil
end
Expand Down
6 changes: 5 additions & 1 deletion sig/datadog/appsec.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ module Datadog

def self.processor: () -> Datadog::AppSec::Processor?

def self.reconfigure: (ruleset: ::Hash[untyped, untyped], actions: Array[Hash[String, untyped]]) -> void
def self.reconfigure: (
ruleset: ::Hash[untyped, untyped],
actions: Array[Hash[String, untyped]],
telemetry: Datadog::Core::Telemetry::Component
) -> void

def self.reconfigure_lock: () { () -> void } -> void

Expand Down
6 changes: 5 additions & 1 deletion sig/datadog/appsec/component.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ module Datadog

def initialize: (processor: Datadog::AppSec::Processor?) -> void

def self.reconfigure: (ruleset: ::Hash[untyped, untyped], actions: Array[Hash[String, untyped]]) -> void
def self.reconfigure: (
ruleset: ::Hash[untyped, untyped],
actions: Array[Hash[String, untyped]],
telemetry: Datadog::Core::Telemetry::Component
) -> void

def self.reconfigure_lock: () { () -> void } -> void

Expand Down
4 changes: 2 additions & 2 deletions sig/datadog/core/telemetry/component.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Datadog

attr_reader enabled: bool

include Core::Telemetry::Logging
include Core::Telemetry::_Logging
include Core::Utils::Forking

def self.build: (untyped settings, Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings agent_settings, Datadog::Core::Logger logger) -> Component
Expand All @@ -25,8 +27,6 @@ module Datadog

def integrations_change!: () -> void

def log!: (Datadog::Core::Telemetry::Event::Log) -> void

def inc: (String namespace, String metric_name, Datadog::Core::Telemetry::Metric::input_value value, ?tags: Datadog::Core::Telemetry::Metric::tags_input, ?common: bool) -> void

def dec: (String namespace, String metric_name, Datadog::Core::Telemetry::Metric::input_value value, ?tags: Datadog::Core::Telemetry::Metric::tags_input, ?common: bool) -> void
Expand Down
15 changes: 15 additions & 0 deletions sig/datadog/core/telemetry/logger.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Datadog
module Core
module Telemetry
module Logger
def self.report: (Exception exception, level: Symbol, ?description: String?) -> void

def self.error: (String description) -> void

private

def self.instance: () -> Datadog::Core::Telemetry::Component?
end
end
end
end
12 changes: 5 additions & 7 deletions sig/datadog/core/telemetry/logging.rbs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
module Datadog
module Core
module Telemetry
module Logging
interface _Logging
def log!: (Datadog::Core::Telemetry::Event::Log) -> void
end

module Logging : _Logging
module DatadogStackTrace
GEM_ROOT: String

def self.from: (Exception exception) -> String?
end

extend Datadog::Core::Telemetry::Logging

def report: (Exception exception, level: Symbol, ?description: String?) -> void

def error: (String description) -> void

private

def telemetry_component: () -> Datadog::Core::Telemetry::Component
end
end
end
Expand Down
64 changes: 64 additions & 0 deletions spec/datadog/core/telemetry/logger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'spec_helper'

require 'datadog/core/telemetry/logger'

RSpec.describe Datadog::Core::Telemetry::Logger do
describe '.report' do
context 'when there is a telemetry component configured' do
it do
exception = StandardError.new
telemetry = instance_double(Datadog::Core::Telemetry::Component)
allow(Datadog.send(:components)).to receive(:telemetry).and_return(telemetry)
expect(telemetry).to receive(:report).with(exception, level: :error, description: 'Oops...')

expect do
described_class.report(exception, level: :error, description: 'Oops...')
end.not_to raise_error
end

context 'when only given an exception' do
it do
exception = StandardError.new
telemetry = instance_double(Datadog::Core::Telemetry::Component)
allow(Datadog.send(:components)).to receive(:telemetry).and_return(telemetry)
expect(telemetry).to receive(:report).with(exception, level: :error, description: nil)

expect do
described_class.report(exception)
end.not_to raise_error
end
end
end

context 'when there is no telemetry component configured' do
it do
exception = StandardError.new
allow(Datadog.send(:components)).to receive(:telemetry).and_return(nil)

expect do
described_class.report(exception, level: :error, description: 'Oops...')
end.not_to raise_error
end
end
end

describe '.error' do
context 'when there is a telemetry component configured' do
it do
telemetry = instance_double(Datadog::Core::Telemetry::Component)
allow(Datadog.send(:components)).to receive(:telemetry).and_return(telemetry)
expect(telemetry).to receive(:error).with('description')

expect { described_class.error('description') }.not_to raise_error
end
end

context 'when there is no telemetry component configured' do
it do
allow(Datadog.send(:components)).to receive(:telemetry).and_return(nil)

expect { described_class.error('description') }.not_to raise_error
end
end
end
end
Loading

0 comments on commit 7b61550

Please sign in to comment.