This repository was archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add log context help to build log plugin (#108)
- Loading branch information
Showing
3 changed files
with
115 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
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) | ||
} |
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,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) | ||
} | ||
} |
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,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 |