Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit 7e846f8

Browse files
Eric-Warehimealjo242
authored andcommitted
chore: Add log sampling (#709)
1 parent fcccb1d commit 7e846f8

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

oracle/init.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ func (o *OracleImpl) createPriceProvider(ctx context.Context, cfg config.Provide
102102
// Add the provider to the oracle.
103103
o.priceProviders[provider.Name()] = state
104104

105+
// Add the provider name to the message here since we want these to ignore log sampling limits
105106
o.logger.Info(
106-
"created price provider state",
107+
fmt.Sprintf("created %s provider state", provider.Name()),
107108
zap.String("provider", provider.Name()),
108109
zap.Int("num_tickers", len(provider.GetIDs())),
109110
)

oracle/update.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ func (o *OracleImpl) UpdateProviderState(providerTickers []types.ProviderTicker,
7474
}()
7575
}
7676

77-
// Update the provider's state.
78-
o.logger.Info("updated provider state", zap.String("provider_state", provider.Name()))
77+
// Ignore sampling limits for provider update logs via injecting provider name in message
78+
o.logger.Info(
79+
fmt.Sprintf("updated %s provider state", provider.Name()),
80+
zap.String("provider", provider.Name()),
81+
zap.Int("num_tickers", len(provider.GetIDs())),
82+
)
7983
return state, nil
8084
}
8185

@@ -104,8 +108,6 @@ func (o *OracleImpl) fetchAllPrices() {
104108

105109
// update the last sync time
106110
o.metrics.AddTick()
107-
108-
o.logger.Info("oracle updated prices", zap.Time("last_sync", o.lastPriceSync), zap.Int("num_prices", len(o.aggregator.GetPrices())))
109111
}
110112

111113
func (o *OracleImpl) fetchPrices(provider *types.PriceProvider) {

pkg/log/zap.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package log
33
import (
44
"fmt"
55
"os"
6+
"strings"
7+
"time"
68

79
"go.uber.org/zap"
810
"go.uber.org/zap/zapcore"
@@ -27,6 +29,8 @@ type Config struct {
2729
MaxAge int
2830
// Compress determines if the rotated log files should be compressed.
2931
Compress bool
32+
// LogSamplePeriod is the duration in which we de-dupe identical log messages.
33+
LogSamplePeriod time.Duration
3034
}
3135

3236
// NewDefaultConfig creates a default configuration for the logger.
@@ -40,6 +44,7 @@ func NewDefaultConfig() Config {
4044
MaxBackups: 1,
4145
MaxAge: 3, // 3 days
4246
Compress: false,
47+
LogSamplePeriod: 10 * time.Second,
4348
}
4449
}
4550

@@ -93,6 +98,10 @@ func NewLogger(config Config) *zap.Logger {
9398
} else {
9499
core = stdCore
95100
}
101+
if strings.ToUpper(config.StdOutLogLevel) != zap.DebugLevel.CapitalString() && strings.ToUpper(config.FileOutLogLevel) != zap.DebugLevel.CapitalString() {
102+
// If we're not in debug log level anywhere, filter any logs which have non-unique messages within a 10-second period
103+
core = zapcore.NewSamplerWithOptions(core, config.LogSamplePeriod, 1, 0)
104+
}
96105

97106
return zap.New(
98107
core,

pkg/math/oracle/aggregator.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ func (m *IndexPriceAggregator) AggregatePrices() {
7777
indexPrices := make(types.Prices)
7878
scaledPrices := make(types.Prices)
7979

80+
var missingPrices []string
81+
8082
for ticker, market := range m.cfg.Markets {
8183
if !market.Ticker.Enabled {
8284
m.logger.Debug("skipping disabled market", zap.Any("market", market))
@@ -92,7 +94,8 @@ func (m *IndexPriceAggregator) AggregatePrices() {
9294

9395
// We need to have at least the minimum number of providers to calculate the median.
9496
if len(convertedPrices) < int(target.MinProviderCount) { //nolint:gosec
95-
m.logger.Error(
97+
missingPrices = append(missingPrices, ticker)
98+
m.logger.Debug(
9699
"insufficient amount of converted prices",
97100
zap.String("target_ticker", ticker),
98101
zap.Int("num_converted_prices", len(convertedPrices)),
@@ -127,6 +130,9 @@ func (m *IndexPriceAggregator) AggregatePrices() {
127130
// Update the aggregated data. These prices are going to be used as the index prices the
128131
// next time we calculate prices.
129132
m.logger.Debug("calculated median prices for price feeds", zap.Int("num_prices", len(indexPrices)))
133+
if len(missingPrices) > 0 {
134+
m.logger.Info("failed to calculate prices for price feeds", zap.Strings("missing_prices", missingPrices))
135+
}
130136
m.indexPrices = indexPrices
131137
m.scaledPrices = scaledPrices
132138
}

0 commit comments

Comments
 (0)