如何使用 System.Diagnostics.Metrics.Histogram 计算百分位
我正在尝试使用 .Net Meter API 创建 pxx 指标。下面是示例代码,
System.Diagnostics.Metrics.MeterListener listener = new System.Diagnostics.Metrics.MeterListener
{
InstrumentPublished = (instrument, meterListener) =>
{
if (instrument.Meter.Name == "ct")
{
Console.WriteLine($"{instrument.Name} : {instrument.Meter.Name}");
meterListener.EnableMeasurementEvents(instrument, null);
}
}
};
listener.SetMeasurementEventCallback<long>(
(instrument, measurement, tags, state) => {
KeyValuePair<string, object>[] tagsKv = tags.ToArray();
Console.WriteLine($"state => {state}");
foreach (KeyValuePair<string, object> tagKv in tagsKv)
{
Console.WriteLine($"tag => {tagKv.Key} :: {tagKv.Value}");
}
instrument.
Console.WriteLine($"Instrument: {instrument.Name} has recorded the measurement {measurement}");
});
listener.Start();
System.Diagnostics.Metrics.Meter meter1 = new System.Diagnostics.Metrics.Meter("ct", "v1.0");
System.Diagnostics.Metrics.Histogram<long> hist = meter1.CreateHistogram<long>(
name: "Latency",
unit: "Ms");
hist.Record(10);
hist.Record(20);
hist.Record(30);
我可以在measurementEventCallback中看到记录的值,即10、20、30。但我想根据这些值计算 pxx (p99、p90 等)。我怎样才能做到这一点。我在网上找不到任何例子。
I am trying to create pxx metrics using .Net Meter API. Below is the sample code
System.Diagnostics.Metrics.MeterListener listener = new System.Diagnostics.Metrics.MeterListener
{
InstrumentPublished = (instrument, meterListener) =>
{
if (instrument.Meter.Name == "ct")
{
Console.WriteLine(quot;{instrument.Name} : {instrument.Meter.Name}");
meterListener.EnableMeasurementEvents(instrument, null);
}
}
};
listener.SetMeasurementEventCallback<long>(
(instrument, measurement, tags, state) => {
KeyValuePair<string, object>[] tagsKv = tags.ToArray();
Console.WriteLine(quot;state => {state}");
foreach (KeyValuePair<string, object> tagKv in tagsKv)
{
Console.WriteLine(quot;tag => {tagKv.Key} :: {tagKv.Value}");
}
instrument.
Console.WriteLine(quot;Instrument: {instrument.Name} has recorded the measurement {measurement}");
});
listener.Start();
System.Diagnostics.Metrics.Meter meter1 = new System.Diagnostics.Metrics.Meter("ct", "v1.0");
System.Diagnostics.Metrics.Histogram<long> hist = meter1.CreateHistogram<long>(
name: "Latency",
unit: "Ms");
hist.Record(10);
hist.Record(20);
hist.Record(30);
I am able to see recorded values i.e. 10, 20, 30 in measurementEventCallback. But I want to calculate pxx (p99, p90 etc) from these values. How can I achieve that. I couldn't find any examples online.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短摘要:Meter API 不支持它。
参考 https://github.com/dotnet/runtime/issues/66592 了解详细信息回答。
Short Summary: It is not supported by Meter API.
Ref https://github.com/dotnet/runtime/issues/66592 for detailed answer.