向应用洞察添加可选参数(服务总线触发Azure功能)

o75abkj4  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(86)

我的应用由服务总线触发的Azure函数和经典API组成。我想在App Insights中为API和Azure函数添加2个可选参数。我已经为API完成了此操作(我使用遥测初始化程序),API是一个简单的接口,因为我可以在其中注入HttpContext(可选参数的值可以通过header访问),但是现在在azure函数中不能简单的使用HttpContext来访问这些参数(并使用ITELEMETRY),但是它们可以通过函数中使用的Helper方法访问。我想知道是否有办法只在使用此Helper方法时才向App Insight添加可选参数。
我第一次尝试添加这些参数时使用的助手函数(不起作用,不会向应用洞察添加任何内容)

public class FunctionHelper 
    {
        private readonly IServiceProvider serviceProvider;
        private static readonly TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.CreateDefault();
        private readonly TelemetryClient telemetryClient = new(telemetryConfiguration);

        public FunctionScopeHelper(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
        }

        public IServiceScope BuildServiceScopeWithContext(string parameter1, string parameter2)
        {
            var requestTelemetry = new RequestTelemetry
            {
                Name = "Function Telemetry"
            };

            var operation = this.telemetryClient.StartOperation(requestTelemetry);

            requestTelemetry.Properties.Add(Parameter1, parameter1);
            requestTelemetry.Properties.Add(Parameter2, parameter2);

            this.telemetryClient.StopOperation(operation);

            // rest of the helper method

            return scope;
        }
    }

第二次尝试添加这些参数(仍然没有添加任何内容)

public class FunctionHelper 
    {
        private readonly IServiceProvider serviceProvider;
        private static readonly TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.CreateDefault();
        private readonly TelemetryClient telemetryClient = new(telemetryConfiguration);

        public FunctionScopeHelper(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
        }

        public IServiceScope BuildServiceScopeWithContext(string parameter1, string parameter2)
        {
            var requestTelemetry = new RequestTelemetry
            {
                Name = "Function Telemetry"
            };

            var operation = this.telemetryClient.StartOperation(requestTelemetry);

            if (this.telemetryClient.Context.GlobalProperties is ISupportProperties)
            {
                this.telemetryClient.Context.GlobalProperties.Add(Parameter1, parameter1);
                this.telemetryClient.Context.GlobalProperties.Add(Parameter2, parameter2);
            }

            this.telemetryClient.StopOperation(operation);

            // rest of the helper method

            return scope;
        }
    }

下面是我如何在API端处理它的(通过ITELEMETRYInitializer)

public class TelemetryInitializer : ITelemetryInitializer
    {
        private readonly IHttpContextAccessor httpContextAcessor;

        public TelemetryInitializer(IHttpContextAccessor httpContextAcessor)
        {
            this.httpContextAcessor = httpContextAcessor;
        }

        public void Initialize(ITelemetry telemetry)
        {
            if (telemetry is not ISupportProperties supportPropertiesTelemetry)
            {
                return;
            }

            var httpContext = this.httpContextAcessor.HttpContext;

            if (httpContext != null)
            {
                var (parameter1, parameter2) = HelperClass.GetParameters(httpContext);

                var properties = supportPropertiesTelemetry.Properties;

                if (properties.ContainsKey("Parameter1") || parameter1 == null || parameter2 == null )
                {
                    return;
                }

                properties["Parameter1"] = parameter1;
                properties["Parameter2"] = parameter2;
            }          
        }
    }

希望我解释得很好。谢谢帮助:)
我尝试用以下三种方法添加可选参数:
1.项目元素初始化程序(失败:功能应用程序中缺少httpContext)
1.使用RequestTelemetry.Add()通过helper方法添加(不向应用洞察添加任何内容)
1.使用TelemetryClient.Context.GlobalProperties.Add()通过helper方法添加(也不会向应用洞察添加任何内容)

tf7tbtn2

tf7tbtn21#

若要向Service Bus触发的Azure函数中的App Insights添加可选参数,可以使用TelemetryClient.Context.GlobalProperties属性。GlobalProperties属性是一个字典,可用于存储与TelemetryClient发送的所有遥测项关联的自定义属性。
感谢Peter Bons的评论。
可以将可选参数添加到GlobalProperties字典中。

public class FunctionHelper
 {

   private readonly TelemetryClient telemetryClient;
   public FunctionHelper()
   {
          this.telemetryClient = new TelemetryClient();
   }
   public void AddOptionalParameters(string parameter1, string parameter2)
   { 
        if (this.telemetryClient.Context.GlobalProperties is ISupportProperties)
           {
                    this.telemetryClient.Context.GlobalProperties.Add("Parameter1", parameter1);
                    this.telemetryClient.Context.GlobalProperties.Add("Parameter2", parameter2);
           }
    }

然后,您可以在Azure函数中调用AddOptionalParameters方法并传入可选参数的值。
这些值将被添加到GlobalProperties字典中,并将与TelemetryClient发送的所有遥测项目相关联。
如果无法使用ITelemetryInitializerRequestTelemetry.Add()方法添加可选参数,则可以尝试使用TelemetryClient.TrackEvent()方法将自定义事件发送到App Insights。
然后,您可以修改FunctionHelper类,以使用可选参数发送定制事件。
使用optional parameters将自定义事件发送到App Insights

public class FunctionHelper
   {
         private readonly IServiceProvider serviceProvider;
         private static readonly TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.CreateDefault();
         private readonly TelemetryClient telemetryClient = new TelemetryClient(telemetryConfiguration);

          public FunctionScopeHelper(IServiceProvider serviceProvider)
           {
              this.serviceProvider = serviceProvider;
           }

           public IServiceScope BuildServiceScopeWithContext(string parameter1, string parameter2)
           {
                telemetryClient.TrackEvent("CustomEvent", new Dictionary<string, string>
           {
                { "Parameter1", parameter1 },
                { "Parameter2", parameter2 }
           });

            var serviceScope = serviceProvider.CreateScope();
            return serviceScope;
           }
        }

欲知更多信息,请参考MSDoc1MSDoc2.

相关问题