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

fix(server/v2/api/telemetry): enable global metrics #22571

Merged
merged 5 commits into from
Nov 20, 2024
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
4 changes: 4 additions & 0 deletions server/v2/api/telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ type GatherResponse struct {

// NewMetrics creates a new instance of Metrics
func NewMetrics(cfg *Config) (*Metrics, error) {
if !cfg.Enable {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Document nil return and consider adding IsEnabled method

To improve safety and usability:

  1. Document that NewMetrics returns nil when metrics are disabled
  2. Consider adding an IsEnabled() bool method to safely check metrics state

Add documentation and helper method:

+ // NewMetrics creates a new instance of Metrics.
+ // Returns nil if metrics are disabled in the configuration.
 func NewMetrics(cfg *Config) (*Metrics, error) {
   if !cfg.Enable {
     return nil, nil
   }
// Add this helper method to the Metrics struct
func (m *Metrics) IsEnabled() bool {
    return m != nil
}


if numGlobalLabels := len(cfg.GlobalLabels); numGlobalLabels > 0 {
parsedGlobalLabels := make([]metrics.Label, numGlobalLabels)
for i, gl := range cfg.GlobalLabels {
Expand Down
10 changes: 9 additions & 1 deletion server/v2/api/telemetry/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Server[T transaction.Tx] struct {
}

// New creates a new telemetry server.
func New[T transaction.Tx](cfg server.ConfigMap, logger log.Logger) (*Server[T], error) {
func New[T transaction.Tx](cfg server.ConfigMap, logger log.Logger, enableTelemetry func()) (*Server[T], error) {
srv := &Server[T]{}
serverCfg := srv.Config().(*Config)
if len(cfg) > 0 {
Expand All @@ -39,6 +39,14 @@ func New[T transaction.Tx](cfg server.ConfigMap, logger log.Logger) (*Server[T],
srv.config = serverCfg
srv.logger = logger.With(log.ModuleKey, srv.Name())

if enableTelemetry == nil {
panic("enableTelemetry must be provided")
}

if srv.config.Enable {
enableTelemetry()
}

metrics, err := NewMetrics(srv.config)
if err != nil {
return nil, fmt.Errorf("failed to initialize metrics: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion simapp/v2/simdv2/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/debug"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
sdktelemetry "github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/genutil"
Expand Down Expand Up @@ -117,7 +118,7 @@ func InitRootCmd[T transaction.Tx](
}
}

telemetryServer, err := telemetry.New[T](deps.GlobalConfig, logger)
telemetryServer, err := telemetry.New[T](deps.GlobalConfig, logger, sdktelemetry.EnableTelemetry)
if err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func IsTelemetryEnabled() bool {
return globalTelemetryEnabled
}

// SetTelemetryEnabled allows for the global telemetry enabled state to be set.
func EnableTelemetry() {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
globalTelemetryEnabled = true
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change potentially affects state.

Call sequence:

github.com/cosmos/cosmos-sdk/telemetry.IsTelemetryEnabled (telemetry/metrics.go:23)
github.com/cosmos/cosmos-sdk/telemetry.IncrCounter (telemetry/metrics.go:53)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).deliverTx (telemetry/metrics.go:761)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).internalFinalizeBlock (telemetry/metrics.go:747)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).FinalizeBlock (telemetry/metrics.go:902)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Consider thread safety and design implications of EnableTelemetry

The current implementation has several concerns:

  1. Thread safety: Access to globalTelemetryEnabled isn't synchronized, which could lead to race conditions in concurrent environments.
  2. The function contradicts the documented behavior that states the variable "does not change for the lifetime of the program"
  3. One-way operation: Consider whether disabling telemetry should also be supported

Consider this thread-safe implementation:

+var telemetryMu sync.RWMutex

 func EnableTelemetry() {
+    telemetryMu.Lock()
+    defer telemetryMu.Unlock()
     globalTelemetryEnabled = true
 }

 func IsTelemetryEnabled() bool {
+    telemetryMu.RLock()
+    defer telemetryMu.RUnlock()
     return globalTelemetryEnabled
 }

Committable suggestion skipped: line range outside the PR's diff.


// globalLabels defines the set of global labels that will be applied to all
// metrics emitted using the telemetry package function wrappers.
var globalLabels = []metrics.Label{}
Expand Down
Loading