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

Commit

Permalink
Support use logrus to transmit trace context to log (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu authored May 19, 2021
1 parent 4cc0503 commit 91ed112
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- { name: 'Go micro', plugin_dir: 'micro', go_version: '1.14'}
- { name: 'Go resty', plugin_dir: 'resty', go_version: '1.12'}
- { name: 'Go restful', plugin_dir: 'go-restful', go_version: '1.13'}
- { name: 'Go logrus', plugin_dir: 'logrus', go_version: '1.12'}
steps:
- name: Set up Go ${{ matrix.case.go_version }}
uses: actions/setup-go@v2
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

The plugins of [go2sky](https://github.com/SkyAPM/go2sky)

### Plugin Summary
## Plugin Summary

### Trace Plugins
1. [http server & client](http/README.md)
1. [gin](gin/README.md)
1. [gear](gear/README.md)
1. [go-resty](resty/README.md)
1. [go-micro](micro/README.md)
1. [go-restful](go-restful/README.md)

### Log Plugins
1. [logrus](logrus/README.md)
33 changes: 33 additions & 0 deletions logrus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Go2sky with logrus (v1.8.1)

## Installation

```bash
go get -u github.com/SkyAPM/go2sky-plugins/logrus
```

## Usage

```go
package main

import (
"context"
"github.com/sirupsen/logrus"
logrusplugin "github.com/SkyAPM/go2sky-plugins/logrus"
)

func main() {
// init format with custom trace context key
// SW_CTX format: [$serviceName,$instanceName,$traceId,$traceSegmentId,$spanId]
logrus.SetFormatter(logrusplugin.Wrap(&logrus.JSONFormatter{}, "SW_CTX"}))

// init tracer

// log with context
ctx := context.Background()
logrus.WithContext(ctx).Info("test1")
}
```

[See more](example_logrus_test.go).
19 changes: 19 additions & 0 deletions logrus/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 logrus is a plugin that can be transmit trace context to the log framework.
package logrus
54 changes: 54 additions & 0 deletions logrus/example_logrus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 logrus

import (
"context"
"os"

"github.com/SkyAPM/go2sky"
"github.com/sirupsen/logrus"
)

func ExampleWrapFormat() {
context := context.Background()

logrus.SetOutput(os.Stdout)

// init tracer
_, err := go2sky.NewTracer("example")
if err != nil {
logrus.Fatalf("create tracer error %v \n", err)
}

// json format
logrus.SetFormatter(Wrap(&logrus.JSONFormatter{
DisableTimestamp: true,
}, "SW_CTX"))
logrus.WithContext(context).Info("test1")

// test format
logrus.SetFormatter(Wrap(&logrus.TextFormatter{
DisableTimestamp: true,
}, "SW_CTX"))
logrus.WithContext(context).Info("test2")

// Output:
// {"SW_CTX":"[,,N/A,N/A,-1]","level":"info","msg":"test1"}
// level=info msg=test2 SW_CTX="[,,N/A,N/A,-1]"
}
8 changes: 8 additions & 0 deletions logrus/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/SkyAPM/go2sky-plugins/logrus

go 1.12

require (
github.com/SkyAPM/go2sky v1.0.1-0.20210518045634-6c4fc8fa5f1e
github.com/sirupsen/logrus v1.8.1
)
48 changes: 48 additions & 0 deletions logrus/logrus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 logrus

import (
"github.com/SkyAPM/go2sky/log"
"github.com/sirupsen/logrus"
)

// WrapFormat is wrap format to transmit trace context when logging
type WrapFormat struct {
Base logrus.Formatter
traceContextKey string
}

// Wrap original format
func Wrap(base logrus.Formatter, contextKey string) *WrapFormat {
if contextKey == "" {
contextKey = "SW_CTX"
}

return &WrapFormat{base, contextKey}
}

// Format logging with trace context
func (format *WrapFormat) Format(entry *logrus.Entry) ([]byte, error) {
// append trace context
if entry.Context != nil {
entry.Data[format.traceContextKey] = log.FromContext(entry.Context).String()
}

return format.Base.Format(entry)
}

0 comments on commit 91ed112

Please sign in to comment.