Skip to content

feat: add toolkit logging impl #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/plugin-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ jobs:
- grpc
- irisv12
- trace-activation
- logging-activation
- fasthttp
- discard-reporter
- fiber
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Release Notes.
#### Features

* support attaching events to span in the toolkit.
* support record log entry in the toolkit.

#### Plugins

Expand Down
4 changes: 3 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use (
./plugins/mux
./plugins/grpc
./plugins/irisv12
./plugins/trace-activation
./plugins/toolkit-activation
./plugins/fasthttp
./plugins/fiber
./plugins/echov4
Expand Down Expand Up @@ -64,6 +64,8 @@ use (
./test/plugins/scenarios/segmentio-kafka
./test/plugins/scenarios/go-elasticsearchv8

./test/plugins/scenarios/logging-activation

./tools/go-agent

./toolkit
Expand Down
91 changes: 89 additions & 2 deletions plugins/core/logreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
package core

import (
"fmt"
"time"

"github.com/apache/skywalking-go/plugins/core/operator"

commonv3 "skywalking.apache.org/repo/goapi/collect/common/v3"
logv3 "skywalking.apache.org/repo/goapi/collect/logging/v3"
)
Expand All @@ -37,6 +40,8 @@ type logTracingContext interface {
GetEndPointName() string
}

var noopContext = &NoopSpan{}

func (t *Tracer) ReportLog(ctx, timeObj interface{}, level, msg string, labels map[string]string) {
tracingContext, ok := ctx.(logTracingContext)
if !ok || tracingContext == nil {
Expand All @@ -46,7 +51,12 @@ func (t *Tracer) ReportLog(ctx, timeObj interface{}, level, msg string, labels m
if entity == nil {
return
}
timeData := timeObj.(time.Time)
timeData, ok := timeObj.(time.Time)
if !ok {
// as a fallback strategy to solve some plugins that
// cannot be introduced into the standard library
timeData = time.Now()
}

tags := &logv3.LogTags{
Data: []*commonv3.KeyStringValuePair{
Expand All @@ -62,7 +72,6 @@ func (t *Tracer) ReportLog(ctx, timeObj interface{}, level, msg string, labels m
Value: v,
})
}

logData := &logv3.LogData{
Timestamp: Millisecond(timeData),
Service: tracingContext.GetServiceName(),
Expand All @@ -85,3 +94,81 @@ func (t *Tracer) ReportLog(ctx, timeObj interface{}, level, msg string, labels m

t.Reporter.SendLog(logData)
}

func (t *Tracer) GetLogContext(withEndpoint bool) interface{} {
var (
serviceName string
instanceName string
endpoint string

activeSpan TracingSpan = noopContext
)

if s, ok := t.ActiveSpan().(TracingSpan); ok && s != nil {
activeSpan = s
if withEndpoint {
endpoint = findEndpointNameBySpan(s)
}
}
entity := t.Entity()
if e, ok := entity.(operator.Entity); ok && e != nil {
serviceName, instanceName = e.GetServiceName(), e.GetInstanceName()
}
return &SkyWalkingLogContext{
ServiceName: serviceName,
InstanceName: instanceName,
TraceID: activeSpan.GetTraceID(),
TraceSegmentID: activeSpan.GetSegmentID(),
SpanID: activeSpan.GetSpanID(),
EndPoint: endpoint,
}
}

func findEndpointNameBySpan(s TracingSpan) string {
tmp := s
for tmp != nil {
if name := tmp.GetOperationName(); name != "" {
return name
}
tmp = tmp.ParentSpan()
}
return ""
}

type SkyWalkingLogContext struct {
ServiceName string
InstanceName string
TraceID string
EndPoint string
TraceSegmentID string
SpanID int32
}

func (s *SkyWalkingLogContext) GetServiceName() string {
return s.ServiceName
}

func (s *SkyWalkingLogContext) GetInstanceName() string {
return s.InstanceName
}

func (s *SkyWalkingLogContext) GetTraceID() string {
return s.TraceID
}

func (s *SkyWalkingLogContext) GetTraceSegmentID() string {
return s.TraceSegmentID
}

func (s *SkyWalkingLogContext) GetSpanID() int32 {
return s.SpanID
}

func (s *SkyWalkingLogContext) GetEndPointName() string {
return s.EndPoint
}

func (s *SkyWalkingLogContext) String() string {
return fmt.Sprintf("[%s,%s,%s,%s,%d]", s.ServiceName, s.InstanceName,
s.TraceID, s.TraceSegmentID, s.SpanID)
}
1 change: 1 addition & 0 deletions plugins/core/operator/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ type LogOperator interface {

type LogReporter interface {
ReportLog(ctx, time interface{}, level, msg string, labels map[string]string)
GetLogContext(withEndpoint bool) interface{}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewInstrument() *Instrument {
}

func (i *Instrument) Name() string {
return "trace-activation"
return "toolkit-activation"
}

func (i *Instrument) BasePackage() string {
Expand All @@ -47,6 +47,17 @@ func (i *Instrument) VersionChecker(version string) bool {
}

func (i *Instrument) Points() []*instrument.Point {
var instPoints []*instrument.Point
// append toolkit/trace related enhancements Point
instPoints = append(instPoints, tracePoint()...)

// append toolkit/logging related enhancements Point
instPoints = append(instPoints, loggingPoint()...)

return instPoints
}

func tracePoint() []*instrument.Point {
return []*instrument.Point{
{
PackagePath: "trace", At: instrument.NewStructEnhance("SpanRef"),
Expand Down Expand Up @@ -138,6 +149,27 @@ func (i *Instrument) Points() []*instrument.Point {
}
}

func loggingPoint() []*instrument.Point {
return []*instrument.Point{
{
PackagePath: "logging", At: instrument.NewStaticMethodEnhance("Debug"),
Interceptor: "DebugEntryInterceptor",
},
{
PackagePath: "logging", At: instrument.NewStaticMethodEnhance("Info"),
Interceptor: "InfoEntryInterceptor",
},
{
PackagePath: "logging", At: instrument.NewStaticMethodEnhance("Warn"),
Interceptor: "WarnEntryInterceptor",
},
{
PackagePath: "logging", At: instrument.NewStaticMethodEnhance("Error"),
Interceptor: "ErrorEntryInterceptor",
},
}
}

func (i *Instrument) FS() *embed.FS {
return &fs
}
33 changes: 33 additions & 0 deletions plugins/toolkit-activation/logging/debug_entry_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) 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 log

import (
"github.com/apache/skywalking-go/plugins/core/operator"
)

type DebugEntryInterceptor struct{}

func (h *DebugEntryInterceptor) BeforeInvoke(invocation operator.Invocation) error {
sendLogEntry(debugLevel, invocation.Args()...)
return nil
}

func (h *DebugEntryInterceptor) AfterInvoke(_ operator.Invocation, _ ...interface{}) error {
return nil
}
31 changes: 31 additions & 0 deletions plugins/toolkit-activation/logging/error_entry_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) 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 log

import "github.com/apache/skywalking-go/plugins/core/operator"

type ErrorEntryInterceptor struct{}

func (h *ErrorEntryInterceptor) BeforeInvoke(invocation operator.Invocation) error {
sendLogEntry(errorLevel, invocation.Args()...)
return nil
}

func (h *ErrorEntryInterceptor) AfterInvoke(_ operator.Invocation, _ ...interface{}) error {
return nil
}
31 changes: 31 additions & 0 deletions plugins/toolkit-activation/logging/info_entry_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) 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 log

import "github.com/apache/skywalking-go/plugins/core/operator"

type InfoEntryInterceptor struct{}

func (h *InfoEntryInterceptor) BeforeInvoke(invocation operator.Invocation) error {
sendLogEntry(infoLevel, invocation.Args()...)
return nil
}

func (h *InfoEntryInterceptor) AfterInvoke(_ operator.Invocation, _ ...interface{}) error {
return nil
}
60 changes: 60 additions & 0 deletions plugins/toolkit-activation/logging/send_entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) 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 log

import (
"github.com/apache/skywalking-go/plugins/core/operator"
)

const (
debugLevel = "debug"
infoLevel = "info"
warnLevel = "warn"
errorLevel = "error"
)

func sendLogEntry(level string, args ...interface{}) {
if len(args) == 0 {
return
}
logReporter, ok := operator.GetOperator().LogReporter().(operator.LogReporter)
if !ok || logReporter == nil {
return
}

msg := args[0].(string)
labels := parseLabels(args[1])
logReporter.ReportLog(logReporter.GetLogContext(true), args[1], level, msg, labels)
}

// parseLabels parses multiple args into a map of labels
func parseLabels(args interface{}) map[string]string {
keyValues, ok := args.([]string)
if !ok || len(keyValues) < 2 {
return nil
}

ret := make(map[string]string)
for i := 0; i < len(keyValues); i += 2 {
v1 := keyValues[i]
v2 := keyValues[i+1]
ret[v1] = v2
}

return ret
}
Loading
Loading