Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[logs] Potential data loss when using custom log enrichment processor and upgrading to v1.5.0 - v1.7.0 #5186

Closed
CodeBlanch opened this issue Dec 15, 2023 · 0 comments
Labels
pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package

Comments

@CodeBlanch
Copy link
Member

[This issue is a meant as a landing spot for anyone searching.]

Issue: When upgrading to OpenTelemetry.dll v1.5.0 - v1.7.0 users may lose attributes added by custom LogRecord processors

Who is impacted?

Users attempting to enrich LogRecord instances via a BaseProcessor<LogRecord> implementation by setting only LogRecord.State. For example:

private sealed class LogEnrichmentProcessor : BaseProcessor<LogRecord>
{
    public override void OnEnd(LogRecord data)
    {
        if (data.State is IReadOnlyList<KeyValuePair<string, object?>> listOfKvp)
        {
            data.State = this.AddFields(listOfKvp);
        }
    }

    private IReadOnlyList<KeyValuePair<string, object?>> AddFields(IReadOnlyList<KeyValuePair<string, object?>> stateValues)
    {
        // Implementation not shown
    }
}

What happens?

Starting with OpenTelemetry v1.5.0 the SDK will automatically set Attributes \ StateValues if the logged TState implements IReadOnlyList or IEnumerable of KeyValuePair<string, object?>.

When the OnEnd code above executes, LogRecord.State & LogRecord.Attributes \ LogRecord.StateValues are in sync. But when it exits, they are out of sync.

Depending on the exporter(s) being used, data loss may occur:

  • If an exporter looks at Attributes \ StateValues first, it will export the original (prior to enrichment) attributes and the enriched attributes will be lost.

  • If an exporter looks at State first, everything should work fine and the enriched data will be exported.

Is there a fix?

  • Upgrade to an OpenTelemetry SDK >v1.7.0 which includes [sdk-logs] Keep LogRecord.State & LogRecord.Attributes in sync if either is updated by a log processor #5169 to keep State & Attributes \ StateValues in sync automatically.

  • Or update the log processor to do this:

         public override void OnEnd(LogRecord data)
         {
              if (data.State is IReadOnlyList<KeyValuePair<string, object>> listOfKvp)
              {
                  data.State = data.StateValues = this.AddFields(data, listOfKvp);
              }
         }

    Setting both State and StateValues (or Attributes) will work for all known SDK versions.

@CodeBlanch CodeBlanch added the pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package label Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package
Projects
None yet
Development

No branches or pull requests

1 participant