我想跟踪 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)