Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/common-go/log/fields.go
2498 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package log
6
7
import log "github.com/sirupsen/logrus"
8
9
const (
10
UserIDField = "userId"
11
// OwnerIDField is the log field name of a workspace owner
12
OwnerIDField = UserIDField
13
// WorkspaceIDField is the log field name of a workspace ID (not instance ID)
14
WorkspaceIDField = "workspaceId"
15
// WorkspaceInstanceIDField is the log field name of a workspace instance ID
16
WorkspaceInstanceIDField = "instanceId"
17
// ProjectIDField is the log field name of the project
18
ProjectIDField = "projectId"
19
// TeamIDField is the log field name of the team
20
TeamIDField = "teamId"
21
22
OrganizationIDField = "orgId"
23
24
ServiceContextField = "serviceContext"
25
PersonalAccessTokenIDField = "patId"
26
OIDCClientConfigIDField = "oidcClientConfigId"
27
)
28
29
// OWI builds a structure meant for logrus which contains the owner, workspace and instance.
30
// Beware that this refers to the terminology outside of wsman which maps like:
31
//
32
// owner = owner, workspace = metaID, instance = workspaceID
33
func OWI(owner, workspace, instance string) log.Fields {
34
return log.Fields{
35
OwnerIDField: owner,
36
WorkspaceIDField: workspace,
37
WorkspaceInstanceIDField: instance,
38
}
39
}
40
41
// LogContext builds a structure meant for logrus which contains the owner, workspace and instance.
42
// Beware that this refers to the terminology outside of wsman which maps like:
43
//
44
// owner = owner, workspace = metaID, instance = workspaceID
45
func LogContext(ownerID, workspaceID, instanceID, projectID, orgID string) log.Fields {
46
return Compose(
47
WorkspaceOwner(ownerID),
48
WorkspaceID(workspaceID),
49
WorkspaceInstanceID(instanceID),
50
ProjectID(projectID),
51
OrganizationID(orgID),
52
)
53
}
54
55
func WorkspaceOwner(owner string) log.Fields {
56
return String(OwnerIDField, owner)
57
}
58
59
func WorkspaceID(workspaceID string) log.Fields {
60
return String(WorkspaceIDField, workspaceID)
61
}
62
63
func WorkspaceInstanceID(instanceID string) log.Fields {
64
return String(WorkspaceInstanceIDField, instanceID)
65
}
66
67
func ProjectID(projectID string) log.Fields {
68
return String(ProjectIDField, projectID)
69
}
70
71
func OrganizationID(orgID string) log.Fields {
72
return Compose(
73
// We continue to log TeamID in place of Organization for compatibility.
74
String(TeamIDField, orgID),
75
String(OrganizationIDField, orgID),
76
)
77
}
78
79
func PersonalAccessTokenID(patID string) log.Fields {
80
return String(PersonalAccessTokenIDField, patID)
81
}
82
83
func OIDCClientConfigID(id string) log.Fields {
84
return String(OIDCClientConfigIDField, id)
85
}
86
87
func UserID(userID string) log.Fields {
88
return String(UserIDField, userID)
89
}
90
91
// Compose composes multiple sets of log.Fields into a single
92
func Compose(fields ...log.Fields) log.Fields {
93
res := log.Fields{}
94
for _, f := range fields {
95
for key, val := range f {
96
res[key] = val
97
}
98
}
99
return res
100
}
101
102
// ServiceContext is the shape required for proper error logging in the GCP context.
103
// See https://cloud.google.com/error-reporting/reference/rest/v1beta1/ServiceContext
104
// Note that we musn't set resourceType for reporting errors.
105
type serviceContext struct {
106
Service string `json:"service"`
107
Version string `json:"version"`
108
}
109
110
func ServiceContext(service, version string) log.Fields {
111
return log.Fields{
112
ServiceContextField: serviceContext{service, version},
113
}
114
}
115
116
func String(key, value string) log.Fields {
117
return log.Fields{key: value}
118
}
119
120