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

Commit

Permalink
Add log context help to build log plugin (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu authored May 18, 2021
1 parent 6d93a02 commit 6c4fc8f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
48 changes: 48 additions & 0 deletions log/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Copyright 2021 SkyAPM org
//
// Licensed 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 log

import (
"context"
"fmt"

"github.com/SkyAPM/go2sky"
)

type SkyWalkingContext struct {
ServiceName string
ServiceInstanceName string
TraceID string
TraceSegmentID string
SpanID int32
}

// FromContext from context for logging
func FromContext(ctx context.Context) *SkyWalkingContext {
return &SkyWalkingContext{
ServiceName: go2sky.ServiceName(ctx),
ServiceInstanceName: go2sky.ServiceInstanceName(ctx),
TraceID: go2sky.TraceID(ctx),
TraceSegmentID: go2sky.TraceSegmentID(ctx),
SpanID: go2sky.SpanID(ctx),
}
}

func (context *SkyWalkingContext) String() string {
return fmt.Sprintf("[%s,%s,%s,%s,%d]", context.ServiceName, context.ServiceInstanceName,
context.TraceID, context.TraceSegmentID, context.SpanID)
}
47 changes: 47 additions & 0 deletions log/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Copyright 2021 SkyAPM org
//
// Licensed 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 log

import (
"context"
"fmt"
"testing"
)

func TestFromContext(t *testing.T) {
ctx := context.Background()
skyWalkingContext := FromContext(ctx)
verifyContext(t, skyWalkingContext)
}

func verifyContext(t *testing.T, ctx *SkyWalkingContext) {
if ctx == nil {
t.Error("nil context")
return
}

contextString := ctx.String()
if contextString == "" {
t.Error("empty context string")
}

exceptString := fmt.Sprintf("[%s,%s,%s,%s,%d]", ctx.ServiceName, ctx.ServiceInstanceName,
ctx.TraceID, ctx.TraceSegmentID, ctx.SpanID)
if contextString != exceptString {
t.Errorf("wrong context string, excepted:%s, actual:%s", exceptString, contextString)
}
}
20 changes: 20 additions & 0 deletions log/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright 2021 SkyAPM org
//
// Licensed 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 log help to build trace context data into log plugins.
*/
package log

0 comments on commit 6c4fc8f

Please sign in to comment.