metrics
的信息可以以报表的形式推送到:控制台、日志文件、CSV文件、JMX。
当然也可以推送到其他的存储系统,比如InfluxDB,而且metrics
社区如此繁荣,这样的推送早已有开源社区写好了。引入:
<dependency>
<groupId>com.github.davidb</groupId>
<artifactId>metrics-influxdb</artifactId>
<version>1.1.0</version>
</dependency>
编码:
ScheduledReporter influxdbReporter = InfluxdbReporter.forRegistry(metricRegistry)
.protocol(
// HttpInfluxdbProtocol(String scheme, String host, int port, String user, String password, String db)
new HttpInfluxdbProtocol("http", "127.0.0.1", 8086, "admin", "123456", "metrics"))
.convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).filter(MetricFilter.ALL)
.skipIdleMetrics(false) //
.tag("cluster", "adindex4j") //
.tag("client", "index-worker") //
.tag("server", "Tomcat#1") //
.transformer(new CategoriesMetricMeasurementTransformer("module", "artifact")) //
.build();
influxdbReporter.start(10, TimeUnit.SECONDS);
关于 influxdb的功能介绍和数据模型,请阅读 influxdb-introduction。
Users of macOS 10.8 and higher can install InfluxDB using the Homebrew package manager. Once brew
is installed, you can install InfluxDB by running:
$ brew update
$ brew install influxdb
安装完成地址: /usr/local/Cellar/influxdb/1.7.6
$ influxd -config /usr/local/etc/influxdb.conf
- 链接服务器
$ influx
Connected to http://localhost:8086 version v1.7.6
InfluxDB shell version: v1.7.6
Enter an InfluxQL query
>
$ influx -precision rfc3339
Connected to http://localhost:8086 version 1.7.x
InfluxDB shell 1.7.x
>
- 创建数据库
$ influx -precision rfc3339
Connected to http://localhost:8086 version 1.7.x
InfluxDB shell 1.7.x
> CREATE DATABASE mydb
> SHOW DATABASES
name: databases
name
----
_internal
mydb
> USE mydb
Using database mydb
> INSERT cpu,host=serverA,region=us_west value=0.64
> show measurements
> SELECT "host", "region", "value" FROM "cpu"
name: cpu
time host region value
---- ---- ------ -----
1560225964764481000 serverA us_west 0.64
要知道数据库存在哪里呢?
You’ll have to update the configuration file appropriately for each InfluxDB instance you have.
...
[meta]
dir = "/mnt/db/meta"
...
...
[data]
dir = "/mnt/db/data"
...
wal-dir = "/mnt/influx/wal"
...
...
[hinted-handoff]
...
dir = "/mnt/db/hh"
...
$ brew update
$ brew install grafana
To start Grafana using homebrew services first make sure homebrew/services is installed.
brew tap homebrew/services
Then start Grafana using:
brew services start grafana
Default login and password admin
/ admin
To run Grafana open your browser and go to http://localhost:3000/. 3000 is the default http port that Grafana listens to if you haven’t configured a different port. Then follow the instructions here.
登录时,用账号:admin 密码: admin
第一次登录,提示修改密码。不妨设置为:123456
- 配置数据源
- 创建趋势图
问题:既然metrics都是标准化的,那么对它的展现也应该是标准化的,难道就没有
grafana plugin
提前自动给metrics
创建Dashbord吗? 答案应该是有的。
The Configuration file should be located at /usr/local/etc/grafana/grafana.ini
.
The log file should be located at /usr/local/var/log/grafana/grafana.log
.