From 13b7b1d2cdfe28325ad652310f4d15d5c5a19573 Mon Sep 17 00:00:00 2001 From: Wessie Date: Fri, 20 Dec 2024 21:32:16 +0100 Subject: [PATCH] cmd/hanyuu, telemetry/otelzerolog: add passing off Instrumentation name and version This uses the current git commit hash as version --- cmd/hanyuu/main.go | 24 ++++++++++++++++++++++-- telemetry/otelzerolog/hook.go | 30 +++++++++++++++--------------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/cmd/hanyuu/main.go b/cmd/hanyuu/main.go index 9a1b897..1a1b0d1 100644 --- a/cmd/hanyuu/main.go +++ b/cmd/hanyuu/main.go @@ -9,6 +9,7 @@ import ( "os" "os/signal" "runtime/debug" + "sync" "syscall" "time" @@ -125,9 +126,20 @@ var versionCmd = cmd{ execute: printVersion, } +var CommitHash = sync.OnceValue[string](func() string { + if info, ok := debug.ReadBuildInfo(); ok { // requires go version 1.12+ + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + return setting.Value + } + } + } + return "(devel)" +}) + func printVersion(context.Context, config.Loader) error { if info, ok := debug.ReadBuildInfo(); ok { // requires go version 1.12+ - fmt.Printf("%s %s\n", info.Path, info.Main.Version) + fmt.Printf("%s %s\n", info.Path, CommitHash()) for _, mod := range info.Deps { fmt.Printf("\t%s %s\n", mod.Path, mod.Version) } @@ -268,6 +280,11 @@ var bleveCmd = cmd{ noSIGUSR2: true, } +var ( + InstrumentationName = "github.com/R-a-dio/valkyrie" + InstrumentationVersion = CommitHash() +) + func main() { var disableStdout bool // setup configuration file as top-level flag @@ -326,7 +343,10 @@ func main() { os.Exit(1) } // use the opentelemetry zerolog hook - logger = logger.Level(level).Hook(otelzerolog.Hook()) + logger = logger.Level(level).Hook(otelzerolog.Hook( + InstrumentationName, + InstrumentationVersion, + )) // setup root context ctx := context.Background() diff --git a/telemetry/otelzerolog/hook.go b/telemetry/otelzerolog/hook.go index 9bb7ee4..67699b8 100644 --- a/telemetry/otelzerolog/hook.go +++ b/telemetry/otelzerolog/hook.go @@ -12,16 +12,10 @@ import ( "go.opentelemetry.io/otel/log/global" ) -var ( - InstrumentationName = "github.com/R-a-dio/valkyrie/telemetry/otelzerolog" - InstrumentationVersion = "0.1.0" -) - -func Hook() zerolog.Hook { +func Hook(instrumentation_name, instrumentation_version string) zerolog.Hook { logger := global.GetLoggerProvider().Logger( - // TODO: make this use proper names and version - InstrumentationName, - log.WithInstrumentationVersion(InstrumentationVersion), + instrumentation_name, + log.WithInstrumentationVersion(instrumentation_version), ) return &hook{logger} @@ -31,23 +25,29 @@ type hook struct { logger log.Logger } -func (h hook) Run(e *zerolog.Event, level zerolog.Level, msg string) { - if !e.Enabled() { +func (h hook) Run(e *zerolog.Event, zerolevel zerolog.Level, msg string) { + if !e.Enabled() { // check if zerolog is enabled return } - r := log.Record{} ctx := e.GetCtx() + level := convertLevel(zerolevel) + + if !h.logger.Enabled(ctx, log.EnabledParameters{Severity: level}) { + // check if opentelemetry logging is enabled + return + } + + r := log.Record{} now := time.Now() + r.SetSeverity(level) + r.SetSeverityText(zerolevel.String()) r.SetBody(log.StringValue(msg)) r.SetTimestamp(now) r.SetObservedTimestamp(now) - r.SetSeverity(convertLevel(level)) - r.SetSeverityText(level.String()) - logData := make(map[string]interface{}) // create a string that appends } to the end of the buf variable you access via reflection ev := fmt.Sprintf("%s}", reflect.ValueOf(e).Elem().FieldByName("buf"))