Skip to content
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

feat(metrics): set build info as an attribute in metrics #2165

Merged
merged 4 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions cmd/celestia/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

cmdnode "github.com/celestiaorg/celestia-node/cmd"
"github.com/celestiaorg/celestia-node/nodebuilder"
"github.com/celestiaorg/celestia-node/nodebuilder/core"
"github.com/celestiaorg/celestia-node/nodebuilder/gateway"
"github.com/celestiaorg/celestia-node/nodebuilder/header"
Expand All @@ -26,6 +27,12 @@ func persistentPreRunEnv(cmd *cobra.Command, nodeType node.Type, _ []string) err
return err
}
ctx = cmdnode.WithNetwork(ctx, parsedNetwork)
ctx = cmdnode.WithNodeBuildInfo(ctx, &nodebuilder.BuildInfo{
LastCommit: lastCommit,
SemanticVersion: semanticVersion,
SystemVersion: systemVersion,
GolangVersion: golangVersion,
})

// loads existing config into the environment
ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx))
Expand Down
7 changes: 5 additions & 2 deletions cmd/celestia/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ var (
buildTime string
lastCommit string
semanticVersion string

systemVersion = fmt.Sprintf("%s/%s", runtime.GOARCH, runtime.GOOS)
golangVersion = runtime.Version()
)

var versionCmd = &cobra.Command{
Expand All @@ -24,6 +27,6 @@ func printBuildInfo(_ *cobra.Command, _ []string) {
fmt.Printf("Semantic version: %s\n", semanticVersion)
fmt.Printf("Commit: %s\n", lastCommit)
fmt.Printf("Build Date: %s\n", buildTime)
fmt.Printf("System version: %s/%s\n", runtime.GOARCH, runtime.GOOS)
fmt.Printf("Golang version: %s\n", runtime.Version())
fmt.Printf("System version: %s\n", systemVersion)
fmt.Printf("Golang version: %s\n", golangVersion)
}
11 changes: 11 additions & 0 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func NodeConfig(ctx context.Context) nodebuilder.Config {
return cfg
}

// NodeInfo reads the node build inforamtion from the context.
func NodeInfo(ctx context.Context) nodebuilder.BuildInfo {
return ctx.Value(buildInfo{}).(nodebuilder.BuildInfo)
}

// WithNodeType sets the node type in the given context.
func WithNodeType(ctx context.Context, tp node.Type) context.Context {
return context.WithValue(ctx, nodeTypeKey{}, tp)
Expand Down Expand Up @@ -73,10 +78,16 @@ func WithNodeConfig(ctx context.Context, config *nodebuilder.Config) context.Con
return context.WithValue(ctx, configKey{}, *config)
}

// WithNodeConfig sets the node config build information.
func WithNodeBuildInfo(ctx context.Context, info *nodebuilder.BuildInfo) context.Context {
return context.WithValue(ctx, buildInfo{}, *info)
}

type (
optionsKey struct{}
configKey struct{}
storePathKey struct{}
nodeTypeKey struct{}
networkKey struct{}
buildInfo struct{}
)
2 changes: 1 addition & 1 deletion cmd/flags_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func ParseMiscFlags(ctx context.Context, cmd *cobra.Command) (context.Context, e
opts = append(opts, otlpmetrichttp.WithInsecure())
}

ctx = WithNodeOptions(ctx, nodebuilder.WithMetrics(opts, NodeType(ctx)))
ctx = WithNodeOptions(ctx, nodebuilder.WithMetrics(opts, NodeType(ctx), NodeInfo(ctx)))
}

ok, err = cmd.Flags().GetBool(p2pMetrics)
Expand Down
8 changes: 8 additions & 0 deletions nodebuilder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,11 @@ func (cfg *Config) Decode(r io.Reader) error {
_, err := toml.NewDecoder(r).Decode(cfg)
return err
}

// BuildInfo stores all necessary information for the current build.
type BuildInfo struct {
vgonkivs marked this conversation as resolved.
Show resolved Hide resolved
LastCommit string
SemanticVersion string
SystemVersion string
GolangVersion string
}
1 change: 1 addition & 0 deletions nodebuilder/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func TestLifecycle_WithMetrics(t *testing.T) {
otlpmetrichttp.WithInsecure(),
},
tt.tp,
BuildInfo{},
),
)
require.NotNil(t, node)
Expand Down
13 changes: 9 additions & 4 deletions nodebuilder/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/libp2p/go-libp2p/core/peer"
"github.com/pyroscope-io/client/pyroscope"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/metric"
Expand Down Expand Up @@ -62,9 +63,10 @@ func WithPyroscope(endpoint string, nodeType node.Type) fx.Option {
}

// WithMetrics enables metrics exporting for the node.
func WithMetrics(metricOpts []otlpmetrichttp.Option, nodeType node.Type) fx.Option {
func WithMetrics(metricOpts []otlpmetrichttp.Option, nodeType node.Type, buildInfo BuildInfo) fx.Option {
baseComponents := fx.Options(
fx.Supply(metricOpts),
fx.Supply(buildInfo),
fx.Invoke(initializeMetrics),
fx.Invoke(state.WithMetrics),
fx.Invoke(fraud.WithMetrics),
Expand Down Expand Up @@ -109,6 +111,7 @@ func initializeMetrics(
lc fx.Lifecycle,
peerID peer.ID,
nodeType node.Type,
buildInfo BuildInfo,
opts []otlpmetrichttp.Option,
) error {
exp, err := otlpmetrichttp.New(ctx, opts...)
Expand All @@ -121,16 +124,18 @@ func initializeMetrics(
metric.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(fmt.Sprintf("Celestia-%s", nodeType.String())),
// TODO(@Wondertan): Versioning: semconv.ServiceVersionKey
semconv.ServiceInstanceIDKey.String(peerID.String()),
// custom key-val pairs
attribute.String("service.lastCommit", buildInfo.LastCommit),
attribute.String("service.semanticVersion", buildInfo.SemanticVersion),
attribute.String("service.systemVersion", buildInfo.SystemVersion),
attribute.String("service.goVersion", buildInfo.GolangVersion),
)))

lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return provider.Shutdown(ctx)
},
})
global.SetMeterProvider(provider)

return nil
}
Loading