-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: Conditionally emit metrics based on enablement #19903
Changes from all commits
f378a1a
8aea593
ee6fac9
91f7b6b
6ac4f46
de6950e
75dafb6
a27839d
42435f4
0390f3e
88eb4bc
5e58525
e49123d
a76e4f8
af0ba45
9897209
e2642b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package telemetry | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var mu sync.Mutex | ||
|
||
func initTelemetry(v bool) { | ||
globalTelemetryEnabled = v | ||
} | ||
|
||
// Reset the global state to a known disabled state before each test. | ||
func setupTest(t *testing.T) { | ||
t.Helper() | ||
mu.Lock() // Ensure no other test can modify global state at the same time. | ||
defer mu.Unlock() | ||
initTelemetry(false) | ||
} | ||
|
||
// TestNow tests the Now function when telemetry is enabled and disabled. | ||
func TestNow(t *testing.T) { | ||
setupTest(t) // Locks the mutex to avoid race condition. | ||
|
||
initTelemetry(true) | ||
telemetryTime := Now() | ||
assert.NotEqual(t, time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled") | ||
|
||
setupTest(t) // Reset the global state and lock the mutex again. | ||
|
||
initTelemetry(false) | ||
telemetryTime = Now() | ||
assert.Equal(t, time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled") | ||
} | ||
|
||
// TestIsTelemetryEnabled tests the IsTelemetryEnabled function. | ||
func TestIsTelemetryEnabled(t *testing.T) { | ||
setupTest(t) // Locks the mutex to avoid race condition. | ||
|
||
initTelemetry(true) | ||
assert.True(t, IsTelemetryEnabled(), "IsTelemetryEnabled() should return true when globalTelemetryEnabled is set to true") | ||
|
||
setupTest(t) // Reset the global state and lock the mutex again. | ||
|
||
initTelemetry(false) | ||
assert.False(t, IsTelemetryEnabled(), "IsTelemetryEnabled() should return false when globalTelemetryEnabled is set to false") | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |||||||
"context" | ||||||||
"encoding/json" | ||||||||
"fmt" | ||||||||
"time" | ||||||||
|
||||||||
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" | ||||||||
"google.golang.org/grpc" | ||||||||
|
@@ -93,7 +92,7 @@ func (am AppModule) ValidateGenesis(bz json.RawMessage) error { | |||||||
|
||||||||
// InitGenesis performs genesis initialization for the circuit module. | ||||||||
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) error { | ||||||||
start := time.Now() | ||||||||
start := telemetry.Now() | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change from using + // Using telemetry.Now() to optimize performance when telemetry is disabled
start := telemetry.Now() Committable suggestion
Suggested change
|
||||||||
var genesisState types.GenesisState | ||||||||
if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil { | ||||||||
return err | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The introduction of
globalTelemetryEnabled
and theIsTelemetryEnabled()
function are well-implemented. However, consider changingglobalTelemetryEnabled
from an atomic type to a regular boolean since it is not modified after initialization.Committable suggestion