我正在尝试检查 opentelemetry 导出器是否发送了指标,并且我想首先在控制台上测试它,如本页所述 https://opentelemetry.io/docs/languages/python/exporters/#console ,
所以我做了这段代码:
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader, ConsoleMetricExporter
resource = Resource(attributes={
SERVICE_NAME: "your-service-name"
})
reader = PeriodicExportingMetricReader(ConsoleMetricExporter())
meterProvider = MeterProvider(resource=resource, metric_readers=[reader])
metrics.set_meter_provider(meterProvider)
meter = metrics.get_meter()
counter = meter.create_counter("my_metric", description="Example metric sent to console", unit="1")
l = [1, 2, 3, 4, 5]
for value in l:
counter.add(value)
但我无法将指标发送到标准输出。这就是我所看到的:
{
"resource_metrics": [
{
"resource": {
"attributes": {
"service.name": "your-service-name"
},
"schema_url": ""
},
"scope_metrics": [
{
"scope": {
"name": "__main__",
"version": "",
"schema_url": "",
"attributes": null
},
"metrics": [
{
"name": "my_metric",
"description": "Example metric sent to console",
"unit": "1",
"data": {
"data_points": [
{
"attributes": {},
"start_time_unix_nano": 1731532392141560100,
"time_unix_nano": 1731532392141560100,
"value": 15
}
],
"aggregation_temporality": 2,
"is_monotonic": true
}
}
],
"schema_url": ""
}
],
"schema_url": ""
}
]
}
出了什么问题?
您似乎确实看到了指标。您有一个 data_point,其值为 15 - 是 [1, 2, 3, 4, 5] 的累积值。这些值会累积,因为您正在使用计数器。
"metrics": [
{
"name": "my_metric",
"description": "Example metric sent to console",
"unit": "1",
"data": {
"data_points": [
{
"attributes": {},
"start_time_unix_nano": 1731532392141560100,
"time_unix_nano": 1731532392141560100,
"value": 15
}
],
"aggregation_temporality": 2,
"is_monotonic": true
}
}
]
您可以将阅读器配置为每 0.5 秒导出一次,然后通过包含 time.sleep(1) 每秒添加到计数器中,以观察值的增加:
reader = PeriodicExportingMetricReader(ConsoleMetricExporter(), export_interval_millis=500)
meterProvider = MeterProvider(resource=resource, metric_readers=[reader])
metrics.set_meter_provider(meterProvider)
meter = metrics.get_meter("your-service-name")
counter = meter.create_counter("my_metric", description="Example metric sent to console", unit="1")
l = [1, 2, 3, 4, 5]
for value in l:
counter.add(value)
time.sleep(1)
然后你的出口商将生产类似的东西:
"data_points": [
{
"attributes": {},
"start_time_unix_nano": 1731545432747512000,
"time_unix_nano": 1731545433250621000,
"value": 1,
"exemplars": []
}
],
...
"data_points": [
{
"attributes": {},
"start_time_unix_nano": 1731545432747512000,
"time_unix_nano": 1731545433757252000,
"value": 3,
"exemplars": []
}
],
...
"data_points": [
{
"attributes": {},
"start_time_unix_nano": 1731545432747512000,
"time_unix_nano": 1731545434259877000,
"value": 3,
"exemplars": []
}
],
...
"data_points": [
{
"attributes": {},
"start_time_unix_nano": 1731545432747512000,
"time_unix_nano": 1731545434763509000,
"value": 6,
"exemplars": []
}
],
随着你的计数器累积。
如果您想观察样本数据中的值
[1, 2, 3, 4, 5]
,您可以使用带有 gauge.set(value) 的仪表而不是计数器。