Metrics 数据上报

参考Samples: dubbo-go-samples/metrics

1. 数据上报配置

在不指定 metrics: 字段的时候,默认开启拉模式 prometheus 数据上报,端口默认为9090,监听path默认为/metrics。可参考如下服务端配置例子来定制化。

  1. dubbo:
  2. application:
  3. version: 3.0.0-rc3 # 版本号
  4. metrics:
  5. enable: true # default is true
  6. path: /custom-metrics-path # default is /metrics
  7. port: 9091 # default is 9090
  8. namespace: dubbo # default is dubbo 作为数据上报 metrics 的前缀
  9. registries:
  10. myzk:
  11. protocol: zookeeper
  12. address: localhost:2181
  13. protocols:
  14. triple:
  15. name: tri
  16. port: 20000
  17. provider:
  18. services:
  19. GreeterProvider:
  20. group: dubbo-go # 所属 group,需要与客户端一致
  21. interface: com.apache.dubbo.HelloService # 接口名

2. 默认数据上报指标

本地启动上述服务端后,可于 localhost:9091/custom-metrics-path 收集到默认数据指标,包含gc、goroutine数目,内存占用情况等。

  1. # HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
  2. # TYPE go_gc_duration_seconds summary
  3. go_gc_duration_seconds{quantile="0"} 3.0916e-05
  4. go_gc_duration_seconds{quantile="0.25"} 3.0916e-05
  5. go_gc_duration_seconds{quantile="0.5"} 3.1167e-05
  6. go_gc_duration_seconds{quantile="0.75"} 0.000164084
  7. go_gc_duration_seconds{quantile="1"} 0.000164084
  8. go_gc_duration_seconds_sum 0.000226167
  9. go_gc_duration_seconds_count 3
  10. # HELP go_goroutines Number of goroutines that currently exist.
  11. # TYPE go_goroutines gauge
  12. go_goroutines 25
  13. # HELP go_info Information about the Go environment.
  14. # TYPE go_info gauge
  15. go_info{version="go1.17.2"} 1
  16. # HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
  17. # TYPE go_memstats_alloc_bytes gauge
  18. go_memstats_alloc_bytes 6.195808e+06
  19. # HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
  20. # TYPE go_memstats_alloc_bytes_total counter
  21. go_memstats_alloc_bytes_total 9.482768e+06
  22. # HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
  23. # TYPE go_memstats_buck_hash_sys_bytes gauge
  24. go_memstats_buck_hash_sys_bytes 1.449179e+06
  25. # HELP go_memstats_frees_total Total number of frees.
  26. # TYPE go_memstats_frees_total counter
  27. go_memstats_frees_total 29419
  28. # HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.
  29. # TYPE go_memstats_gc_cpu_fraction gauge
  30. go_memstats_gc_cpu_fraction 0.022937924367975027
  31. # HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.
  32. # TYPE go_memstats_gc_sys_bytes gauge
  33. go_memstats_gc_sys_bytes 5.235864e+06
  34. # HELP go_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use.
  35. # TYPE go_memstats_heap_alloc_bytes gauge
  36. go_memstats_heap_alloc_bytes 6.195808e+06
  37. # HELP go_memstats_heap_idle_bytes Number of heap bytes waiting to be used.
  38. # TYPE go_memstats_heap_idle_bytes gauge
  39. go_memstats_heap_idle_bytes 3.792896e+06
  40. # HELP go_memstats_heap_inuse_bytes Number of heap bytes that are in use.
  41. # TYPE go_memstats_heap_inuse_bytes gauge
  42. go_memstats_heap_inuse_bytes 8.036352e+06
  43. # HELP go_memstats_heap_objects Number of allocated objects.
  44. # TYPE go_memstats_heap_objects gauge
  45. go_memstats_heap_objects 16489
  46. # HELP go_memstats_heap_released_bytes Number of heap bytes released to OS.
  47. # TYPE go_memstats_heap_released_bytes gauge
  48. go_memstats_heap_released_bytes 3.416064e+06
  49. # HELP go_memstats_heap_sys_bytes Number of heap bytes obtained from system.
  50. # TYPE go_memstats_heap_sys_bytes gauge
  51. go_memstats_heap_sys_bytes 1.1829248e+07
  52. # HELP go_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection.
  53. # TYPE go_memstats_last_gc_time_seconds gauge
  54. go_memstats_last_gc_time_seconds 1.635778234064745e+09
  55. # HELP go_memstats_lookups_total Total number of pointer lookups.
  56. # TYPE go_memstats_lookups_total counter
  57. go_memstats_lookups_total 0
  58. # HELP go_memstats_mallocs_total Total number of mallocs.
  59. # TYPE go_memstats_mallocs_total counter
  60. go_memstats_mallocs_total 45908
  61. # HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.
  62. # TYPE go_memstats_mcache_inuse_bytes gauge
  63. go_memstats_mcache_inuse_bytes 9600
  64. # HELP go_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system.
  65. # TYPE go_memstats_mcache_sys_bytes gauge
  66. go_memstats_mcache_sys_bytes 16384
  67. # HELP go_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures.
  68. # TYPE go_memstats_mspan_inuse_bytes gauge
  69. go_memstats_mspan_inuse_bytes 116144
  70. # HELP go_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system.
  71. # TYPE go_memstats_mspan_sys_bytes gauge
  72. go_memstats_mspan_sys_bytes 131072
  73. # HELP go_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place.
  74. # TYPE go_memstats_next_gc_bytes gauge
  75. go_memstats_next_gc_bytes 7.935184e+06
  76. # HELP go_memstats_other_sys_bytes Number of bytes used for other system allocations.
  77. # TYPE go_memstats_other_sys_bytes gauge
  78. go_memstats_other_sys_bytes 1.426069e+06
  79. # HELP go_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator.
  80. # TYPE go_memstats_stack_inuse_bytes gauge
  81. go_memstats_stack_inuse_bytes 753664
  82. # HELP go_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator.
  83. # TYPE go_memstats_stack_sys_bytes gauge
  84. go_memstats_stack_sys_bytes 753664
  85. # HELP go_memstats_sys_bytes Number of bytes obtained from system.
  86. # TYPE go_memstats_sys_bytes gauge
  87. go_memstats_sys_bytes 2.084148e+07
  88. # HELP go_threads Number of OS threads created.
  89. # TYPE go_threads gauge
  90. go_threads 12

3. 调用指标 RT

启动例子中的客户端,经过几次调用后,会发现增加了对应的数据指标,以默认namespace dubbo 为前缀。

  1. # HELP dubbo_provider_service_rt
  2. # TYPE dubbo_provider_service_rt gauge
  3. dubbo_provider_service_rt{group="dubbo-go",method="SayHello",service="com.apache.dubbo.HelloService",timeout="",version="3.0.0-rc3"} 127542

可看到对应接口名、方法名的请求rt时间。该部分调用指标待后续完善。

4. 手动数据上报

可在业务代码中调用API进行手动数据上报。支持Counter、Summary、Gauge 类型,并支持多个标签。上报后即可收集。

  1. import "dubbo.apache.org/dubbo-go/v3/metrics/prometheus"
  2. for {
  3. // metrics refresh per second
  4. time.Sleep(time.Second)
  5. prometheus.IncSummary("test_summary", rand.Float64())
  6. prometheus.IncSummaryWithLabel("test_summary_with_label", rand.Float64(), map[string]string{
  7. "summarylabel1": "value1", // label and value for this summary
  8. "summarylabel2": "value2",
  9. })
  10. prometheus.IncCounter("test_counter")
  11. prometheus.IncCounterWithLabel("test_counter_with_label", map[string]string{
  12. "counterlabel1": "value1", // label and value for this counter
  13. "counterlabel2": "value2",
  14. })
  15. prometheus.SetGauge("test_gauge", rand.Float64())
  16. prometheus.SetGaugeWithLabel("test_gauge_with_label", rand.Float64(), map[string]string{
  17. "gaugelabel1": "value1",
  18. "gaugelabel2": "value2",
  19. })
  20. }

运行一段时间后,可以收集到如下用户定义指标

  1. # HELP dubbo_test_counter
  2. # TYPE dubbo_test_counter counter
  3. dubbo_test_counter 463
  4. # HELP dubbo_test_counter_with_label
  5. # TYPE dubbo_test_counter_with_label counter
  6. dubbo_test_counter_with_label{counterlabel1="value1",counterlabel2="value2"} 463
  7. # HELP dubbo_test_gauge
  8. # TYPE dubbo_test_gauge gauge
  9. dubbo_test_gauge 0.7402836247772934
  10. # HELP dubbo_test_gauge_with_label
  11. # TYPE dubbo_test_gauge_with_label gauge
  12. dubbo_test_gauge_with_label{gaugelabel1="value1",gaugelabel2="value2"} 0.8360973807546456
  13. # HELP dubbo_test_summary
  14. # TYPE dubbo_test_summary summary
  15. dubbo_test_summary_sum 228.1800106582441
  16. dubbo_test_summary_count 463
  17. # HELP dubbo_test_summary_with_label
  18. # TYPE dubbo_test_summary_with_label summary
  19. dubbo_test_summary_with_label{summarylabel1="value1",summarylabel2="value2",quantile="0.5"} 0.5174757569778469
  20. dubbo_test_summary_with_label{summarylabel1="value1",summarylabel2="value2",quantile="0.75"} 0.734268575017709
  21. dubbo_test_summary_with_label{summarylabel1="value1",summarylabel2="value2",quantile="0.9"} 0.9059918694279894
  22. dubbo_test_summary_with_label{summarylabel1="value1",summarylabel2="value2",quantile="0.98"} 0.9761803018478838
  23. dubbo_test_summary_with_label{summarylabel1="value1",summarylabel2="value2",quantile="0.99"} 0.9820270046489341
  24. dubbo_test_summary_with_label{summarylabel1="value1",summarylabel2="value2",quantile="0.999"} 0.9986025122460248
  25. dubbo_test_summary_with_label_sum{summarylabel1="value1",summarylabel2="value2"} 233.609026131067
  26. dubbo_test_summary_with_label_count{summarylabel1="value1",summarylabel2="value2"} 463

最后修改 December 16, 2022: Fix check (#1736) (97972c1)