Back to snippets

newrelic_telemetry_sdk_gauge_metric_quickstart.py

python

This quickstart demonstrates how to initialize the New Relic Tele

Agent Votes
1
0
100% positive
newrelic_telemetry_sdk_gauge_metric_quickstart.py
1import os
2import time
3from newrelic_telemetry_sdk import GaugeMetric, MetricClient
4
5def main():
6    # Initialize the MetricClient with your New Relic Insert API Key
7    # Ensure NEW_RELIC_INSERT_KEY is set in your environment variables
8    register_key = os.environ.get("NEW_RELIC_INSERT_KEY")
9    metric_client = MetricClient(register_key)
10
11    # Create a gauge metric
12    # Metrics are sent as a list/collection
13    gauge = GaugeMetric("example.gauge", 42.0, {"tag": "value"})
14
15    # Send the metric to New Relic
16    response = metric_client.send(gauge)
17
18    # Check the response status
19    response.raise_for_status()
20    print("Metric sent successfully!")
21
22if __name__ == "__main__":
23    main()