波前python sdk
wavefront-sdk-python的Python项目详细描述
wavefront sdk python
wavefront by vmware sdk for python是一个核心库,用于通过代理或直接接收将度量、直方图和跟踪数据从python应用程序发送到wavefront。
要求和安装
支持Python2.7+和Python3.x。
pip install wavefront-sdk-python
设置波前发送器
您可以选择使用以下技术之一将度量、直方图或跟踪数据从应用程序发送到波前服务:
- 使用direct ingestion将数据直接发送到波前服务。这是最简单的快速起床和跑步的方法。
- 使用Wavefront proxy,然后将数据转发到波前服务。这是一个大规模部署的建议选择,它需要对互联网中断的恢复能力、对数据排队和过滤的控制等等。
实例化一个与您的选择相对应的对象:
- 选项1:Create a ^{
} 将数据直接发送到波前服务。 - 选项2:Create a ^{
} 将数据发送到波前代理。
选项1:创建wavefrontdirectclient
要创建一个WavefrontDirectClient
,需要用它需要的信息初始化它,以便将数据直接发送到波前。
步骤1。获取波前访问信息
收集以下访问信息:
- 确定波前实例的url。这是登录Wavefront时连接到的URL,通常类似于
https://mydomain.wavefront.com
。 - 在wavefront中,验证您是否具有直接数据接收权限,以及obtain an API token。
步骤2。初始化WavefrontDirectClient
通过提供在步骤1中获得的访问信息来初始化WavefrontDirectClient
。
您可以选择指定参数来优化以下摄取属性:
- 最大队列大小-波前发送器的内部缓冲区容量。任何超过此大小的数据都将被丢弃。
- 刷新间隔-用于将数据从波前发送器直接刷新到波前的间隔。
- 批量大小-在每个刷新间隔内发送到波前的数据量。
一起,批次大小和冲洗间隔控制波前发送器的最大理论吞吐量。您应该只覆盖默认值来设置更高的值。
fromwavefront_sdkimportWavefrontDirectClient# Create a sender with:# your Wavefront URL# a Wavefront API token that was created with direct ingestion permission# max queue size (in data points). Default: 50,000# batch size (in data points). Default: 10,000# flush interval (in seconds). Default: 1 secondwavefront_sender=WavefrontDirectClient(server="<SERVER_ADDR>",token="<TOKEN>",max_queue_size=50000,batch_size=10000,flush_interval_seconds=5)
选项2:创建wavefrontproxyclient
注意:在应用程序可以使用WavefrontProxyClient
之前,必须set up and start a Wavefront proxy。
要创建一个WavefrontProxyClient
,您可以用它向波前代理发送数据所需的信息来实例化它,包括:
- 将运行Wavefront代理的主机的名称。
- 要向其发送数据的一个或多个代理侦听端口。指定的端口取决于要发送的数据类型(度量、直方图和/或跟踪数据)。必须至少指定一个侦听器端口。
- 用于优化与代理的通信的可选设置。
fromwavefront_sdkimportWavefrontProxyClient# Create a sender with:# the proxy hostname or address# the default listener port (2878) for sending metrics to# the recommended listener port (2878) for sending histograms to# the recommended listener port (30000) for sending trace data to# a nondefault interval (2 seconds) for flushing data from the sender to the proxy. Default: 5 secondswavefront_sender=WavefrontProxyClient(host="<PROXY_HOST>",metrics_port=2878,distribution_port=2878,tracing_port=30000,internal_flush=2)
注意:当您在指定的代理主机上set up a Wavefront proxy时,您将为要发送的每种类型的数据指定它将侦听的端口。WavefrontProxyClient
必须将数据发送到wavefront代理侦听的相同端口。因此,与端口相关的参数必须指定与相应的代理配置属性相同的端口号:
^{ | Corresponding property in ^{ |
---|---|
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
将单个数据点发送到波前
下面的示例演示如何将单个数据点发送到波前。你使用你在上面创建的波前发送器。
单个公制或增量计数器
fromuuidimportUUID# Wavefront metrics data format:# <metricName> <metricValue> [<timestamp>] source=<source> [pointTags]wavefront_sender.send_metric(name="new york.power.usage",value=42422.0,timestamp=1533529977,source="localhost",tags={"datacenter":"dc1"})# Wavefront delta counter data format:# <metricName> <metricValue> source=<source> [pointTags]wavefront_sender.send_delta_counter(name="delta.counter",value=1.0,source="localhost",tags={"datacenter":"dc1"})
单直方图分布
fromuuidimportUUIDfromwavefront_sdk.entities.histogramimporthistogram_granularity# Wavefront histogram data format:# {!M | !H | !D} [<timestamp>] #<count> <mean> [centroids] <histogramName> source=<source> [pointTags]# Example: You can choose to send to at most 3 bins: Minute, Hour, Day# "!M 1533529977 #20 30.0 #10 5.1 request.latency source=appServer1 region=us-west"# "!H 1533529977 #20 30.0 #10 5.1 request.latency source=appServer1 region=us-west"# "!D 1533529977 #20 30.0 #10 5.1 request.latency source=appServer1 region=us-west"wavefront_sender.send_distribution(name="request.latency",centroids=[(30,20),(5.1,10)],histogram_granularities={histogram_granularity.DAY,histogram_granularity.HOUR,histogram_granularity.MINUTE},timestamp=1533529977,source="appServer1",tags={"region":"us-west"})
单跨
fromuuidimportUUID# Wavefront trace and span data format:# <tracingSpanName> source=<source> [pointTags] <start_millis> <duration_milliseconds># Example: "getAllUsers source=localhost# traceId=7b3bf470-9456-11e8-9eb6-529269fb1459# spanId=0313bafe-9457-11e8-9eb6-529269fb1459# parent=2f64e538-9457-11e8-9eb6-529269fb1459# application=Wavefront http.method=GET# 1533529977 343500"wavefront_sender.send_span(name="getAllUsers",start_millis=1533529977,duration_millis=343500,source="localhost",trace_id=UUID("7b3bf470-9456-11e8-9eb6-529269fb1459"),span_id=UUID("0313bafe-9457-11e8-9eb6-529269fb1459"),parents=[UUID("2f64e538-9457-11e8-9eb6-529269fb1459")],follows_from=None,tags=[("application","Wavefront"),("http.method","GET")],span_logs=None)
发送批处理数据
下面的示例演示如何手动生成数据点,并将它们作为批处理发送到波前。
批量指标
fromuuidimportUUIDfromwavefront_sdk.commonimportmetric_to_line_data# Generate string data in Wavefront metric formatone_metric_data=metric_to_line_data(name="new-york.power.usage",value=42422,timestamp=1493773500,source="localhost",tags={"datacenter":"dc1"},default_source="defaultSource")# Result of one_metric_data:# '"new-york.power.usage" 42422.0 1493773500 source="localhost" "datacenter"="dc1"\n'# List of databatch_metric_data=[one_metric_data,one_metric_data]# Send list of data immediatelywavefront_sender.send_metric_now(batch_metric_data)
批次直方图
fromuuidimportUUIDfromwavefront_sdk.entities.histogramimporthistogram_granularityfromwavefront_sdk.commonimporthistogram_to_line_data# Generate string data in Wavefront histogram formatone_histogram_data=histogram_to_line_data(name="request.latency",centroids=[(30.0,20),(5.1,10)],histogram_granularities={histogram_granularity.MINUTE,histogram_granularity.HOUR,histogram_granularity.DAY},timestamp=1493773500,source="appServer1",tags={"region":"us-west"},default_source="defaultSource")# Result of one_histogram_data:# '!D 1493773500 #20 30.0 #10 5.1 "request.latency" source="appServer1" "region"="us-west"\n# !H 1493773500 #20 30.0 #10 5.1 "request.latency" source="appServer1" "region"="us-west"\n# !M 1493773500 #20 30.0 #10 5.1 "request.latency" source="appServer1" "region"="us-west"\n'# List of databatch_histogram_data=[one_histogram_data,one_histogram_data]# Send list of data immediatelywavefront_sender.send_distribution_now(batch_histogram_data)
批处理跟踪数据
fromuuidimportUUIDfromwavefront_sdk.commonimporttracing_span_to_line_data# Generate string data in Wavefront tracing span formatone_tracing_span_data=tracing_span_to_line_data(name="getAllUsers",start_millis=1552949776000,duration_millis=343,source="localhost",trace_id=UUID("7b3bf470-9456-11e8-9eb6-529269fb1459"),span_id=UUID("0313bafe-9457-11e8-9eb6-529269fb1459"),parents=[UUID("2f64e538-9457-11e8-9eb6-529269fb1459")],follows_from=[UUID("5f64e538-9457-11e8-9eb6-529269fb1459")],tags=[("application","Wavefront"),("http.method","GET")],span_logs=None,default_source="defaultSource")# Result of one_tracing_span_data:# '"getAllUsers" source="localhost" traceId=7b3bf470-9456-11e8-9eb6-529269fb1459 spanId=0313bafe-# 9457-11e8-9eb6-529269fb1459 parent=2f64e538-9457-11e8-9eb6-529269fb1459 followsFrom=5f64e538-# 9457-11e8-9eb6-529269fb1459 "application"="Wavefront" "http.method"="GET" 1552949776000 343\n'# List of databatch_span_data=[one_tracing_span_data,one_tracing_span_data]# Send list of data immediatelywavefront_sender.send_span_now(batch_span_data)
获取失败计数
如果在发送度量、柱状图或跟踪数据时观察到任何故障,则可以获取故障总数。
# Get the total failure counttotal_failures=wavefront_sender.get_failure_count()
关闭连接
如果波前发送器是WavefrontDirectClient
,请在关闭应用程序之前刷新所有缓冲区,然后关闭连接。
# To shut down a WavefrontDirectClient# Flush all buffers.wavefront_sender.flush_now()# Close the sender connectionwavefront_sender.close()
如果波前发送器是WavefrontProxyClient
,请在关闭应用程序之前关闭连接。
# To shut down a WavefrontProxyClient# Close the sender connectionwavefront_sender.close()