New Relic Telemetry SDK

0.9.0 · active · verified Sat Apr 11

The `newrelic-telemetry-sdk` is a Python library for sending metrics, events, logs, and spans/traces directly to New Relic using HTTP APIs, without requiring the full New Relic agent. It provides clients for each telemetry type and supports Python 3.9 and newer. The library is actively maintained with regular releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates sending a batch of different metric types (Gauge, Count, Summary) to New Relic using the `MetricClient`. It requires a `NEW_RELIC_LICENSE_KEY` environment variable for authentication.

import os
import time
from newrelic_telemetry_sdk import GaugeMetric, CountMetric, SummaryMetric, MetricClient

# Ensure NEW_RELIC_LICENSE_KEY is set in your environment
# Example: export NEW_RELIC_LICENSE_KEY="YOUR_NEW_RELIC_LICENSE_KEY"
LICENSE_KEY = os.environ.get("NEW_RELIC_LICENSE_KEY", "")

if not LICENSE_KEY:
    print("Warning: NEW_RELIC_LICENSE_KEY environment variable not set. Metrics will not be sent.")
else:
    metric_client = MetricClient(LICENSE_KEY)

    # Example: GaugeMetric - a single value at a point in time
    temperature = GaugeMetric("room_temperature", 72.5, {"units": "Fahrenheit"})

    # Example: CountMetric - track occurrences over an interval
    errors_count = CountMetric(name="application_errors", value=3, interval_ms=5000)

    # Example: SummaryMetric - track count, min, max, sum over an interval
    response_times_summary = SummaryMetric(
        "http_response_time", count=10, min=50, max=200, sum=1200, interval_ms=5000
    )

    batch = [temperature, errors_count, response_times_summary]

    try:
        response = metric_client.send_batch(batch)
        response.raise_for_status()  # Raises HTTPError for bad responses (4xx or 5xx)
        print("Sent metrics successfully!")
    except Exception as e:
        print(f"Failed to send metrics: {e}")

view raw JSON →