-
Notifications
You must be signed in to change notification settings - Fork 72
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
frontend: refactor metrics #1413
Merged
Merged
Changes from all commits
Commits
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 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
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 |
---|---|---|
|
@@ -15,17 +15,43 @@ import ( | |
|
||
type ContextError struct { | ||
got any | ||
key contextKey | ||
} | ||
|
||
func (c *ContextError) Error() string { | ||
return fmt.Sprintf( | ||
"error retrieving value from context, value obtained was '%v' and type obtained was '%T'", | ||
"error retrieving value for key %q from context, value obtained was '%v' and type obtained was '%T'", | ||
c.key, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. |
||
c.got, | ||
c.got) | ||
} | ||
|
||
type contextKey int | ||
|
||
func (c contextKey) String() string { | ||
switch c { | ||
case contextKeyOriginalPath: | ||
return "originalPath" | ||
case contextKeyBody: | ||
return "body" | ||
case contextKeyLogger: | ||
return "logger" | ||
case contextKeyVersion: | ||
return "version" | ||
case contextKeyDBClient: | ||
return "dbClient" | ||
case contextKeyResourceID: | ||
return "resourceID" | ||
case contextKeyCorrelationData: | ||
return "correlationData" | ||
case contextKeySystemData: | ||
return "systemData" | ||
case contextKeyPattern: | ||
return "pattern" | ||
} | ||
return "<unknown>" | ||
} | ||
|
||
const ( | ||
// Keys for request-scoped data in http.Request contexts | ||
contextKeyOriginalPath contextKey = iota | ||
|
@@ -36,6 +62,7 @@ const ( | |
contextKeyResourceID | ||
contextKeyCorrelationData | ||
contextKeySystemData | ||
contextKeyPattern | ||
) | ||
|
||
func ContextWithOriginalPath(ctx context.Context, originalPath string) context.Context { | ||
|
@@ -47,6 +74,7 @@ func OriginalPathFromContext(ctx context.Context) (string, error) { | |
if !ok { | ||
err := &ContextError{ | ||
got: originalPath, | ||
key: contextKeyOriginalPath, | ||
} | ||
return originalPath, err | ||
} | ||
|
@@ -62,6 +90,7 @@ func BodyFromContext(ctx context.Context) ([]byte, error) { | |
if !ok { | ||
err := &ContextError{ | ||
got: body, | ||
key: contextKeyBody, | ||
} | ||
return body, err | ||
} | ||
|
@@ -77,6 +106,7 @@ func LoggerFromContext(ctx context.Context) *slog.Logger { | |
if !ok { | ||
err := &ContextError{ | ||
got: logger, | ||
key: contextKeyLogger, | ||
} | ||
// Return the default logger as a fail-safe, but log | ||
// the failure to obtain the logger from the context. | ||
|
@@ -95,6 +125,7 @@ func VersionFromContext(ctx context.Context) (api.Version, error) { | |
if !ok { | ||
err := &ContextError{ | ||
got: version, | ||
key: contextKeyVersion, | ||
} | ||
return version, err | ||
} | ||
|
@@ -110,6 +141,7 @@ func DBClientFromContext(ctx context.Context) (database.DBClient, error) { | |
if !ok { | ||
err := &ContextError{ | ||
got: dbClient, | ||
key: contextKeyDBClient, | ||
} | ||
return dbClient, err | ||
} | ||
|
@@ -125,6 +157,7 @@ func ResourceIDFromContext(ctx context.Context) (*azcorearm.ResourceID, error) { | |
if !ok { | ||
err := &ContextError{ | ||
got: resourceID, | ||
key: contextKeyResourceID, | ||
} | ||
return resourceID, err | ||
} | ||
|
@@ -140,6 +173,7 @@ func CorrelationDataFromContext(ctx context.Context) (*arm.CorrelationData, erro | |
if !ok { | ||
err := &ContextError{ | ||
got: correlationData, | ||
key: contextKeyCorrelationData, | ||
} | ||
return correlationData, err | ||
} | ||
|
@@ -155,8 +189,18 @@ func SystemDataFromContext(ctx context.Context) (*arm.SystemData, error) { | |
if !ok { | ||
err := &ContextError{ | ||
got: systemData, | ||
key: contextKeySystemData, | ||
} | ||
return systemData, err | ||
} | ||
return systemData, nil | ||
} | ||
|
||
func ContextWithPattern(ctx context.Context, pattern *string) context.Context { | ||
return context.WithValue(ctx, contextKeyPattern, pattern) | ||
} | ||
|
||
func PatternFromContext(ctx context.Context) *string { | ||
pattern, _ := ctx.Value(contextKeyPattern).(*string) | ||
return pattern | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Appreciate the refactoring of the context API. I won't ask for it this time but in the future it would be good to split refactoring into one or more focused commits for easier review. There's a lot packed into this one commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I got a bit too enthusiastic here.