Skip to content

[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 5 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions components/common-go/log/fields.go
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 {
Copy link
Member Author

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.

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}
}
30 changes: 30 additions & 0 deletions components/common-go/log/fields_test.go
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)
}
67 changes: 1 addition & 66 deletions components/common-go/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,69 +19,6 @@ import (
log "github.com/sirupsen/logrus"
)

const (
// OwnerField is the log field name of a workspace owner
OwnerField = "userId"
// WorkspaceField is the log field name of a workspace ID (not instance ID)
WorkspaceField = "workspaceId"
// InstanceField is the log field name of a workspace instance ID
InstanceField = "instanceId"
// ProjectField is the log field name of the project
ProjectField = "projectId"
// TeamField is the log field name of the team
TeamField = "teamId"
)

// 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{
OwnerField: owner,
WorkspaceField: workspace,
InstanceField: 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(owner, workspace, instance, project, team string) log.Fields {
logFields := log.Fields{}

if owner != "" {
logFields[OwnerField] = owner
}

if workspace != "" {
logFields[WorkspaceField] = workspace
}

if instance != "" {
logFields[InstanceField] = instance
}

if project != "" {
logFields[ProjectField] = project
}

if team != "" {
logFields[TeamField] = team
}

return logFields
}

// 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"`
}

// Log is the application wide console logger
var Log = log.WithFields(log.Fields{})

Expand Down Expand Up @@ -111,9 +48,7 @@ func logLevelFromEnv() {

// Init initializes/configures the application-wide logger
func Init(service, version string, json, verbose bool) {
Log = log.WithFields(log.Fields{
"serviceContext": ServiceContext{service, version},
})
Log = log.WithFields(ServiceContext(service, version))
log.SetReportCaller(true)

if json {
Expand Down
2 changes: 1 addition & 1 deletion components/common-go/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func FromContext(ctx context.Context, name string) (opentracing.Span, context.Co

// ApplyOWI sets the owner, workspace and instance tags on a span
func ApplyOWI(span opentracing.Span, owi logrus.Fields) {
for _, k := range []string{log.OwnerField, log.WorkspaceField, log.InstanceField, log.ProjectField, log.TeamField} {
for _, k := range []string{log.OwnerIDField, log.WorkspaceIDField, log.WorkspaceInstanceIDField, log.ProjectIDField, log.TeamIDField} {
val, ok := owi[k]
if !ok {
continue
Expand Down
12 changes: 5 additions & 7 deletions components/public-api-server/pkg/apiv1/identityprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"fmt"

connect "github.com/bufbuild/connect-go"
"github.com/gitpod-io/gitpod/common-go/log"
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/zitadel/oidc/pkg/oidc"
)

Expand Down Expand Up @@ -47,8 +47,6 @@ func (srv *IdentityProviderService) GetIDToken(ctx context.Context, req *connect
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Must have at least one audience entry"))
}

logger := ctxlogrus.Extract(ctx).WithField("workspace_id", workspaceID)

conn, err := getConnection(ctx, srv.connectionPool)
if err != nil {
return nil, err
Expand All @@ -62,18 +60,18 @@ func (srv *IdentityProviderService) GetIDToken(ctx context.Context, req *connect

workspace, err := conn.GetWorkspace(ctx, workspaceID)
if err != nil {
logger.WithError(err).Error("Failed to get workspace.")
log.Extract(ctx).WithError(err).Error("Failed to get workspace.")
return nil, proxy.ConvertError(err)
}

user, err := conn.GetLoggedInUser(ctx)
if err != nil {
logger.WithError(err).Error("Failed to get calling user.")
log.Extract(ctx).WithError(err).Error("Failed to get calling user.")
return nil, proxy.ConvertError(err)
}

if workspace.Workspace == nil {
logger.WithError(err).Error("Server did not return a workspace.")
log.Extract(ctx).WithError(err).Error("Server did not return a workspace.")
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("workspace not found"))
}

Expand All @@ -84,7 +82,7 @@ func (srv *IdentityProviderService) GetIDToken(ctx context.Context, req *connect

token, err := srv.idTokenSource.IDToken(ctx, "gitpod", req.Msg.Audience, userInfo)
if err != nil {
logger.WithError(err).Error("Failed to produce ID token.")
log.Extract(ctx).WithError(err).Error("Failed to produce ID token.")
return nil, proxy.ConvertError(err)
}
return &connect.Response[v1.GetIDTokenResponse]{
Expand Down
9 changes: 2 additions & 7 deletions components/public-api-server/pkg/apiv1/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"
Expand Down Expand Up @@ -89,9 +88,7 @@ func (s *OIDCService) CreateClientConfig(ctx context.Context, req *connect.Reque
return nil, status.Errorf(codes.Internal, "Failed to store OIDC client config.")
}

log.AddFields(ctx, logrus.Fields{
"oidcClientConfigId": created.ID.String(),
})
log.AddFields(ctx, log.OIDCClientConfigID(created.ID.String()))

converted, err := dbOIDCClientConfigToAPI(created, s.cipher)
if err != nil {
Expand Down Expand Up @@ -247,9 +244,7 @@ func (s *OIDCService) getUser(ctx context.Context, conn protocol.APIInterface) (
return nil, uuid.Nil, proxy.ConvertError(err)
}

log.AddFields(ctx, logrus.Fields{
"userId": user.ID,
})
log.AddFields(ctx, log.UserID(user.ID))

if !s.isFeatureEnabled(ctx, conn, user) {
return nil, uuid.Nil, connect.NewError(connect.CodePermissionDenied, errors.New("This feature is currently in beta. If you would like to be part of the beta, please contact us."))
Expand Down
2 changes: 1 addition & 1 deletion components/public-api-server/pkg/apiv1/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (s *TeamService) DeleteTeamMember(ctx context.Context, req *connect.Request
}

func (s *TeamService) toTeamAPIResponse(ctx context.Context, conn protocol.APIInterface, team *protocol.Team) (*v1.Team, error) {
logger := log.Extract(ctx).WithField("teamId", team.ID)
logger := log.Extract(ctx).WithFields(log.OrganizationID(team.ID))
members, err := conn.GetTeamMembers(ctx, team.ID)
if err != nil {
logger.WithError(err).Error("Failed to get team members.")
Expand Down
5 changes: 1 addition & 4 deletions components/public-api-server/pkg/apiv1/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/gorm"
Expand Down Expand Up @@ -302,9 +301,7 @@ func (s *TokensService) getUser(ctx context.Context, conn protocol.APIInterface)
return nil, uuid.Nil, proxy.ConvertError(err)
}

log.AddFields(ctx, logrus.Fields{
"userId": user.ID,
})
log.AddFields(ctx, log.UserID(user.ID))

if !s.isFeatureEnabled(ctx, conn, user) {
return nil, uuid.Nil, connect.NewError(connect.CodePermissionDenied, errors.New("This feature is currently in beta. If you would like to be part of the beta, please contact us."))
Expand Down
5 changes: 1 addition & 4 deletions components/public-api-server/pkg/apiv1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
"github.com/sirupsen/logrus"
)

func NewUserService(pool proxy.ServerConnectionPool) *UserService {
Expand All @@ -40,9 +39,7 @@ func (s *UserService) GetAuthenticatedUser(ctx context.Context, req *connect.Req
if err != nil {
return nil, proxy.ConvertError(err)
}
log.AddFields(ctx, logrus.Fields{
"userId": user.ID,
})
log.AddFields(ctx, log.UserID(user.ID))

response := userToAPIResponse(user)

Expand Down
Loading