-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[common-go] Composable log fields #16860
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License.AGPL.txt in the project root for license information. | ||
|
||
package log | ||
|
||
import log "github.com/sirupsen/logrus" | ||
|
||
const ( | ||
UserIDField = "userId" | ||
// OwnerIDField is the log field name of a workspace owner | ||
OwnerIDField = UserIDField | ||
// WorkspaceIDField is the log field name of a workspace ID (not instance ID) | ||
WorkspaceIDField = "workspaceId" | ||
// WorkspaceInstanceIDField is the log field name of a workspace instance ID | ||
WorkspaceInstanceIDField = "instanceId" | ||
// ProjectIDField is the log field name of the project | ||
ProjectIDField = "projectId" | ||
// TeamIDField is the log field name of the team | ||
TeamIDField = "teamId" | ||
|
||
OrganizationIDField = "orgId" | ||
|
||
ServiceContextField = "serviceContext" | ||
PersonalAccessTokenIDField = "patId" | ||
OIDCClientConfigIDField = "oidcClientConfigId" | ||
) | ||
|
||
// OWI builds a structure meant for logrus which contains the owner, workspace and instance. | ||
// Beware that this refers to the terminology outside of wsman which maps like: | ||
// | ||
// owner = owner, workspace = metaID, instance = workspaceID | ||
func OWI(owner, workspace, instance string) log.Fields { | ||
return log.Fields{ | ||
OwnerIDField: owner, | ||
WorkspaceIDField: workspace, | ||
WorkspaceInstanceIDField: instance, | ||
} | ||
} | ||
|
||
// LogContext builds a structure meant for logrus which contains the owner, workspace and instance. | ||
// Beware that this refers to the terminology outside of wsman which maps like: | ||
// | ||
// owner = owner, workspace = metaID, instance = workspaceID | ||
func LogContext(ownerID, workspaceID, instanceID, projectID, orgID string) log.Fields { | ||
return Compose( | ||
WorkspaceOwner(ownerID), | ||
WorkspaceID(workspaceID), | ||
WorkspaceInstanceID(instanceID), | ||
ProjectID(projectID), | ||
OrganizationID(orgID), | ||
) | ||
} | ||
|
||
func WorkspaceOwner(owner string) log.Fields { | ||
return String(OwnerIDField, owner) | ||
} | ||
|
||
func WorkspaceID(workspaceID string) log.Fields { | ||
return String(WorkspaceIDField, workspaceID) | ||
} | ||
|
||
func WorkspaceInstanceID(instanceID string) log.Fields { | ||
return String(WorkspaceInstanceIDField, instanceID) | ||
} | ||
|
||
func ProjectID(projectID string) log.Fields { | ||
return String(ProjectIDField, projectID) | ||
} | ||
|
||
func OrganizationID(orgID string) log.Fields { | ||
return Compose( | ||
// We continue to log TeamID in place of Organization for compatibility. | ||
String(TeamIDField, orgID), | ||
String(OrganizationIDField, orgID), | ||
) | ||
} | ||
|
||
func PersonalAccessTokenID(patID string) log.Fields { | ||
return String(PersonalAccessTokenIDField, patID) | ||
} | ||
|
||
func OIDCClientConfigID(id string) log.Fields { | ||
return String(OIDCClientConfigIDField, id) | ||
} | ||
|
||
func UserID(userID string) log.Fields { | ||
return String(UserIDField, userID) | ||
} | ||
|
||
// Compose composes multiple sets of log.Fields into a single | ||
func Compose(fields ...log.Fields) log.Fields { | ||
res := log.Fields{} | ||
for _, f := range fields { | ||
for key, val := range f { | ||
res[key] = val | ||
} | ||
} | ||
return res | ||
} | ||
|
||
// ServiceContext is the shape required for proper error logging in the GCP context. | ||
// See https://cloud.google.com/error-reporting/reference/rest/v1beta1/ServiceContext | ||
// Note that we musn't set resourceType for reporting errors. | ||
type serviceContext struct { | ||
Service string `json:"service"` | ||
Version string `json:"version"` | ||
} | ||
|
||
func ServiceContext(service, version string) log.Fields { | ||
return log.Fields{ | ||
ServiceContextField: serviceContext{service, version}, | ||
} | ||
} | ||
|
||
func String(key, value string) log.Fields { | ||
return log.Fields{key: value} | ||
} |
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) 2023 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License.AGPL.txt in the project root for license information. | ||
|
||
package log | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCompose(t *testing.T) { | ||
fields := Compose( | ||
WorkspaceOwner("owner"), | ||
WorkspaceID("workspace"), | ||
WorkspaceInstanceID("instance"), | ||
ProjectID("project"), | ||
OrganizationID("org"), | ||
) | ||
require.Equal(t, logrus.Fields{ | ||
OwnerIDField: "owner", | ||
OrganizationIDField: "org", | ||
TeamIDField: "org", | ||
ProjectIDField: "project", | ||
WorkspaceInstanceIDField: "instance", | ||
WorkspaceIDField: "workspace", | ||
}, fields) | ||
} |
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
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.
These helpers allow us to control centrally the common fields, and be able to adjust the naming of the fields should we choose to do so.