Skip to content

Null Ref Exception when InstrumentationKey is null #613

@TheRealPiotrP

Description

@TheRealPiotrP

We are building some middleware for ASP.NET Core which takes advantage of AI's handling of the Request-Id header. While testing the middleware we wanted to enable AI but did not want to report any telemetry. We therefore created:

var appInsightsOptions =
                new ApplicationInsightsServiceOptions
                {
                    DeveloperMode = Environment.IsDevelopment()
                };

Note that there is no Instrumentation Key. Invoking services.AddApplicationInsightsTelemetry(appInsightsOptions); with these options succeeds. However, our component under test later makes Web Requests via an HttpClient. These invocations result in a NRE with no useful debugging information.

We worked around this issue by adding an empty-string Instrumentation Key to our AI Options:

var appInsightsOptions =
                new ApplicationInsightsServiceOptions
                {
                    DeveloperMode = Environment.IsDevelopment(),
                    InstrumentationKey = ""
                };

Expected:

There are two obvious options:

  • Enable HttpClient to work when AI is enabled but an Instrumentation Key is not provided. This is preferred, since AI seems to work [e.g. it reads an incoming request's Request-Id header and propagates it to Activity.Current.Id] so long as we don't invoke an HttpClient.
  • Provide an actionable exception. This would probably be some sort of argument exception when invoking AddApplicationInsightsTelemetry without an Instrumentation Key.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions