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

Commit 6c4fc8f

Browse files
authored
Add log context help to build log plugin (#108)
1 parent 6d93a02 commit 6c4fc8f

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

log/context.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Copyright 2021 SkyAPM org
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
package log
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
"github.com/SkyAPM/go2sky"
24+
)
25+
26+
type SkyWalkingContext struct {
27+
ServiceName string
28+
ServiceInstanceName string
29+
TraceID string
30+
TraceSegmentID string
31+
SpanID int32
32+
}
33+
34+
// FromContext from context for logging
35+
func FromContext(ctx context.Context) *SkyWalkingContext {
36+
return &SkyWalkingContext{
37+
ServiceName: go2sky.ServiceName(ctx),
38+
ServiceInstanceName: go2sky.ServiceInstanceName(ctx),
39+
TraceID: go2sky.TraceID(ctx),
40+
TraceSegmentID: go2sky.TraceSegmentID(ctx),
41+
SpanID: go2sky.SpanID(ctx),
42+
}
43+
}
44+
45+
func (context *SkyWalkingContext) String() string {
46+
return fmt.Sprintf("[%s,%s,%s,%s,%d]", context.ServiceName, context.ServiceInstanceName,
47+
context.TraceID, context.TraceSegmentID, context.SpanID)
48+
}

log/context_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Copyright 2021 SkyAPM org
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
package log
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"testing"
23+
)
24+
25+
func TestFromContext(t *testing.T) {
26+
ctx := context.Background()
27+
skyWalkingContext := FromContext(ctx)
28+
verifyContext(t, skyWalkingContext)
29+
}
30+
31+
func verifyContext(t *testing.T, ctx *SkyWalkingContext) {
32+
if ctx == nil {
33+
t.Error("nil context")
34+
return
35+
}
36+
37+
contextString := ctx.String()
38+
if contextString == "" {
39+
t.Error("empty context string")
40+
}
41+
42+
exceptString := fmt.Sprintf("[%s,%s,%s,%s,%d]", ctx.ServiceName, ctx.ServiceInstanceName,
43+
ctx.TraceID, ctx.TraceSegmentID, ctx.SpanID)
44+
if contextString != exceptString {
45+
t.Errorf("wrong context string, excepted:%s, actual:%s", exceptString, contextString)
46+
}
47+
}

log/doc.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Copyright 2021 SkyAPM org
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
/*
18+
Package log help to build trace context data into log plugins.
19+
*/
20+
package log

0 commit comments

Comments
 (0)