Skip to content

Commit af0ba45

Browse files
committed
Improvents on telemetry calls
1 parent a76e4f8 commit af0ba45

File tree

14 files changed

+45
-70
lines changed

14 files changed

+45
-70
lines changed

baseapp/abci.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ func (app *BaseApp) Query(_ context.Context, req *abci.RequestQuery) (resp *abci
177177

178178
telemetry.IncrCounter(1, "query", "count")
179179
telemetry.IncrCounter(1, "query", req.Path)
180-
start := telemetry.Now()
181-
defer telemetry.MeasureSince(start, req.Path)
180+
defer telemetry.MeasureSince(time.Now(), req.Path)
182181

183182
if req.Path == QueryPathBroadcastTx {
184183
return sdkerrors.QueryResult(errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "can't route a broadcast tx message"), app.trace), nil

server/start.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,6 @@ func startAPIServer(
533533
}
534534

535535
func startTelemetry(cfg serverconfig.Config) (*telemetry.Metrics, error) {
536-
if !cfg.Telemetry.Enabled {
537-
return nil, nil
538-
}
539-
540536
return telemetry.New(cfg.Telemetry)
541537
}
542538

telemetry/metrics.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/http"
8-
"sync/atomic"
98
"time"
109

1110
"github.com/hashicorp/go-metrics"
@@ -17,17 +16,11 @@ import (
1716

1817
// globalTelemetryEnabled is a private variable that stores the telemetry enabled state.
1918
// It is set on initialization and does not change for the lifetime of the program.
20-
var globalTelemetryEnabled atomic.Bool
19+
var globalTelemetryEnabled bool
2120

22-
// initTelemetry sets the global variable based on the configuration.
23-
// It is called only once, at startup, to set the telemetry enabled state.
24-
func initTelemetry(enabled bool) {
25-
globalTelemetryEnabled.Store(enabled)
26-
}
27-
28-
// isTelemetryEnabled provides controlled access to check if telemetry is enabled.
29-
func isTelemetryEnabled() bool {
30-
return globalTelemetryEnabled.Load()
21+
// IsTelemetryEnabled provides controlled access to check if telemetry is enabled.
22+
func IsTelemetryEnabled() bool {
23+
return globalTelemetryEnabled
3124
}
3225

3326
// globalLabels defines the set of global labels that will be applied to all
@@ -111,7 +104,7 @@ type GatherResponse struct {
111104

112105
// New creates a new instance of Metrics
113106
func New(cfg Config) (_ *Metrics, rerr error) {
114-
initTelemetry(cfg.Enabled)
107+
globalTelemetryEnabled = cfg.Enabled
115108
if !cfg.Enabled {
116109
return nil, nil
117110
}

telemetry/wrapper.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewLabel(name, value string) metrics.Label {
2424
// metric for a module with a given set of keys. If any global labels are defined,
2525
// they will be added to the module label.
2626
func ModuleMeasureSince(module string, start time.Time, keys ...string) {
27-
if !isTelemetryEnabled() {
27+
if !IsTelemetryEnabled() {
2828
return
2929
}
3030

@@ -39,7 +39,7 @@ func ModuleMeasureSince(module string, start time.Time, keys ...string) {
3939
// module with a given set of keys. If any global labels are defined, they will
4040
// be added to the module label.
4141
func ModuleSetGauge(module string, val float32, keys ...string) {
42-
if !isTelemetryEnabled() {
42+
if !IsTelemetryEnabled() {
4343
return
4444
}
4545

@@ -53,7 +53,7 @@ func ModuleSetGauge(module string, val float32, keys ...string) {
5353
// IncrCounter provides a wrapper functionality for emitting a counter metric with
5454
// global labels (if any).
5555
func IncrCounter(val float32, keys ...string) {
56-
if !isTelemetryEnabled() {
56+
if !IsTelemetryEnabled() {
5757
return
5858
}
5959

@@ -63,7 +63,7 @@ func IncrCounter(val float32, keys ...string) {
6363
// IncrCounterWithLabels provides a wrapper functionality for emitting a counter
6464
// metric with global labels (if any) along with the provided labels.
6565
func IncrCounterWithLabels(keys []string, val float32, labels []metrics.Label) {
66-
if !isTelemetryEnabled() {
66+
if !IsTelemetryEnabled() {
6767
return
6868
}
6969

@@ -73,7 +73,7 @@ func IncrCounterWithLabels(keys []string, val float32, labels []metrics.Label) {
7373
// SetGauge provides a wrapper functionality for emitting a gauge metric with
7474
// global labels (if any).
7575
func SetGauge(val float32, keys ...string) {
76-
if !isTelemetryEnabled() {
76+
if !IsTelemetryEnabled() {
7777
return
7878
}
7979

@@ -83,7 +83,7 @@ func SetGauge(val float32, keys ...string) {
8383
// SetGaugeWithLabels provides a wrapper functionality for emitting a gauge
8484
// metric with global labels (if any) along with the provided labels.
8585
func SetGaugeWithLabels(keys []string, val float32, labels []metrics.Label) {
86-
if !isTelemetryEnabled() {
86+
if !IsTelemetryEnabled() {
8787
return
8888
}
8989

@@ -93,7 +93,7 @@ func SetGaugeWithLabels(keys []string, val float32, labels []metrics.Label) {
9393
// MeasureSince provides a wrapper functionality for emitting a a time measure
9494
// metric with global labels (if any).
9595
func MeasureSince(start time.Time, keys ...string) {
96-
if !isTelemetryEnabled() {
96+
if !IsTelemetryEnabled() {
9797
return
9898
}
9999

@@ -102,7 +102,7 @@ func MeasureSince(start time.Time, keys ...string) {
102102

103103
// Now return the current time if telemetry is enabled or a zero time if it's not
104104
func Now() time.Time {
105-
if !isTelemetryEnabled() {
105+
if !IsTelemetryEnabled() {
106106
return time.Time{}
107107
}
108108

telemetry/wrapper_test.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,46 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/stretchr/testify/suite"
8+
"github.com/stretchr/testify/assert"
99
)
1010

11-
// TelemetrySuite is a struct that holds the setup for the telemetry tests.
12-
// It includes a mutex to ensure that tests that depend on the global state
13-
// do not run in parallel, which can cause race conditions and unpredictable results.
14-
type TelemetrySuite struct {
15-
suite.Suite
16-
mu sync.Mutex
11+
var mu sync.Mutex
12+
13+
func initTelemetry(v bool) {
14+
globalTelemetryEnabled = v
1715
}
1816

19-
// SetupTest is called before each test to reset the global state to a known disabled state.
20-
// This ensures each test starts with the telemetry disabled
21-
func (suite *TelemetrySuite) SetupTest() {
17+
// Reset the global state to a known disabled state before each test.
18+
func setupTest(t *testing.T) {
19+
mu.Lock() // Ensure no other test can modify global state at the same time.
20+
defer mu.Unlock()
2221
initTelemetry(false)
2322
}
2423

2524
// TestNow tests the Now function when telemetry is enabled and disabled.
26-
func (suite *TelemetrySuite) TestNow() {
27-
suite.mu.Lock()
28-
defer suite.mu.Unlock()
25+
func TestNow(t *testing.T) {
26+
setupTest(t) // Locks the mutex to avoid race condition.
2927

3028
initTelemetry(true)
3129
telemetryTime := Now()
32-
suite.NotEqual(time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled")
30+
assert.NotEqual(t, time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled")
31+
32+
setupTest(t) // Reset the global state and lock the mutex again.
3333

3434
initTelemetry(false)
3535
telemetryTime = Now()
36-
suite.Equal(time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled")
36+
assert.Equal(t, time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled")
3737
}
3838

39-
// TestIsTelemetryEnabled tests the isTelemetryEnabled function.
40-
func (suite *TelemetrySuite) TestIsTelemetryEnabled() {
41-
suite.mu.Lock()
42-
defer suite.mu.Unlock()
39+
// TestIsTelemetryEnabled tests the IsTelemetryEnabled function.
40+
func TestIsTelemetryEnabled(t *testing.T) {
41+
setupTest(t) // Locks the mutex to avoid race condition.
4342

4443
initTelemetry(true)
45-
suite.True(isTelemetryEnabled(), "isTelemetryEnabled() should return true when globalTelemetryEnabled is set to true")
44+
assert.True(t, IsTelemetryEnabled(), "IsTelemetryEnabled() should return true when globalTelemetryEnabled is set to true")
4645

47-
initTelemetry(false)
48-
suite.False(isTelemetryEnabled(), "isTelemetryEnabled() should return false when globalTelemetryEnabled is set to false")
49-
}
46+
setupTest(t) // Reset the global state and lock the mutex again.
5047

51-
// TestTelemetrySuite initiates the test suite.
52-
func TestTelemetrySuite(t *testing.T) {
53-
suite.Run(t, new(TelemetrySuite))
48+
initTelemetry(false)
49+
assert.False(t, IsTelemetryEnabled(), "IsTelemetryEnabled() should return false when globalTelemetryEnabled is set to false")
5450
}

x/crisis/abci.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111

1212
// check all registered invariants
1313
func EndBlocker(ctx context.Context, k keeper.Keeper) {
14-
start := telemetry.Now()
15-
defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyEndBlocker)
14+
defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyEndBlocker)
1615

1716
sdkCtx := sdk.UnwrapSDKContext(ctx)
1817
if k.InvCheckPeriod() == 0 || sdkCtx.BlockHeight()%int64(k.InvCheckPeriod()) != 0 {

x/crisis/module.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,11 @@ func (am AppModule) ValidateGenesis(bz json.RawMessage) error {
117117

118118
// InitGenesis performs genesis initialization for the crisis module.
119119
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) error {
120-
start := telemetry.Now()
121120
var genesisState types.GenesisState
122121
if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil {
123122
return err
124123
}
125-
telemetry.MeasureSince(start, "InitGenesis", "crisis", "unmarshal")
124+
telemetry.MeasureSince(telemetry.Now(), "InitGenesis", "crisis", "unmarshal")
126125

127126
am.keeper.InitGenesis(ctx, &genesisState)
128127
if !am.skipGenesisInvariants {

x/distribution/keeper/abci.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111
// and distribute rewards for the previous block.
1212
// TODO: use context.Context after including the comet service
1313
func (k Keeper) BeginBlocker(ctx sdk.Context) error {
14-
start := telemetry.Now()
15-
defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker)
14+
defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker)
1615

1716
// determine the total power signing the block
1817
var previousTotalPower int64

x/evidence/keeper/abci.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import (
1414
// BeginBlocker iterates through and handles any newly discovered evidence of
1515
// misbehavior submitted by CometBFT. Currently, only equivocation is handled.
1616
func (k Keeper) BeginBlocker(ctx context.Context) error {
17-
start := telemetry.Now()
18-
defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker)
17+
defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker)
1918

2019
bi := sdk.UnwrapSDKContext(ctx).CometInfo()
2120

x/gov/keeper/abci.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import (
2121

2222
// EndBlocker is called every block.
2323
func (k Keeper) EndBlocker(ctx context.Context) error {
24-
start := telemetry.Now()
25-
defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyEndBlocker)
24+
defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyEndBlocker)
2625

2726
logger := k.Logger()
2827
// delete dead proposals from store and returns theirs deposits.

0 commit comments

Comments
 (0)