1

我想跟踪 ms azure monitor 中某些 id 的使用情况(应用程序洞察力)。我现在正在使用以下代码摘录来执行此操作。问题是,例如,如果连续快速拨打六个电话,则第一个电话会被跟踪六次。第二个被跟踪五次。第三四次以此类推。(看下图)如何解决所有 id 仅在显式调用时才被跟踪的问题?

from azure_monitor import AzureMonitorMetricsExporter
from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics.export.controller import PushController

metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
exporter = AzureMonitorMetricsExporter(
    connection_string='InstrumentationKey=<my-key>'
)
controller = PushController(meter, exporter, 0)


def add_question(question_id):
    requests_counter = meter.create_metric(
        name=question_id,
        description="question used",
        unit="1",
        value_type=int,
        metric_type=Counter,
    )
    labels = {"type": "question"}
    requests_counter.add(1, labels)
    controller.shutdown()
    print(str(datetime.now()) + " - Done recording metric: question")
    print(question_id)

天蓝色监控图

4

1 回答 1

0

对我的 github 错误报告的评论解决了我的问题。唯一要做的更改是添加statefull=False如下内容MeterProvider

metrics.set_meter_provider(MeterProvider(stateful=False))

于 2020-11-30T16:30:33.710 回答