-
Notifications
You must be signed in to change notification settings - Fork 264
Add distributed tracing and structured error handling for extensions #6321
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
JeffreyCA
merged 10 commits into
Azure:main
from
JeffreyCA:jeffreyca/6292/ext-improve-tracing
Dec 12, 2025
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3253c78
Add distributed tracing and structured error handling for extensions
JeffreyCA f39ee9b
Add tests
JeffreyCA a0b4908
Fix lint
JeffreyCA 3732a29
Propagate trace context to the extension process
JeffreyCA eea682a
Use gRPC metadata to pass trace context
JeffreyCA 2bca3ba
Fix cspell
JeffreyCA 18e6c34
Update tests
JeffreyCA db15050
Fix lint
JeffreyCA 4b0d846
Generalize errors
JeffreyCA 6f8d2de
Revert metadata changes
JeffreyCA 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package azdext | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
| ) | ||
|
|
||
| // ExtensionResponseError represents an HTTP response error returned from an extension over gRPC. | ||
| // It mirrors azcore.ResponseError and preserves structured error information for telemetry purposes. | ||
| type ExtensionResponseError struct { | ||
| // Message is the human-readable error message | ||
| Message string | ||
| // Details contains additional error details | ||
| Details string | ||
| // ErrorCode is the error code from the service (e.g., "Conflict", "NotFound") | ||
| ErrorCode string | ||
| // StatusCode is the HTTP status code (e.g., 409, 404, 500) | ||
| StatusCode int | ||
| // ServiceName is the service name for telemetry (e.g., "ai.azure.com") | ||
| ServiceName string | ||
| } | ||
|
|
||
| // Error implements the error interface | ||
| func (e *ExtensionResponseError) Error() string { | ||
| if e.Details != "" { | ||
| return fmt.Sprintf("%s: %s", e.Message, e.Details) | ||
| } | ||
| return e.Message | ||
| } | ||
|
|
||
| // HasServiceInfo returns true if the error contains service information for telemetry | ||
| func (e *ExtensionResponseError) HasServiceInfo() bool { | ||
| return e.StatusCode > 0 && e.ServiceName != "" | ||
| } | ||
|
|
||
| // errorMessage defines the common interface for protobuf error messages | ||
| // This allows us to write generic unwrap logic for any generated proto message | ||
| type errorMessage interface { | ||
| comparable | ||
| GetMessage() string | ||
| GetDetails() string | ||
| GetErrorCode() string | ||
| GetStatusCode() int32 | ||
| GetServiceName() string | ||
| } | ||
|
|
||
| // errorInfo is a helper struct to hold extracted error information | ||
| // before converting to a specific protobuf message type | ||
| type errorInfo struct { | ||
| message string | ||
| details string | ||
| errorCode string | ||
| statusCode int32 | ||
| service string | ||
| } | ||
|
|
||
| // captureErrorInfo extracts structured error information from a Go error. | ||
| // It handles nil errors, ExtensionResponseError, and azcore.ResponseError. | ||
| func captureErrorInfo(err error) errorInfo { | ||
| if err == nil { | ||
| return errorInfo{} | ||
| } | ||
|
|
||
| // Default to the error string | ||
| info := errorInfo{message: err.Error()} | ||
|
|
||
| // If it's already an ExtensionResponseError, preserve all fields including Details | ||
| var extErr *ExtensionResponseError | ||
| if errors.As(err, &extErr) { | ||
| info.message = extErr.Message | ||
| info.details = extErr.Details | ||
| info.errorCode = extErr.ErrorCode | ||
| //nolint:gosec // G115: HTTP status codes are well within int32 range | ||
| info.statusCode = int32(extErr.StatusCode) | ||
| info.service = extErr.ServiceName | ||
| return info | ||
| } | ||
|
|
||
| // Try to extract structured error information from Azure SDK errors | ||
| var respErr *azcore.ResponseError | ||
| if errors.As(err, &respErr) { | ||
| info.errorCode = respErr.ErrorCode | ||
| //nolint:gosec // G115: HTTP status codes are well within int32 range | ||
| info.statusCode = int32(respErr.StatusCode) | ||
| if respErr.RawResponse != nil && respErr.RawResponse.Request != nil { | ||
| info.service = respErr.RawResponse.Request.Host | ||
| } | ||
| } | ||
|
|
||
| return info | ||
| } | ||
|
|
||
| // WrapErrorForServiceTarget wraps a Go error into a ServiceTargetErrorMessage for transmission over gRPC. | ||
| func WrapErrorForServiceTarget(err error) *ServiceTargetErrorMessage { | ||
| info := captureErrorInfo(err) | ||
| if info.message == "" { | ||
| return nil | ||
| } | ||
|
|
||
| return &ServiceTargetErrorMessage{ | ||
| Message: info.message, | ||
| Details: info.details, | ||
| ErrorCode: info.errorCode, | ||
| StatusCode: info.statusCode, | ||
| ServiceName: info.service, | ||
| } | ||
| } | ||
|
|
||
| // WrapErrorForFrameworkService wraps a Go error into a FrameworkServiceErrorMessage for transmission over gRPC. | ||
| func WrapErrorForFrameworkService(err error) *FrameworkServiceErrorMessage { | ||
| info := captureErrorInfo(err) | ||
| if info.message == "" { | ||
| return nil | ||
| } | ||
|
|
||
| return &FrameworkServiceErrorMessage{ | ||
| Message: info.message, | ||
| Details: info.details, | ||
| ErrorCode: info.errorCode, | ||
| StatusCode: info.statusCode, | ||
| ServiceName: info.service, | ||
| } | ||
| } | ||
|
|
||
| // unwrapError is a generic helper to convert protobuf error messages back to Go errors | ||
| func unwrapError[T errorMessage](msg T) error { | ||
| var zero T | ||
| if msg == zero || msg.GetMessage() == "" { | ||
| return nil | ||
| } | ||
|
|
||
| return &ExtensionResponseError{ | ||
| Message: msg.GetMessage(), | ||
| Details: msg.GetDetails(), | ||
| ErrorCode: msg.GetErrorCode(), | ||
| StatusCode: int(msg.GetStatusCode()), | ||
| ServiceName: msg.GetServiceName(), | ||
| } | ||
| } | ||
|
|
||
| // UnwrapErrorFromServiceTarget converts a ServiceTargetErrorMessage back to a Go error. | ||
| func UnwrapErrorFromServiceTarget(msg *ServiceTargetErrorMessage) error { | ||
| return unwrapError(msg) | ||
| } | ||
|
|
||
| // UnwrapErrorFromFrameworkService converts a FrameworkServiceErrorMessage back to a Go error. | ||
| func UnwrapErrorFromFrameworkService(msg *FrameworkServiceErrorMessage) error { | ||
| return unwrapError(msg) | ||
| } |
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.