【问题标题】:AppMetrics how to set application name?AppMetrics如何设置应用名称?
【发布时间】:2019-09-18 10:11:47
【问题描述】:

我在我的 wcf 应用程序中实现了 App.Metrics(App.Metrics 版本 3.1.0)。 当我检查上传数据的 url 时,我发现 app 未填写:

试图找出这种行为的原因,我找到了手册: https://www.app-metrics.io/getting-started/fundamentals/tagging-organizing/

说AssemblyName需要填写,但我仔细检查了-csproj文件包含下一行:

<AssemblyName>MyWebService</AssemblyName>

如何在指标中填充此 app 属性?

【问题讨论】:

    标签: c# .net metrics


    【解决方案1】:

    startup.cs:

    var metrics = MetricsProvider.Instance.Metrics;
    SetMetricsAppTag(metrics, Assembly.GetExecutingAssembly().GetName().Name);   
    
    private static void SetMetricsAppTag(IMetricsRoot metricsRoot, string appTagValue)
    {
         if (!metricsRoot.Options.GlobalTags.ContainsKey("app"))
         {
               metricsRoot.Options.GlobalTags.Add("app", appTagValue);
         }
         else if (string.IsNullOrEmpty(metricsRoot.Options.GlobalTags["app"]) || metricsRoot.Options.GlobalTags["app"] == "unknown")
         {
               metricsRoot.Options.GlobalTags["app"] = appTagValue;
         }
    }
    

    【讨论】:

      【解决方案2】:

      更好的方法是在启动时做-

      var metrics = AppMetrics.CreateDefaultBuilder().
                      Configuration.
                      Configure(options => options.AddAppTag(appName: "nexus"))
                     .Build();
                  services.AddMetrics(metrics);
                  services.AddMetricsTrackingMiddleware();
                  services.AddMetricsEndpoints(opt =>
                  {
                      opt.MetricsTextEndpointOutputFormatter = new MetricsPrometheusTextOutputFormatter();
                      opt.MetricsEndpointOutputFormatter = new MetricsPrometheusProtobufOutputFormatter();
                      opt.EnvironmentInfoEndpointEnabled = false;
                  });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-17
        相关资源
        最近更新 更多