新文物遥测sdk
newrelic-telemetry-sdk的Python项目详细描述
newrelic-telemetry-sdk-python提供了一个Python库,用于发送范围和度量数据 使用pythonrequests库进入New Relic。
当前的sdk支持向new relic metric api发送维度度量,并跨越到newrelic trace api。
为什么这么酷?
新遗迹中的维度度量和痕迹不需要代理
遥测SDK试图提供帮助,因此您向New Relic发送遥测数据的工作可以以正确的方式轻松完成我们已经为您介绍了所有基础知识,因此您可以专注于编写与业务需求或兴趣直接相关的功能代码。
为什么要使用遥测sdk?
我们假设您(或您的客户)对在new relic中看到由您的工具、框架或代码生成的遥测数据感兴趣。你可以写一个出口商这样做请阅读下面的内容开始学习
安装newrelic遥测sdk
首先,必须安装newrelic-telemetry-sdk包。安装 通过PIP:
$ pip install newrelic-telemetry-sdk
如果失败,请从github页面下载库并使用:
$ python setup.py install
报告您的第一个跨度
跨度提供了一种简单的方法来计时代码的组件。 示例代码假定您设置了以下环境变量:
- NEW_RELIC_INSERT_KEY
importosimporttimefromnewrelic_telemetry_sdkimportSpan,SpanClientwithSpan(name='sleep')asspan:time.sleep(0.5)span_client=SpanClient(os.environ['NEW_RELIC_INSERT_KEY'])response=span_client.send(span)response.raise_for_status()print('Span sleep sent successfully!')
报告您的第一个指标
有三种不同类型的指标:
- 计量
- 计数法
- 总和度量
度量描述
Metric Type | Interval Required | Description | Example |
---|---|---|---|
Gauge | No | A single value at a single point in time. | Room Temperature. |
Count | Yes | Track the total number of occurrences of an event. | Number of errors that have occurred. |
Summary | Yes | Track count, sum, min, and max values over time. | The summarized duration of 100 HTTP requests. |
示例
示例代码假定您设置了以下环境变量:
- NEW_RELIC_INSERT_KEY
importosimporttimefromnewrelic_telemetry_sdkimportGaugeMetric,CountMetric,SummaryMetric,MetricClientmetric_client=MetricClient(os.environ['NEW_RELIC_INSERT_KEY'])temperature=GaugeMetric("temperature",78.6,{"units":"Farenheit"})# Record that there have been 5 errors in the last 2 secondserrors=CountMetric(name="errors",value=5,interval_ms=2000)# Record a summary of 10 response times over the last 2 secondssummary=SummaryMetric("responses",count=10,min=0.2,max=0.5,sum=4.7,interval_ms=2000)response=metric_client.send_batch((temperature,errors,summary))response.raise_for_status()print("Sent metrics successfully!")