Class: NewRelic::TelemetrySdk::Client

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/newrelic/telemetry_sdk/clients/client.rb

Overview

This class is a parent class for clients used to send data to the New Relic data ingest endpoints over HTTP (e.g. TraceClient for span data). Clients will automatically resend data if a recoverable error occurs. They will also automatically handle connection issues and New Relic errors.

Direct Known Subclasses

TraceClient

Instance Method Summary collapse

Methods included from Logger

#clear_already_logged, #log_error, #log_once, logger, #logger, logger=, #logger=

Constructor Details

#initialize(host:, path:, headers: {}, use_gzip: true, payload_type:) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/newrelic/telemetry_sdk/clients/client.rb', line 17

def initialize host:,
               path:,
               headers: {},
               use_gzip: true,
               payload_type:
  @connection = set_up_connection host
  @path = path
  @headers = headers
  @gzip_request = use_gzip
  @payload_type = payload_type
  @user_agent_products = nil
  add_user_agent_header @headers
  add_content_encoding_header @headers if @gzip_request
  @connection_attempts = 0
end

Instance Method Details

#add_user_agent_product(product, version = nil) ⇒ Object

Allows creators of exporters and other product built on this SDK to provide information about their product for analytic purposes. It may be called multiple times and is idempotent.

Both product and version must conform to RFC 7230.

Parameters:

  • product (String)

    The name of the exporter or other product, e.g. NewRelic-Ruby-OpenTelemetry.

  • version (optional, String) (defaults to: nil)

    The version number of the exporter or other product.

See Also:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/newrelic/telemetry_sdk/clients/client.rb', line 83

def add_user_agent_product product, version=nil
  # The product token must be valid to add to the headers
  if product !~ RFC7230_TOKEN
    log_once :warn, "Product is not a valid RFC 7230 token"
    return
  end

  # The version is ignored if invalid
  if version && version !~ RFC7230_TOKEN
    log_once :warn, "Product version is not a valid RFC 7230 token"
    version = nil
  end

  entry = [product, version].compact.join("/")

  # adds the product entry and updates the combined user agent
  # header, ignoring duplicate product entries.
  @user_agent_products ||= []
  unless @user_agent_products.include? entry
    @user_agent_products << entry
    add_user_agent_header @headers
  end
rescue => e
  log_error "Encountered error adding user agent product", e
end

#report(item) ⇒ Object

Reports a single item to a New Relic data ingest endpoint.

Parameters:

  • item

    a single point of data to send to New Relic (e.g. a Span). The item should respond to the #to_h method to return a Hash which is then serialized and sent to the data ingest endpoint.



41
42
43
44
45
46
# File 'lib/newrelic/telemetry_sdk/clients/client.rb', line 41

def report item
  # Report a batch of one pre-transformed item with no common attributes
  report_batch [[item.to_h], nil]
rescue => e
  log_error "Encountered error reporting item in client. Dropping data: 1 point of data", e
end

#report_batch(batch_data) ⇒ Object

Reports a batch of one or more items to a New Relic data ingest endpoint.

Parameters:

  • batch_data (Array)

    a two-part array contianing a Array of Hashes paired with a Hash of common attributes.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/newrelic/telemetry_sdk/clients/client.rb', line 55

def report_batch batch_data
  # We need to generate a version 4 uuid that will
  # be used for each unique batch, including on retries.
  # If a batch is split due to a 413 response,
  # each smaller batch should have its own.

  data, common_attributes = batch_data

  @headers[:'x-request-id'] = SecureRandom.uuid

  post_body = format_payload data, common_attributes
  send_with_response_handling post_body, data, common_attributes
rescue => e
  log_error "Encountered error reporting batch in client. Dropping data: #{data.size} points of data", e
end