Class: Couchbase::Observability::Handler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, op_name, parent_span, receiver, tracer, meter) ⇒ Handler

Returns a new instance of Handler.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/couchbase/utils/observability.rb', line 67

def initialize(backend, op_name, parent_span, receiver, tracer, meter)
  @tracer = tracer
  @meter = meter

  cluster_labels = backend.cluster_labels
  @cluster_name = cluster_labels[:cluster_name]
  @cluster_uuid = cluster_labels[:cluster_uuid]

  @op_span = create_span(op_name, parent_span)
  @meter_attributes = create_meter_attributes
  @start_time = Time.now
  add_operation_name(op_name)
  add_receiver_attributes(receiver)
end

Instance Attribute Details

#op_spanObject (readonly)

Returns the value of attribute op_span.



65
66
67
# File 'lib/couchbase/utils/observability.rb', line 65

def op_span
  @op_span
end

Instance Method Details

#add_bucket_name(name) ⇒ Object



117
118
119
120
# File 'lib/couchbase/utils/observability.rb', line 117

def add_bucket_name(name)
  @op_span.set_attribute(ATTR_BUCKET_NAME, name)
  @meter_attributes[ATTR_BUCKET_NAME] = name
end

#add_collection_name(name) ⇒ Object



127
128
129
130
# File 'lib/couchbase/utils/observability.rb', line 127

def add_collection_name(name)
  @op_span.set_attribute(ATTR_COLLECTION_NAME, name)
  @meter_attributes[ATTR_COLLECTION_NAME] = name
end

#add_durability_level(level) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/couchbase/utils/observability.rb', line 132

def add_durability_level(level)
  durability_str =
    case level
    when :majority
      ATTR_VALUE_DURABILITY_MAJORITY
    when :majority_and_persist_to_active
      ATTR_VALUE_DURABILITY_MAJORITY_AND_PERSIST_TO_ACTIVE
    when :persist_to_majority
      ATTR_VALUE_DURABILITY_PERSIST_TO_MAJORITY
    end
  @op_span.set_attribute(ATTR_DURABILITY, durability_str) unless durability_str.nil?
end

#add_error(error) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/couchbase/utils/observability.rb', line 153

def add_error(error)
  @op_span.status = :error
  @meter_attributes[ATTR_ERROR_TYPE] =
    if error.is_a?(Couchbase::Error::CouchbaseError) || error.is_a?(Couchbase::Error::InvalidArgument)
      error.class.name.split("::").last
    else
      "_OTHER"
    end
end

#add_operation_name(name) ⇒ Object



112
113
114
115
# File 'lib/couchbase/utils/observability.rb', line 112

def add_operation_name(name)
  @op_span.set_attribute(ATTR_OPERATION_NAME, name)
  @meter_attributes[ATTR_OPERATION_NAME] = name
end

#add_query_statement(statement, options) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/couchbase/utils/observability.rb', line 163

def add_query_statement(statement, options)
  pos_params = options.instance_variable_get(:@positional_parameters)
  named_params = options.instance_variable_get(:@named_parameters)

  # The statement attribute is added only if positional or named parameters are in use.
  return if (pos_params.nil? || pos_params.empty?) && (named_params.nil? || named_params.empty?)

  @op_span.set_attribute(ATTR_QUERY_STATEMENT, statement)
end

#add_retries(retries) ⇒ Object



145
146
147
# File 'lib/couchbase/utils/observability.rb', line 145

def add_retries(retries)
  @op_span.set_attribute(ATTR_RETRIES, retries.to_i)
end

#add_scope_name(name) ⇒ Object



122
123
124
125
# File 'lib/couchbase/utils/observability.rb', line 122

def add_scope_name(name)
  @op_span.set_attribute(ATTR_SCOPE_NAME, name)
  @meter_attributes[ATTR_SCOPE_NAME] = name
end

#add_service(service) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/couchbase/utils/observability.rb', line 92

def add_service(service)
  service_str =
    case service
    when :kv
      ATTR_VALUE_SERVICE_KV
    when :query
      ATTR_VALUE_SERVICE_QUERY
    when :analytics
      ATTR_VALUE_SERVICE_ANALYTICS
    when :search
      ATTR_VALUE_SERVICE_SEARCH
    when :management
      ATTR_VALUE_SERVICE_MANAGEMENT
    when :views
      ATTR_VALUE_SERVICE_VIEWS
    end
  @op_span.set_attribute(ATTR_SERVICE, service_str) unless service_str.nil?
  @meter_attributes[ATTR_SERVICE] = service_str unless service_str.nil?
end

#add_spans_from_backend(backend_spans) ⇒ Object



173
174
175
176
177
# File 'lib/couchbase/utils/observability.rb', line 173

def add_spans_from_backend(backend_spans)
  backend_spans.each do |backend_span|
    add_backend_span(backend_span, @op_span)
  end
end

#finishObject



179
180
181
182
183
# File 'lib/couchbase/utils/observability.rb', line 179

def finish
  @op_span.finish
  duration_us = ((Time.now - @start_time) * 1_000_000).round
  @meter.value_recorder(METER_NAME_OPERATION_DURATION, @meter_attributes).record_value(duration_us)
end

#set_successObject



149
150
151
# File 'lib/couchbase/utils/observability.rb', line 149

def set_success
  @op_span.status = :ok
end

#with_request_encoding_spanObject



82
83
84
85
86
87
88
89
90
# File 'lib/couchbase/utils/observability.rb', line 82

def with_request_encoding_span
  span = create_span(STEP_REQUEST_ENCODING, @op_span)
  begin
    res = yield
  ensure
    span.finish
  end
  res
end