Skip to content

[logging] Enable storing/extracing of logger from context #16658

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 8 commits into from
Mar 9, 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
24 changes: 24 additions & 0 deletions components/common-go/log/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 (
"context"

"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/sirupsen/logrus"
)

func Extract(ctx context.Context) *logrus.Entry {
return ctxlogrus.Extract(ctx)
}

func AddFields(ctx context.Context, fields logrus.Fields) {
ctxlogrus.AddFields(ctx, fields)
}

func ToContext(ctx context.Context, entry *logrus.Entry) context.Context {
return ctxlogrus.ToContext(ctx, entry)
}
37 changes: 37 additions & 0 deletions components/common-go/log/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 (
"context"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)

func TestExtract_GracefulWithoutEntryOnContext(t *testing.T) {
require.NotNil(t, Extract(context.Background()))
}

func TestExtract(t *testing.T) {
ctx := context.Background()

require.NotNil(t, Extract(context.Background()), "graceful without a context logger")

withContextLogger := ToContext(ctx, Log)
require.Equal(t, Log, Extract(withContextLogger))
}

func TestAddFields(t *testing.T) {
ctx := ToContext(context.Background(), Log)
fields := logrus.Fields{
"some-field": "value",
}
AddFields(ctx, fields)

entry := Extract(ctx)
require.Equal(t, entry.Data, fields)
}
5 changes: 4 additions & 1 deletion components/ee/agent-smith/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
github.com/imdario/mergo v0.3.5 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand All @@ -58,11 +59,13 @@ require (
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
google.golang.org/grpc v1.52.3 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
14 changes: 13 additions & 1 deletion components/ee/agent-smith/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/gitpod-cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
)

require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.3 // indirect
Expand Down
Loading