Skip to content

Make Azure Monitor exporter conditional for local dev - #94

Open
Lily Ma (lilyjma) wants to merge 1 commit into
mainfrom
otel-change
Open

Make Azure Monitor exporter conditional for local dev#94
Lily Ma (lilyjma) wants to merge 1 commit into
mainfrom
otel-change

Conversation

@lilyjma

Copy link
Copy Markdown
Contributor

Problem

All 5 projects crash on func start locally because UseAzureMonitorExporter() requires APPLICATIONINSIGHTS_CONNECTION_STRING, which is only set in Azure (provisioned by bicep).

Fix

Made the exporter conditional — only wire it up when the connection string env var is present. OpenTelemetry + UseFunctionsWorkerDefaults() still initializes; telemetry in Azure is unaffected.

Files changed

  • src/FunctionsMcpApp/Program.cs
  • src/FunctionsMcpPrompts/Program.cs
  • src/FunctionsMcpResources/Program.cs
  • src/FunctionsMcpTool/Program.cs
  • src/McpWeatherApp/Program.cs

Skip UseAzureMonitorExporter() when APPLICATIONINSIGHTS_CONNECTION_STRING
is not set so func start works locally without extra configuration.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
builder.Services.AddOpenTelemetry()
.UseFunctionsWorkerDefaults()
.UseAzureMonitorExporter();
var otel = builder.Services.AddOpenTelemetry()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move .AddOpenTelemetry() call to inside if block as well.

if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")))
{
builder.Services.AddOpenTelemetry()
.UseFunctionsWorkerDefaults()
.UseAzureMonitorExporter();
}

@@ -11,9 +11,13 @@

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the approach of using regions so that we do not have unexpected behaviour in prod e.g.

#if DEBUG
                    if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")))
                    {
                        services.AddOpenTelemetry()
                            .UseFunctionsWorkerDefaults()
                            .UseAzureMonitorExporter();
                    }
#else
                    services.AddOpenTelemetry()
                        .UseFunctionsWorkerDefaults()
                        .UseAzureMonitorExporter();
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants