-
Notifications
You must be signed in to change notification settings - Fork 17
时序数据指标采集
nfw999 edited this page Nov 29, 2019
·
1 revision
数据库为influxdb,statsdClient和influxClient适配influxdb数据协议,客户端仅负责协议的封装,数据发送方式需要自行实现或借助第三方客户端,推荐使用log4j实现UDP、TCP、File、Console、HTTP、Kafaka等方式发送。
statsdClient数据格式为:statsd_count,name=测试,project=metricTest:10|c|@0.5 statsdClient数据经statsd后写入influxdb格式为:statsd_count,name=测试,project=metricTest value=10
influxClient数据格式为:influx_test,action=read,project=metricTest,user=ning count=1,value="123"
<dependency>
<groupId>com.autohome.autolog4j</groupId>
<artifactId>metric</artifactId>
<version>${latestVersion}</version>
</dependency>
客户端数据发送方式自由定制,市面上大部分客户端仅支持udp方式发送数据,部门内借助log4j作为数据发送工具,。
@Bean
public IStatsdClient statsdClient() {
//sendSink 自行选择输出端 Consumer<String>
//sampleRate 为采样率
//commonTag 公共标签,通过此Client发送的数据都会打上公共标签
Logger LOGGER = LoggerFactory.getLogger("AsyncStatsrelay");
return LogStatsdClient.client(msg -> LOGGER.trace(msg)).sampleRate(0.5).commonTag("project", "metricTest").build();
//return LogStatsdClient.client(msg -> LOGGER.trace(msg)).sampleRate(0.5).commonTag("project", "metricTest").build();
}
@Bean
public IInfluxClient influxClient() {
//sendSink 自行选择输出端 Consumer<String>
//commonTag 公共标签,通过此Client发送的数据都会打上公共标签
Logger LOGGER = LoggerFactory.getLogger("AsyncInflux");
return LogInfluxClient.client(msg -> LOGGER.trace(msg)).commonTag("project", "metricTest").build();
}
statsd指标发送代码示例
//增10
statsdClient.count(StatsdPoint.measurement("statsd_count").tag("name","nfw").build(),10);
//增1
statsdClient.incrementCounter(StatsdPoint.measurement("statsd_count").tag("name","nfw").build());
//减1
statsdClient.decrementCounter(StatsdPoint.measurement("statsd_count").tag("name","nfw").build());
//覆盖值1
statsdClient.recordGaugeValue(StatsdPoint.measurement("statsd_guage").tag("name","nfw").build(),1);
//增加偏移量1
statsdClient.recordGaugeDelta(StatsdPoint.measurement("statsd_guage").tag("name","nfw").build(),1);
//timing
statsdClient.recordTiming(StatsdPoint.measurement("statsd_timing").tag("name","nfw").build(),1);
//set
statsdClient.recordSetEvent(StatsdPoint.measurement("statsd_set").tag("name","nfw").build(),"set");
influxdb指标发送代码示例
influxClient.write(InfluxPoint.measurement("influx_test").tag("type2","dot").tag("tagkey","tagvalue").field("value","123").time(System.currentTimeMillis(),TimeUnit.MILLISECONDS).build());