-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #445 from TarsCloud/feature/lbbniu/opentelemetry
feat(opentelemetry): support opentelemetry traces、metrics、 logs
- Loading branch information
Showing
23 changed files
with
1,868 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,44 @@ | ||
module github.com/TarsCloud/TarsGo/contrib/log | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/TarsCloud/TarsGo v1.3.11-0.20230324025206-9c0cd0efd13e | ||
go.opentelemetry.io/otel/trace v1.14.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/gin-gonic/gin v1.8.1 // indirect | ||
github.com/go-logr/logr v1.2.3 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/go-playground/locales v0.14.0 // indirect | ||
github.com/go-playground/universal-translator v0.18.0 // indirect | ||
github.com/go-playground/validator/v10 v10.10.0 // indirect | ||
github.com/goccy/go-json v0.9.7 // indirect | ||
github.com/google/uuid v1.3.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/leodido/go-urn v1.2.1 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/ugorji/go/codec v1.2.7 // indirect | ||
go.opentelemetry.io/otel v1.14.0 // indirect | ||
go.uber.org/automaxprocs v1.5.1 // indirect | ||
golang.org/x/sys v0.5.0 // indirect | ||
golang.org/x/text v0.7.0 // indirect | ||
google.golang.org/protobuf v1.28.0 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) | ||
|
||
require ( | ||
github.com/stretchr/testify v1.8.2 | ||
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.14.0 | ||
go.opentelemetry.io/otel/sdk v1.14.0 | ||
golang.org/x/crypto v0.1.0 // indirect | ||
golang.org/x/net v0.7.0 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
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,77 @@ | ||
package log | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/TarsCloud/TarsGo/tars" | ||
) | ||
|
||
// GetLogger Get a logger | ||
func GetLogger(name string) *Logger { | ||
logPath, lg := getLogger(name) | ||
// if the default writer is not ConsoleWriter, the writer has already been configured | ||
if !lg.IsConsoleWriter() { | ||
return lg | ||
} | ||
if logPath == "" { | ||
return lg | ||
} | ||
cfg := tars.GetServerConfig() | ||
lg.SetFileRoller(logPath, int(cfg.LogNum), int(cfg.LogSize)) | ||
return lg | ||
} | ||
|
||
func getLogger(name string) (logPath string, lg *Logger) { | ||
cfg := tars.GetServerConfig() | ||
if cfg == nil { | ||
return "", GetCtxLogger(name) | ||
} | ||
if name == "" { | ||
name = cfg.App + "." + cfg.Server | ||
} else { | ||
name = cfg.App + "." + cfg.Server + "_" + name | ||
} | ||
logPath = filepath.Join(cfg.LogPath, cfg.App, cfg.Server) | ||
lg = GetCtxLogger(name) | ||
return logPath, lg | ||
} | ||
|
||
// GetDayLogger Get a logger roll by day | ||
func GetDayLogger(name string, numDay int) *Logger { | ||
logPath, lg := getLogger(name) | ||
// if the default writer is not ConsoleWriter, the writer has already been configured | ||
if !lg.IsConsoleWriter() { | ||
return lg | ||
} | ||
lg.SetDayRoller(logPath, numDay) | ||
return lg | ||
} | ||
|
||
// GetHourLogger Get a logger roll by hour | ||
func GetHourLogger(name string, numHour int) *Logger { | ||
logPath, lg := getLogger(name) | ||
// if the default writer is not ConsoleWriter, the writer has already been configured | ||
if !lg.IsConsoleWriter() { | ||
return lg | ||
} | ||
lg.SetHourRoller(logPath, numHour) | ||
return lg | ||
} | ||
|
||
// GetRemoteLogger returns a remote logger | ||
func GetRemoteLogger(name string) *Logger { | ||
cfg := tars.GetServerConfig() | ||
lg := GetCtxLogger(name) | ||
if !lg.IsConsoleWriter() { | ||
return lg | ||
} | ||
remoteWriter := tars.NewRemoteTimeWriter() | ||
var set string | ||
if cfg.Enableset { | ||
set = cfg.Setdivision | ||
} | ||
|
||
remoteWriter.InitServerInfo(cfg.App, cfg.Server, name, set) | ||
lg.SetWriter(remoteWriter) | ||
return lg | ||
} |
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,102 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/TarsCloud/TarsGo/tars/util/current" | ||
"github.com/TarsCloud/TarsGo/tars/util/rogger" | ||
|
||
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace" | ||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
const ( | ||
traceIDStr = "4bf92f3577b34da6a3ce929d0e0e4736" | ||
spanIDStr = "00f067aa0ba902b7" | ||
) | ||
|
||
var ( | ||
traceID = mustTraceIDFromHex(traceIDStr) | ||
spanID = mustSpanIDFromHex(spanIDStr) | ||
) | ||
|
||
func mustTraceIDFromHex(s string) (t trace.TraceID) { | ||
var err error | ||
t, err = trace.TraceIDFromHex(s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return | ||
} | ||
|
||
func mustSpanIDFromHex(s string) (t trace.SpanID) { | ||
var err error | ||
t, err = trace.SpanIDFromHex(s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return | ||
} | ||
|
||
func TestLog(t *testing.T) { | ||
rogger.SetLevel(rogger.DEBUG) | ||
ctx := context.Background() | ||
l := GetCtxLogger("log") | ||
defer l.Flush(ctx) | ||
l.SetPrefix("prefix") | ||
testCases := []struct { | ||
name string | ||
ctx func(t *testing.T) context.Context | ||
}{ | ||
{ | ||
name: "background", | ||
ctx: func(t *testing.T) context.Context { | ||
return context.Background() | ||
}, | ||
}, | ||
{ | ||
name: "opentelemetry", | ||
ctx: func(t *testing.T) context.Context { | ||
exporter, err := stdouttrace.New( | ||
// Use human-readable output. | ||
stdouttrace.WithPrettyPrint(), | ||
// Do not print timestamps for the demo. | ||
stdouttrace.WithoutTimestamps(), | ||
) | ||
require.NoError(t, err) | ||
tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter)) | ||
otelCtx, _ := tp.Tracer("opentelemetry").Start(context.Background(), "span") | ||
return otelCtx | ||
}, | ||
}, | ||
{ | ||
name: "tars trace", | ||
ctx: func(t *testing.T) context.Context { | ||
tarsCtx := current.ContextWithTarsCurrent(context.Background()) | ||
current.OpenTarsTrace(tarsCtx, true) | ||
tt, _ := current.GetTarsTrace(tarsCtx) | ||
tt.NewSpan() | ||
return tarsCtx | ||
}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
for _, format := range []rogger.LogFormat{rogger.Text, rogger.Json} { | ||
rogger.SetFormat(format) | ||
l.Debug(tc.ctx(t), "DEBUG LOG") | ||
l.Debugf(tc.ctx(t), "DEBUG LOG FORMAT: %s", format.String()) | ||
l.Info(tc.ctx(t), "INfO LOG") | ||
l.Infof(tc.ctx(t), "INfO LOG FORMAT: %s", format.String()) | ||
l.Warn(tc.ctx(t), "WARN LOG") | ||
l.Warnf(tc.ctx(t), "WARN LOG FORMAT: %s", format.String()) | ||
l.Error(tc.ctx(t), "ERROR LOG") | ||
l.Errorf(tc.ctx(t), "ERROR LOG FORMAT: %s", format.String()) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.