Skip to content

Commit 13b3b01

Browse files
authored
[logging] Enable storing/extracing of logger from context (#16658)
* [logging] Enable storing/extracing of logger from context * Fix * Fix * Fix * Fix * Fix * Fix
1 parent eed3b30 commit 13b3b01

File tree

28 files changed

+858
-15
lines changed

28 files changed

+858
-15
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 (
8+
"context"
9+
10+
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
11+
"github.com/sirupsen/logrus"
12+
)
13+
14+
func Extract(ctx context.Context) *logrus.Entry {
15+
return ctxlogrus.Extract(ctx)
16+
}
17+
18+
func AddFields(ctx context.Context, fields logrus.Fields) {
19+
ctxlogrus.AddFields(ctx, fields)
20+
}
21+
22+
func ToContext(ctx context.Context, entry *logrus.Entry) context.Context {
23+
return ctxlogrus.ToContext(ctx, entry)
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 (
8+
"context"
9+
"testing"
10+
11+
"github.com/sirupsen/logrus"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestExtract_GracefulWithoutEntryOnContext(t *testing.T) {
16+
require.NotNil(t, Extract(context.Background()))
17+
}
18+
19+
func TestExtract(t *testing.T) {
20+
ctx := context.Background()
21+
22+
require.NotNil(t, Extract(context.Background()), "graceful without a context logger")
23+
24+
withContextLogger := ToContext(ctx, Log)
25+
require.Equal(t, Log, Extract(withContextLogger))
26+
}
27+
28+
func TestAddFields(t *testing.T) {
29+
ctx := ToContext(context.Background(), Log)
30+
fields := logrus.Fields{
31+
"some-field": "value",
32+
}
33+
AddFields(ctx, fields)
34+
35+
entry := Extract(ctx)
36+
require.Equal(t, entry.Data, fields)
37+
}

components/ee/agent-smith/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ require (
4141
github.com/google/gnostic v0.5.7-v3refs // indirect
4242
github.com/google/gofuzz v1.1.0 // indirect
4343
github.com/gorilla/websocket v1.5.0 // indirect
44+
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
4445
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
4546
github.com/imdario/mergo v0.3.5 // indirect
4647
github.com/inconshreveable/mousetrap v1.0.0 // indirect
@@ -58,11 +59,13 @@ require (
5859
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37 // indirect
5960
github.com/spf13/pflag v1.0.5 // indirect
6061
golang.org/x/net v0.4.0 // indirect
61-
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
62+
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
6263
golang.org/x/term v0.3.0 // indirect
6364
golang.org/x/text v0.5.0 // indirect
6465
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
6566
google.golang.org/appengine v1.6.7 // indirect
67+
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
68+
google.golang.org/grpc v1.52.3 // indirect
6669
google.golang.org/protobuf v1.28.1 // indirect
6770
gopkg.in/inf.v0 v0.9.1 // indirect
6871
gopkg.in/yaml.v2 v2.4.0 // indirect

components/ee/agent-smith/go.sum

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/gitpod-cli/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ require (
3030
)
3131

3232
require (
33+
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
3334
github.com/kr/text v0.2.0 // indirect
3435
github.com/mattn/go-colorable v0.0.9 // indirect
3536
github.com/mattn/go-isatty v0.0.3 // indirect

0 commit comments

Comments
 (0)