This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support use zap to transmit trace context to log (#20)
- Loading branch information
Showing
8 changed files
with
440 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,35 @@ | ||
# Go2sky with zap (v1.16.0) | ||
|
||
## Installation | ||
|
||
```bash | ||
go get -u github.com/SkyAPM/go2sky-plugins/zap | ||
``` | ||
|
||
## Usage | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
zapplugin "github.com/SkyAPM/go2sky-plugins/zap" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
logger := zap.NewExample() | ||
|
||
// You have two way to adopt | ||
// 1. Addition fields before logging | ||
logger.With(zapplugin.TraceContext(ctx)...).Info("test") | ||
|
||
// 2. Wrap logger and correlate context at logging | ||
logger = zapplugin.WrapWithContext(logger) | ||
logger.Info(ctx, "test") | ||
} | ||
``` | ||
|
||
[See more](example_zap_test.go). |
This file contains 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,19 @@ | ||
// Licensed to SkyAPM org under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. SkyAPM org licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
// Package zap is a plugin that can be transmit trace context to the log framework. | ||
package zap |
This file contains 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,43 @@ | ||
// Licensed to SkyAPM org under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. SkyAPM org licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package zap_test | ||
|
||
import ( | ||
"context" | ||
|
||
zapplugin "github.com/SkyAPM/go2sky-plugins/zap" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func ExampleTraceContext() { | ||
ctx := context.Background() | ||
|
||
logger := zap.NewExample() | ||
logger.With(zapplugin.TraceContext(ctx)...).Info("test") | ||
// Output: | ||
// {"level":"info","msg":"test","SW_CTX":"[,,N/A,N/A,-1]"} | ||
} | ||
|
||
func ExampleWrapWithContext() { | ||
ctx := context.Background() | ||
|
||
logger := zapplugin.WrapWithContext(zap.NewExample()) | ||
logger.Info(ctx, "test") | ||
// Output: | ||
// {"level":"info","msg":"test","SW_CTX":"[,,N/A,N/A,-1]"} | ||
} |
This file contains 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,9 @@ | ||
module github.com/SkyAPM/go2sky-plugins/zap | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/SkyAPM/go2sky v1.0.1-0.20210518045634-6c4fc8fa5f1e | ||
go.uber.org/zap v1.16.0 | ||
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,113 @@ | ||
// Licensed to SkyAPM org under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. SkyAPM org licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package zap | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// WrapLogger is wrap the zap logger, also contains all method at zap logger, correlate the context before logging | ||
type WrapLogger struct { | ||
base *zap.Logger | ||
} | ||
|
||
// WrapWithContext original zap logger(not sugar) | ||
func WrapWithContext(logger *zap.Logger) *WrapLogger { | ||
return &WrapLogger{logger} | ||
} | ||
|
||
// Named wrap the zap logging function | ||
func (log *WrapLogger) Named(s string) *WrapLogger { | ||
return WrapWithContext(log.base.Named(s)) | ||
} | ||
|
||
// WithOptions wrap the zap logging function | ||
func (log *WrapLogger) WithOptions(opts ...zap.Option) *WrapLogger { | ||
return WrapWithContext(log.base.WithOptions(opts...)) | ||
} | ||
|
||
// With wrap the zap logging function | ||
func (log *WrapLogger) With(fields ...zap.Field) *WrapLogger { | ||
return WrapWithContext(log.base.With(fields...)) | ||
} | ||
|
||
// Check wrap the zap logging function | ||
func (log *WrapLogger) Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry { | ||
return log.base.Check(lvl, msg) | ||
} | ||
|
||
// Debug wrap the zap logging function and relate the context | ||
func (log *WrapLogger) Debug(ctx context.Context, msg string, fields ...zap.Field) { | ||
log.appendContextField(ctx, &fields) | ||
log.base.Debug(msg, fields...) | ||
} | ||
|
||
// Info wrap the zap logging function and relate the context | ||
func (log *WrapLogger) Info(ctx context.Context, msg string, fields ...zap.Field) { | ||
log.appendContextField(ctx, &fields) | ||
log.base.Info(msg, fields...) | ||
} | ||
|
||
// Warn wrap the zap logging function and relate the context | ||
func (log *WrapLogger) Warn(ctx context.Context, msg string, fields ...zap.Field) { | ||
log.appendContextField(ctx, &fields) | ||
log.base.Warn(msg, fields...) | ||
} | ||
|
||
// Error wrap the zap logging function and relate the context | ||
func (log *WrapLogger) Error(ctx context.Context, msg string, fields ...zap.Field) { | ||
log.appendContextField(ctx, &fields) | ||
log.base.Error(msg, fields...) | ||
} | ||
|
||
// DPanic wrap the zap logging function and relate the context | ||
func (log *WrapLogger) DPanic(ctx context.Context, msg string, fields ...zap.Field) { | ||
log.appendContextField(ctx, &fields) | ||
log.base.DPanic(msg, fields...) | ||
} | ||
|
||
// Panic wrap the zap logging function and relate the context | ||
func (log *WrapLogger) Panic(ctx context.Context, msg string, fields ...zap.Field) { | ||
log.appendContextField(ctx, &fields) | ||
log.base.Panic(msg, fields...) | ||
} | ||
|
||
// Fatal wrap the zap logging function and relate the context | ||
func (log *WrapLogger) Fatal(ctx context.Context, msg string, fields ...zap.Field) { | ||
log.appendContextField(ctx, &fields) | ||
log.base.Fatal(msg, fields...) | ||
} | ||
|
||
// Sync wrap the zap logging function | ||
func (log *WrapLogger) Sync() error { | ||
return log.base.Sync() | ||
} | ||
|
||
// Core wrap the zap logging function | ||
func (log *WrapLogger) Core() zapcore.Core { | ||
return log.base.Core() | ||
} | ||
|
||
func (log *WrapLogger) appendContextField(ctx context.Context, fields *[]zap.Field) { | ||
if ctx != nil { | ||
*fields = append(*fields, TraceContext(ctx)...) | ||
} | ||
} |
This file contains 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,34 @@ | ||
// Licensed to SkyAPM org under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. SkyAPM org licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package zap | ||
|
||
import ( | ||
"github.com/SkyAPM/go2sky/log" | ||
"go.uber.org/zap" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
// HeaderKeyCtx export zap field key name | ||
const HeaderKeyCtx = "SW_CTX" | ||
|
||
// TraceContext build all SkyWalking Trace Context to zap fields | ||
func TraceContext(ctx context.Context) []zap.Field { | ||
return []zap.Field{ | ||
zap.String(HeaderKeyCtx, log.FromContext(ctx).String()), | ||
} | ||
} |