-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Extend Governor APIs] Correlation ID (#93)
* add correlation ID * add correlation ID and trace * fix test * leave trace config to user with http Transport * undo request options * Add correlation ID context injection and extraction functions
- Loading branch information
Showing
7 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,25 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
events "github.com/metal-toolbox/governor-api/pkg/events/v1alpha1" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (r *Router) mwContextInjectCorrelationID(c *gin.Context) { | ||
var correlationID string | ||
|
||
if cid := c.Request.Header.Get(events.GovernorEventCorrelationIDHeader); cid != "" { | ||
correlationID = cid | ||
} else { | ||
correlationID = uuid.New().String() | ||
} | ||
|
||
r.Logger.Debug("mwCorrelationID", zap.String("correlationID", correlationID)) | ||
|
||
c.Request = c.Request.WithContext(events.InjectCorrelationID( | ||
c.Request.Context(), | ||
correlationID, | ||
)) | ||
} |
This file contains 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 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 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,22 @@ | ||
package v1alpha1 | ||
|
||
import "context" | ||
|
||
type contextKey struct{} | ||
|
||
// GovernorEventCorrelationIDContextKey is the context key for the correlation ID | ||
var governorEventCorrelationIDContextKey = &contextKey{} | ||
|
||
// InjectCorrelationID injects the correlation ID into the context | ||
func InjectCorrelationID(ctx context.Context, correlationID string) context.Context { | ||
return context.WithValue(ctx, governorEventCorrelationIDContextKey, correlationID) | ||
} | ||
|
||
// ExtractCorrelationID extracts the correlation ID from the context | ||
func ExtractCorrelationID(ctx context.Context) string { | ||
if cid, ok := ctx.Value(governorEventCorrelationIDContextKey).(string); ok { | ||
return cid | ||
} | ||
|
||
return "" | ||
} |
This file contains 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