Class: Couchbase::Tracing::ThresholdLoggingTracer

Inherits:
RequestTracer
  • Object
show all
Defined in:
lib/couchbase/tracing/threshold_logging_tracer.rb,
/home/runner/work/couchbase-ruby-client/couchbase-ruby-client/lib/couchbase/tracing/threshold_logging_tracer.rb

Defined Under Namespace

Classes: Group, Item

Instance Method Summary collapse

Constructor Details

#initialize(emit_interval: nil, kv_threshold: nil, query_threshold: nil, views_threshold: nil, search_threshold: nil, analytics_threshold: nil, management_threshold: nil, sample_size: nil) ⇒ ThresholdLoggingTracer

milliseconds

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/couchbase/tracing/threshold_logging_tracer.rb', line 40

def initialize(
  emit_interval: nil,
  kv_threshold: nil,
  query_threshold: nil,
  views_threshold: nil,
  search_threshold: nil,
  analytics_threshold: nil,
  management_threshold: nil,
  sample_size: nil
)
  super()

  emit_interval = DEFAULT_EMIT_INTERVAL if emit_interval.nil?
  kv_threshold = DEFAULT_KV_THRESHOLD if kv_threshold.nil?
  query_threshold = DEFAULT_HTTP_THRESHOLD if query_threshold.nil?
  views_threshold = DEFAULT_HTTP_THRESHOLD if views_threshold.nil?
  search_threshold = DEFAULT_HTTP_THRESHOLD if search_threshold.nil?
  analytics_threshold = DEFAULT_HTTP_THRESHOLD if analytics_threshold.nil?
  management_threshold = DEFAULT_HTTP_THRESHOLD if management_threshold.nil?
  sample_size = DEFAULT_SAMPLE_SIZE if sample_size.nil?

  raise ArgumentError, "The sample size for ThresholdLoggingTracer must be positive" unless sample_size.positive?

  @emit_interval = emit_interval
  @sample_size = sample_size
  @groups = {
    Observability::ATTR_VALUE_SERVICE_KV => Group.new(floor_us: 1000 * kv_threshold, capacity: @sample_size),
    Observability::ATTR_VALUE_SERVICE_QUERY => Group.new(floor_us: 1000 * query_threshold, capacity: @sample_size),
    Observability::ATTR_VALUE_SERVICE_VIEWS => Group.new(floor_us: 1000 * views_threshold, capacity: @sample_size),
    Observability::ATTR_VALUE_SERVICE_SEARCH => Group.new(floor_us: 1000 * search_threshold, capacity: @sample_size),
    Observability::ATTR_VALUE_SERVICE_ANALYTICS => Group.new(floor_us: 1000 * analytics_threshold, capacity: @sample_size),
    Observability::ATTR_VALUE_SERVICE_MANAGEMENT => Group.new(floor_us: 1000 * management_threshold, capacity: @sample_size),
  }

  # TODO(DC): Find better solution for logging
  @logger = Couchbase.logger || Logger.new($stdout, Utils::StdlibLoggerAdapter.map_spdlog_level(Couchbase.log_level))
  start_reporting_thread
end

Instance Method Details

#closeObject



95
96
97
# File 'lib/couchbase/tracing/threshold_logging_tracer.rb', line 95

def close
  @thread.exit
end

#create_reportObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/couchbase/tracing/threshold_logging_tracer.rb', line 99

def create_report
  data = {}
  @groups.each do |service, group|
    group_data = group.steal_data
    next if group_data.nil?

    data[service] = group_data
  end
  data
end

#record_operation(service, item) ⇒ Object



88
89
90
91
92
93
# File 'lib/couchbase/tracing/threshold_logging_tracer.rb', line 88

def record_operation(service, item)
  group = @groups[service]
  return if group.nil?

  group.record_operation(item)
end

#request_span(name, parent: nil, start_timestamp: nil) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/couchbase/tracing/threshold_logging_tracer.rb', line 79

def request_span(name, parent: nil, start_timestamp: nil)
  ThresholdLoggingSpan.new(
    name,
    start_timestamp: start_timestamp,
    parent: parent,
    tracer: self,
  )
end

#start_reporting_threadObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/couchbase/tracing/threshold_logging_tracer.rb', line 110

def start_reporting_thread
  @thread = Thread.new do # rubocop:disable ThreadSafety/NewThread
    loop do
      sleep(@emit_interval / 1_000.0)
      report = create_report

      next if report.empty?

      begin
        @logger.info("Threshold Logging Report: #{report.to_json}")
      rescue StandardError => e
        @logger.debug("Failed to log threshold logging report: #{e.message}")
      end
    end
  end
end