-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add function to set correlation ID
Signed-off-by: Bob Stasyszyn <[email protected]>
- Loading branch information
1 parent
4f33656
commit 59a4ff9
Showing
3 changed files
with
112 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright Gen Digital Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package correlationid | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"go.opentelemetry.io/otel/trace" | ||
|
||
"github.com/trustbloc/logutil-go/pkg/otel/api" | ||
) | ||
|
||
const ( | ||
nilTraceID = "00000000000000000000000000000000" | ||
correlationIDLength = 8 | ||
) | ||
|
||
type contextKey struct{} | ||
|
||
// Set derives the correlation ID from the OpenTelemetry trace ID and sets it on the returned context. | ||
// If no trace ID is available, a random correlation ID is generated. | ||
func Set(ctx context.Context) (context.Context, string, error) { | ||
var correlationID string | ||
|
||
traceID := trace.SpanFromContext(ctx).SpanContext().TraceID().String() | ||
if traceID != "" && traceID != nilTraceID { | ||
correlationID = deriveID(traceID) | ||
} else { | ||
var err error | ||
correlationID, err = generateID() | ||
if err != nil { | ||
return nil, "", fmt.Errorf("generate correlation ID: %w", err) | ||
} | ||
} | ||
|
||
return context.WithValue(ctx, contextKey{}, correlationID), correlationID, nil | ||
} | ||
|
||
// Transport is an HTTP RoundTripper that adds a correlation ID to the request header. | ||
type Transport struct { | ||
defaultTransport http.RoundTripper | ||
} | ||
|
||
// NewHTTPTransport creates a new HTTP Transport. | ||
func NewHTTPTransport(defaultTransport http.RoundTripper) *Transport { | ||
return &Transport{ | ||
defaultTransport: defaultTransport, | ||
} | ||
} | ||
|
||
// RoundTrip executes a single HTTP transaction. | ||
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
correlationID, ok := req.Context().Value(contextKey{}).(string) | ||
if ok { | ||
req = req.Clone(req.Context()) | ||
req.Header.Add(api.CorrelationIDHeader, correlationID) | ||
} | ||
|
||
return t.defaultTransport.RoundTrip(req) | ||
} | ||
|
||
func generateID() (string, error) { | ||
bytes := make([]byte, correlationIDLength/2) //nolint:gomnd | ||
|
||
if _, err := rand.Read(bytes); err != nil { | ||
return "", err | ||
} | ||
|
||
return strings.ToUpper(hex.EncodeToString(bytes)), nil | ||
} | ||
|
||
func deriveID(id string) string { | ||
hash := sha256.Sum256([]byte(id)) | ||
|
||
return strings.ToUpper(hex.EncodeToString(hash[:correlationIDLength/2])) //nolint:gomnd | ||
} |
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 was deleted.
Oops, something went wrong.