-
Notifications
You must be signed in to change notification settings - Fork 7
Add OTel-compliant span status to ETW DTO logging path #127
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 43 additions & 3 deletions
46
src/Microsoft.OpenTelemetry/.publicApi/PublicAPI.Unshipped.txt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Microsoft.OpenTelemetry/Agent365/Runtime/DTOs/Builders/SpanStatusBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using Azure; | ||
| using Microsoft.Agents.A365.Observability.Runtime.Tracing.Scopes; | ||
|
|
||
| namespace Microsoft.Agents.A365.Observability.Runtime.DTOs.Builders | ||
| { | ||
| /// <summary> | ||
| /// Builds an OpenTelemetry-compliant <see cref="SpanStatus"/> from an exception and, for error | ||
| /// statuses, records the <c>error.type</c> attribute on the span attributes. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Centralizes the exception-to-status mapping so that the ETW DTO logging path stays consistent | ||
| /// with the Activity-based scope path (<see cref="OpenTelemetryScope.RecordError(Exception)"/>). | ||
| /// </remarks> | ||
| public static class SpanStatusBuilder | ||
| { | ||
| /// <summary> | ||
| /// Creates a <see cref="SpanStatus"/> from an optional exception. | ||
| /// </summary> | ||
| /// <param name="error"> | ||
| /// The exception to record. When <c>null</c>, an <see cref="SpanStatusCode.Unset"/> status is | ||
| /// returned and any pre-existing <c>error.type</c> attribute is removed so it is never emitted | ||
| /// without an accompanying error status. | ||
| /// </param> | ||
| /// <param name="attributes"> | ||
| /// Optional span attributes dictionary. When an error is supplied and this is non-<c>null</c>, the | ||
| /// <c>error.type</c> attribute is written following OTel semantic conventions. | ||
| /// </param> | ||
| /// <returns> | ||
| /// An <see cref="SpanStatusCode.Error"/> status (with the exception message) when <paramref name="error"/> | ||
| /// is non-<c>null</c>; otherwise an <see cref="SpanStatusCode.Unset"/> status. | ||
| /// </returns> | ||
| public static SpanStatus FromError(Exception? error, IDictionary<string, object?>? attributes = null) | ||
| { | ||
| if (error == null) | ||
| { | ||
| // Ensure error.type is never emitted without an accompanying error status, even if a | ||
| // caller passed it through extraAttributes (it is not part of the reserved-key filter). | ||
| attributes?.Remove(OpenTelemetryConstants.ErrorTypeKey); | ||
| return new SpanStatus(SpanStatusCode.Unset); | ||
| } | ||
|
|
||
| // Mirrors OpenTelemetryScope.RecordError: prefer the HTTP status from a RequestFailedException, | ||
| // otherwise fall back to the exception's full type name. | ||
| var errorType = error is RequestFailedException requestFailed && requestFailed.Status != 0 | ||
| ? requestFailed.Status.ToString() | ||
| : error.GetType().FullName ?? "error"; | ||
|
|
||
| if (attributes != null) | ||
| { | ||
| attributes[OpenTelemetryConstants.ErrorTypeKey] = errorType; | ||
| } | ||
|
|
||
| return new SpanStatus(SpanStatusCode.Error, error.Message); | ||
| } | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/Microsoft.OpenTelemetry/Agent365/Runtime/DTOs/SpanStatus.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Agents.A365.Observability.Runtime.DTOs | ||
| { | ||
| /// <summary> | ||
| /// Represents an OpenTelemetry span status (<c>code</c> and optional <c>message</c>), | ||
| /// following the OTel specification where <see cref="SpanStatusCode.Unset"/> = 0, | ||
| /// <see cref="SpanStatusCode.Ok"/> = 1, and <see cref="SpanStatusCode.Error"/> = 2. | ||
| /// </summary> | ||
| public readonly struct SpanStatus | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="SpanStatus"/> struct. | ||
| /// </summary> | ||
| /// <param name="code">The status code. Defaults to <see cref="SpanStatusCode.Unset"/>.</param> | ||
| /// <param name="message">Optional status message. Per the OTel spec this is only meaningful for an error status.</param> | ||
| public SpanStatus(SpanStatusCode code = SpanStatusCode.Unset, string? message = null) | ||
| { | ||
| Code = code; | ||
| Message = message; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the status code (<see cref="SpanStatusCode.Unset"/>, <see cref="SpanStatusCode.Ok"/>, or <see cref="SpanStatusCode.Error"/>). | ||
| /// </summary> | ||
| public SpanStatusCode Code { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the optional status message. Only populated for an error status. | ||
| /// </summary> | ||
| public string? Message { get; } | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/Microsoft.OpenTelemetry/Agent365/Runtime/DTOs/SpanStatusCode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Agents.A365.Observability.Runtime.DTOs | ||
| { | ||
| /// <summary> | ||
| /// Represents the OpenTelemetry span status code as defined by the OTLP specification. | ||
| /// The numeric values map directly onto the OTLP <c>Status.StatusCode</c> enum and are | ||
| /// emitted as-is on the ETW telemetry payload. | ||
| /// </summary> | ||
| public enum SpanStatusCode | ||
| { | ||
| /// <summary> | ||
| /// The default status; the span has not been explicitly marked successful or failed. | ||
| /// Maps to OTLP <c>STATUS_CODE_UNSET</c>. | ||
| /// </summary> | ||
| Unset = 0, | ||
|
|
||
| /// <summary> | ||
| /// The operation completed successfully (explicitly set by the application). | ||
| /// Maps to OTLP <c>STATUS_CODE_OK</c>. | ||
| /// </summary> | ||
| Ok = 1, | ||
|
|
||
| /// <summary> | ||
| /// The operation failed. Maps to OTLP <c>STATUS_CODE_ERROR</c>. | ||
| /// </summary> | ||
| Error = 2, | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.