Skip to content

Commit

Permalink
build: Append commit hash to the main LND context
Browse files Browse the repository at this point in the history
It can be disabled via the new `logging.no-commit-hash` config option.
  • Loading branch information
ellemouton committed Dec 2, 2024
1 parent efe08b8 commit 755ad49
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
5 changes: 3 additions & 2 deletions build/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ const (
//
//nolint:ll
type LogConfig struct {
Console *consoleLoggerCfg `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."`
File *FileLoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."`
Console *consoleLoggerCfg `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."`
File *FileLoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."`
NoCommitHash bool `long:"no-commit-hash" description:"If set, the commit-hash of the current build will not be included in log lines by default."`
}

// Validate validates the LogConfig struct values.
Expand Down
29 changes: 29 additions & 0 deletions build/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
package build

import (
"context"
"encoding/hex"
"fmt"
"runtime/debug"
"strings"

"github.com/btcsuite/btclog/v2"
)

var (
Expand Down Expand Up @@ -101,3 +105,28 @@ func Tags() []string {

return strings.Split(RawTags, ",")
}

// WithBuildInfo derives a child context with the build information attached as
// attributes. At the moment, this only includes the current build's commit
// hash.
func WithBuildInfo(ctx context.Context, cfg *LogConfig) (context.Context,
error) {

if cfg.NoCommitHash {
return ctx, nil
}

// Convert the commit hash to a byte slice.
commitHash, err := hex.DecodeString(CommitHash)
if err != nil {
return nil, fmt.Errorf("unable to decode commit hash: %w", err)
}

// Include the first 3 bytes of the commit hash in the context as an
// slog attribute.
if len(commitHash) > 3 {
commitHash = commitHash[:3]
}

return btclog.WithCtx(ctx, btclog.Hex("rev", commitHash)), nil
}
13 changes: 9 additions & 4 deletions lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
}
}()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

ctx, err := build.WithBuildInfo(ctx, cfg.LogConfig)
if err != nil {
return fmt.Errorf("unable to add build info to context: %w",
err)
}

mkErr := func(format string, args ...interface{}) error {
ltndLog.Errorf("Shutting down because error in main "+
"method: "+format, args...)
Expand Down Expand Up @@ -188,10 +197,6 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
strings.Title(BitcoinChainName), network,
)

ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

// Enable http profiling server if requested.
if cfg.Pprof.Profile != "" {
// Create the http handler.
Expand Down
1 change: 1 addition & 0 deletions lntest/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func (cfg *BaseNodeConfig) GenArgs() []string {
"--bitcoin.defaultchanconfs=1",
"--accept-keysend",
"--keep-failed-payment-attempts",
"--logging.no-commit-hash",
fmt.Sprintf("--db.batch-commit-interval=%v", commitInterval),
fmt.Sprintf("--bitcoin.defaultremotedelay=%v", DefaultCSV),
fmt.Sprintf("--rpclisten=%v", cfg.RPCAddr()),
Expand Down
5 changes: 5 additions & 0 deletions sample-lnd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,11 @@

[logging]

; Whether to exclude the current build's commit hash from log lines. Note that
; the commit hash will not currently show up in all LND log lines as this new
; feature will take a few versions to propagate through the codebase.
; logging.no-commit-hash=false

; Disable logging to stdout and stderror.
; logging.console.disable=false

Expand Down

0 comments on commit 755ad49

Please sign in to comment.