Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.37k stars 4.79k forks source link

[BUG] NRE in AzureMonitorLogExporter prevents logs from being uploaded #26807

Closed stevenbrix closed 2 years ago

stevenbrix commented 2 years ago

Library name and version

Azure.Monitor.OpenTelemetry.Exporter

Describe the bug

I downloaded the source code to use this functionality because I need logs to be uploaded to Application Insights. After debugging why my logs weren't uploading, there is an exception in the constructor of MessageData because the logRecord.State object is null:

public MessageData(int version, LogRecord logRecord) : base(version)
{
       Message = logRecord.State.ToString();
       SeverityLevel = LogsHelper.GetSeverityLevel(logRecord.LogLevel)
 }

to fix this:

public MessageData(int version, LogRecord logRecord) : base(version)
{
       Message = logRecord.State?.ToString() ?? logRecord.FormattedMessage;
       SeverityLevel = LogsHelper.GetSeverityLevel(logRecord.LogLevel)
 }

Although after that, I'm still getting this exception:

OpenTelemetry-AzureMonitor-Exporter - EventId: [2], EventName: [WriteError], Message: [FailedToExport - System.ObjectDisposedException: Cannot access a closed Stream. at System.IO.MemoryStream.Write(ReadOnlySpan1 buffer) at System.Text.Json.Utf8JsonWriter.Flush() at System.Text.Json.Utf8JsonWriter.Dispose() at Azure.Monitor.OpenTelemetry.Exporter.NDJsonWriter.Dispose() in /home/stevenki/code/avs/src/conveyor/avs3/Azure.Monitor.OpenTelemetry.Exporter/src/NDJsonWriter.cs:line 46 at Azure.Monitor.OpenTelemetry.Exporter.ApplicationInsightsRestClient.CreateTrackRequest(IEnumerable1 body) in /home/stevenki/code/avs/src/conveyor/avs3/Azure.Monitor.OpenTelemetry.Exporter/src/ApplicationInsightsRestClient.cs:line 97 at Azure.Monitor.OpenTelemetry.Exporter.ApplicationInsightsRestClient.InternalTrackAsync(IEnumerable`1 body, IPersistentStorage storage, CancellationToken cancellationToken) in /home/stevenki/code/avs/src/conveyor/avs3/Azure.Monitor.OpenTelemetry.Exporter/src/Applicat

Expected behavior

logs are uploaded to Application Insights

Actual behavior

logs aren't uploaded to application insights

Reproduction Steps

This is what my builder looks like. I know it's in F#, but should translate to C# pretty easily :)


    let APP_INSIGHTS_KEY= Values.get AppInsightsKey

    let APP_INSIGHTS_CONN_STRING = sprintf "InstrumentationKey=%s;IngestionEndpoint=https://westus2-2.in.applicationinsights.azure.com/" APP_INSIGHTS_KEY

let hostBuilder = Host.CreateDefaultBuilder(args)
                                 .ConfigureLogging(fun builder ->
                                    builder
                                        .AddConsole()
                                        .AddTraceSource(SOURCE)
                                        .AddApplicationInsights(APP_INSIGHTS_CONN_STRING)
                                        .AddOpenTelemetry(fun options ->
                                            options.IncludeScopes <- true
                                            options.ParseStateValues <- true
                                            options.IncludeFormattedMessage <- true
                                            options.AddAzureMonitorLogExporter(fun logOpts ->
                                                logOpts.ConnectionString <- APP_INSIGHTS_CONN_STRING
                                                ) |> ignore
                                            ) |> ignore)

                            .ConfigureServices(fun hostingContext services ->
                              services.AddHostedService<TenantWorkerService>() |> ignore
                              services.AddOpenTelemetryTracing(fun builder ->
                                builder.AddSource(SOURCE)
                                       .AddAzureMonitorTraceExporter(fun options ->
                                           options.ConnectionString <- APP_INSIGHTS_CONN_STRING
                                           ) |> ignore
                                    ) |> ignore
                                )

        let host = hostBuilder.Build()
        host.RunAsync()

Environment

No response

azure-sdk commented 2 years ago

Label prediction was below confidence level 0.6 for Model:ServiceLabels: 'Service Bus:0.24495983,Storage:0.21526647,Monitor - ApplicationInsights:0.04194767'

ghost commented 2 years ago

Thank you for your feedback. This has been routed to the support team for assistance.

PramodValavala-MSFT commented 2 years ago

@stevenbrix Looks like the latest code in the MessageData class should resolve this error. @rajkumar-rangaraj Could you confirm?

rajkumar-rangaraj commented 2 years ago

You're correct. This is fixed in latest code.

PramodValavala-MSFT commented 2 years ago

@rajkumar-rangaraj Thanks for the confirmation! Could you share any insight on when the next release is scheduled for?

@stevenbrix Closing this issue since it has been fixed for the upcoming release.