Class: ChargeBee::Telemetry::TelemetrySupport

Inherits:
Object
  • Object
show all
Defined in:
lib/chargebee/telemetry/telemetry_support.rb

Overview

Helpers for building standardized Chargebee telemetry context and results.

Class Method Summary collapse

Class Method Details

.build_request_end_span_attributes(http_status_code, error) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chargebee/telemetry/telemetry_support.rb', line 52

def build_request_end_span_attributes(http_status_code, error)
  attributes = {
    TelemetryAttributeKeys::HTTP_RESPONSE_STATUS_CODE => http_status_code,
  }

  if error
    if error.chargebee_api_error_type
      attributes[TelemetryAttributeKeys::ERROR_TYPE] = error.chargebee_api_error_type
      attributes[TelemetryAttributeKeys::CHARGEBEE_ERROR_TYPE] = error.chargebee_api_error_type
    end
    attributes[TelemetryAttributeKeys::CHARGEBEE_ERROR_CODE] = error.chargebee_error_code if error.chargebee_error_code
    attributes[TelemetryAttributeKeys::CHARGEBEE_ERROR_PARAM] = error.chargebee_error_param if error.chargebee_error_param
  end

  attributes
end

.build_request_header_span_attributes(request_headers) ⇒ Object

Promotes chargebee-* request headers to http.request.header.* attributes; excludes the chargebee-request-origin-* PII family.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chargebee/telemetry/telemetry_support.rb', line 35

def build_request_header_span_attributes(request_headers)
  attributes = {}
  return attributes if request_headers.nil?

  request_headers.each do |name, value|
    next if name.nil? || value.nil?

    lower_name = name.to_s.downcase
    next unless lower_name.start_with?(TelemetryAttributeKeys::CHARGEBEE_TELEMETRY_HEADER_PREFIX)
    next if lower_name.start_with?(TelemetryAttributeKeys::CHARGEBEE_TELEMETRY_HEADER_EXCLUDE_PREFIX)

    attributes["#{TelemetryAttributeKeys::HTTP_REQUEST_HEADER_ATTRIBUTE_PREFIX}#{lower_name}"] = value.to_s
  end

  attributes
end

.build_request_start_span_attributes(resource, operation, http_method, http_url, server_address, chargebee_site, chargebee_api_version, sdk_version, request_headers = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chargebee/telemetry/telemetry_support.rb', line 19

def build_request_start_span_attributes(resource, operation, http_method, http_url, server_address,
                                        chargebee_site, chargebee_api_version, sdk_version, request_headers = {})
  {
    TelemetryAttributeKeys::URL_FULL => http_url,
    TelemetryAttributeKeys::HTTP_REQUEST_METHOD => http_method,
    TelemetryAttributeKeys::SERVER_ADDRESS => server_address,
    TelemetryAttributeKeys::CHARGEBEE_SITE => chargebee_site,
    TelemetryAttributeKeys::CHARGEBEE_API_VERSION => chargebee_api_version,
    TelemetryAttributeKeys::CHARGEBEE_RESOURCE => resource,
    TelemetryAttributeKeys::CHARGEBEE_OPERATION => operation,
    TelemetryAttributeKeys::CHARGEBEE_SDK_NAME => TelemetryAttributeKeys::SDK_NAME,
    TelemetryAttributeKeys::CHARGEBEE_SDK_VERSION => sdk_version,
  }.merge(build_request_header_span_attributes(request_headers))
end

.build_request_telemetry_context(resource, operation, http_method, http_url, server_address, chargebee_site, chargebee_api_version, sdk_version, request_headers = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/chargebee/telemetry/telemetry_support.rb', line 69

def build_request_telemetry_context(resource, operation, http_method, http_url, server_address,
                                    chargebee_site, chargebee_api_version, sdk_version, request_headers = {})
  RequestTelemetryContext.new(
    span_name: build_span_name(resource, operation),
    resource: resource,
    operation: operation,
    http_method: http_method,
    http_url: http_url,
    server_address: server_address,
    chargebee_site: chargebee_site,
    chargebee_api_version: chargebee_api_version,
    sdk_name: TelemetryAttributeKeys::SDK_NAME,
    sdk_version: sdk_version,
    start_attributes: build_request_start_span_attributes(
      resource, operation, http_method, http_url, server_address, chargebee_site, chargebee_api_version, sdk_version, request_headers
    ),
  )
end

.build_request_telemetry_result(http_status_code, duration_ms, error) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/chargebee/telemetry/telemetry_support.rb', line 88

def build_request_telemetry_result(http_status_code, duration_ms, error)
  RequestTelemetryResult.new(
    http_status_code: http_status_code,
    duration_ms: duration_ms,
    error: error,
    end_attributes: build_request_end_span_attributes(http_status_code, error),
  )
end

.build_span_name(resource, operation) ⇒ Object



11
12
13
# File 'lib/chargebee/telemetry/telemetry_support.rb', line 11

def build_span_name(resource, operation)
  "#{TelemetryAttributeKeys::TELEMETRY_SPAN_NAME_PREFIX}.#{resource}.#{operation}"
end

.extract_http_status_code(err) ⇒ Object



115
116
117
118
119
# File 'lib/chargebee/telemetry/telemetry_support.rb', line 115

def extract_http_status_code(err)
  return err.http_status_code if err.is_a?(ChargeBee::APIError)

  nil
end

.extract_request_telemetry_error(err) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/chargebee/telemetry/telemetry_support.rb', line 97

def extract_request_telemetry_error(err)
  return nil if err.nil?

  message = err.message.to_s
  message = 'Chargebee API request failed' if message.empty?

  if err.is_a?(ChargeBee::APIError)
    return RequestTelemetryError.new(
      message: message,
      chargebee_error_code: err.api_error_code,
      chargebee_api_error_type: err.type.to_s,
      chargebee_error_param: err.param,
    )
  end

  RequestTelemetryError.new(message: message)
end

.resolve_chargebee_api_version(api_path) ⇒ Object



15
16
17
# File 'lib/chargebee/telemetry/telemetry_support.rb', line 15

def resolve_chargebee_api_version(api_path)
  api_path == '/api/v1' ? 'v1' : 'v2'
end