Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Support use zap to transmit trace context to log (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu authored May 27, 2021
1 parent 91ed112 commit 81ed892
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ The plugins of [go2sky](https://github.com/SkyAPM/go2sky)

### Log Plugins
1. [logrus](logrus/README.md)
1. [zap](zap/README.md)
35 changes: 35 additions & 0 deletions zap/README.md
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).
19 changes: 19 additions & 0 deletions zap/doc.go
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
43 changes: 43 additions & 0 deletions zap/example_zap_test.go
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]"}
}
9 changes: 9 additions & 0 deletions zap/go.mod
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
)
186 changes: 186 additions & 0 deletions zap/go.sum

Large diffs are not rendered by default.

113 changes: 113 additions & 0 deletions zap/wrapperd_logger.go
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)...)
}
}
34 changes: 34 additions & 0 deletions zap/zap.go
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()),
}
}

0 comments on commit 81ed892

Please sign in to comment.