Skip to content

Commit

Permalink
cmd/hanyuu, telemetry/otelzerolog: add passing off Instrumentation na…
Browse files Browse the repository at this point in the history
…me and version

This uses the current git commit hash as version
  • Loading branch information
Wessie committed Dec 20, 2024
1 parent aaf4402 commit 13b7b1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
24 changes: 22 additions & 2 deletions cmd/hanyuu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/signal"
"runtime/debug"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
30 changes: 15 additions & 15 deletions telemetry/otelzerolog/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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"))
Expand Down

0 comments on commit 13b7b1d

Please sign in to comment.