Skip to content

Commit

Permalink
feat(metrics): set build info as an attribute in metrics (#2165)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed May 5, 2023
1 parent 0100141 commit 929a334
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 8 deletions.
6 changes: 6 additions & 0 deletions cmd/celestia/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func persistentPreRunEnv(cmd *cobra.Command, nodeType node.Type, _ []string) err
return err
}
ctx = cmdnode.WithNetwork(ctx, parsedNetwork)
ctx = cmdnode.WithNodeBuildInfo(ctx, &node.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) node.BuildInfo {
return ctx.Value(buildInfo{}).(node.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 *node.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
9 changes: 9 additions & 0 deletions nodebuilder/node/buildInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package node

// BuildInfo stores all necessary information for the current build.
type BuildInfo struct {
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,
node.BuildInfo{},
),
)
require.NotNil(t, node)
Expand Down
10 changes: 5 additions & 5 deletions nodebuilder/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,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 node.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 +110,7 @@ func initializeMetrics(
lc fx.Lifecycle,
peerID peer.ID,
nodeType node.Type,
buildInfo node.BuildInfo,
opts []otlpmetrichttp.Option,
) error {
exp, err := otlpmetrichttp.New(ctx, opts...)
Expand All @@ -120,17 +122,15 @@ func initializeMetrics(
metric.WithReader(metric.NewPeriodicReader(exp, metric.WithTimeout(2*time.Second))),
metric.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(fmt.Sprintf("Celestia-%s", nodeType.String())),
// TODO(@Wondertan): Versioning: semconv.ServiceVersionKey
semconv.ServiceNamespaceKey.String(fmt.Sprintf("Celestia-%s", nodeType.String())),
semconv.ServiceNameKey.String(fmt.Sprintf("semver-%s", buildInfo.SemanticVersion)),
semconv.ServiceInstanceIDKey.String(peerID.String()),
)))

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

return nil
}

0 comments on commit 929a334

Please sign in to comment.