How to use Ruby's telemetry SDK to send gauge metric

The git repo does not show an example of how to do this and my attempts at using the NewRelic::TelemetrySdk::Client.report method have been unsuccessful. Can someone please give a simple code example?

Here is my attempt:

require 'newrelic/telemetry_sdk'

NewRelic::TelemetrySdk.configure do |config|
  config.api_insert_key = ENV["NEW_RELIC_INSERT_KEY"]
  config.audit_logging_enabled = true
end

client = NewRelic::TelemetrySdk::Client.new(
  host: "https://metric-api.newrelic.com",
  path: "/metric/v1",
  payload_type: "metrics"
)

data = {
  :name => "test.testy",
  :type => "gauge",
  :value => 2.8,
  :timestamp => (Time.now.to_f * 1000).to_i,
  :attributes => {"app.name": "app"}
}

client.report data

But i get a Bad Request error back.

The “equivalent” curl call does work however

curl -vvv -k -H "Content-Type: application/json" \
  -H "Api-Key: $NEW_RELIC_INSERT_KEY" \
  -X POST https://metric-api.newrelic.com/metric/v1 \
  --data '[{"metrics":[{"name":"test.curl","type":"gauge","value":2.8,"timestamp":1623182684000,"attributes":{"app.name":"app"}}]}]'

Figured it out. It would really help to add an example for this in the examples dir by the way.

the client needs to be configured with ALL the necessary headers:

client = NewRelic::TelemetrySdk::Client.new(
  host: "https://metric-api.newrelic.com",
  path: "/metric/v1",
  payload_type: "metrics",
  headers: {
    "Content-Type": "application/json",
    "Api-Key": ENV["NEW_RELIC_INSERT_KEY"]
  },
)

And the NewRelic::TelemetrySdk.configure block is not needed. Otherwise the code in the original post is fine.

Not sure why headers is an optional parameter (with an empty object as the default!) given that it’s absolutely necessary for the client to work…