Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/infoplat-19823-llo-observed-value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

#added llo_datasource_stream_observed_value gauge: latest observed value per LLO stream (value_type benchmark|bid|ask, config_digest, don_id, csa_public_key). To forward it to Beholder, set `[Telemetry.PrometheusBridge] Enabled = true` and add `llo_datasource_stream_observed_value` to `Prefixes`; setting `Prefixes` replaces the default `["go_"]`, so keep `"go_"` and any prefixes already forwarded.
1 change: 1 addition & 0 deletions core/services/chainlink/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ func NewApplication(ctx context.Context, opts ApplicationOpts) (Application, err
Lggr: globalLogger,
Ks: keyStore.OCR2(),
EthKs: keyStore.Eth(),
CSAPublicKey: csaPubKeyHex,
WorkflowKs: keyStore.Workflow(),
DKGRecipientKs: keyStore.DKGRecipient(),
Relayers: relayChainInterops,
Expand Down
4 changes: 3 additions & 1 deletion core/services/llo/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type DelegateConfig struct {
PluginMonitoringEndpoint telemetry.MultitypeMonitoringEndpoint
DonID uint32
ChainID string
// CSAPublicKey is this node's CSA public key in hex, used to label the observed value gauge.
CSAPublicKey string

// OCR3
TraceLogging bool
Expand Down Expand Up @@ -198,7 +200,7 @@ func NewDelegate(cfg DelegateConfig) (job.ServiceCtx, error) {

t := telem.NewTelemeterService(cfg.telemeterParams(lggr))

ds := observation.NewDataSource(logger.Named(lggr, "DataSource"), cfg.Registry, t)
ds := observation.NewDataSource(logger.Named(lggr, "DataSource"), cfg.Registry, t, cfg.DonID, cfg.CSAPublicKey)

notifier, ok := cfg.ContractTransmitter.(transmitter.TransmitNotifier)
if ok {
Expand Down
41 changes: 38 additions & 3 deletions core/services/llo/observation/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,32 @@ type dataSource struct {

// loopWakeCh coalesces plugin Observe hints (buffer 1): the loop may skip pacing when the plugin is active.
loopWakeCh chan struct{}

// observedValues exports the values that the production instance reads from the cache (see observe).
observedValues *observedValueGauges
observedValuesExpiryOnce sync.Once
}

var _ llodatasource.DataSource = &dataSource{}

// NewDataSource returns the shared LLO data source. llo/v30 and llo/v31 both
// consume llodatasource.DataSource, so a single implementation serves both OCR
// protocol versions; lifecycle gating is driven by opts.LifeCycleStage().
func NewDataSource(lggr logger.Logger, registry Registry, t Telemeter) llodatasource.DataSource {
return newDataSource(lggr, registry, t)
// donID and csaPublicKey label the observed value gauge: the DON this job
// belongs to, and this node's CSA public key in hex.
func NewDataSource(lggr logger.Logger, registry Registry, t Telemeter, donID uint32, csaPublicKey string) llodatasource.DataSource {
return newDataSource(lggr, registry, t, donID, csaPublicKey)
}

func newDataSource(lggr logger.Logger, registry Registry, t Telemeter) *dataSource {
func newDataSource(lggr logger.Logger, registry Registry, t Telemeter, donID uint32, csaPublicKey string) *dataSource {
return &dataSource{
lggr: logger.Named(lggr, "DataSource"),
registry: registry,
t: t,
cache: NewCache(time.Minute),
observationLoopCloseCh: make(chan struct{}),
loopWakeCh: make(chan struct{}, 1),
observedValues: newObservedValueGauges(promStreamObservedValue, donID, csaPublicKey),
}
}

Expand Down Expand Up @@ -249,9 +256,36 @@ func (d *dataSource) observe(ctx context.Context, streamValues lloprotocol.Strea
// Update stream values with the cached observations for all streams.
d.cache.UpdateStreamValues(streamValues)

// Only the production instance records: a staging or retired instance reads the same cache and would
// duplicate every series under a second config digest. inProduction is false for nil opts.
if inProduction {
d.observedValues.record(opts.ConfigDigest(), streamValues, time.Now())
d.startObservedValuesExpiry()
}

return nil
}

// startObservedValuesExpiry starts (once) a goroutine that deletes observed value series of config digests that
// stopped calling Observe. record() alone cannot do this when no digest calls Observe at all (DON stall), and the
// observation loop exits when the stream set is empty, so expiry needs its own ticker. It stops on Close.
func (d *dataSource) startObservedValuesExpiry() {
d.observedValuesExpiryOnce.Do(func() {
d.wg.Go(func() {
ticker := time.NewTicker(observedValueDigestTTL / 2)
defer ticker.Stop()
for {
select {
case <-d.observationLoopCloseCh:
return
case now := <-ticker.C:
d.observedValues.expire(now)
}
}
})
})
}

// startObservationLoop continuously runs pipeline observations for this data source and caches results in memory,
// so the plugin Observe path stays fast regardless of adapter latency.
// Loop pacing (observationLoopPacing), per-round deadline (observationTimeout), cache TTL, and stale-refresh
Expand Down Expand Up @@ -526,6 +560,7 @@ func (d *dataSource) Close() error {
close(d.observationLoopCloseCh)
d.wg.Wait()
d.cache.Close()
d.observedValues.deleteAll()

return nil
}
Expand Down
101 changes: 85 additions & 16 deletions core/services/llo/observation/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func Test_DataSource(t *testing.T) {
t.Run("doesn't set any values if no streams are defined", func(t *testing.T) {
t.Parallel()
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)

vals := makeStreamValues()
ctx, cancel := context.WithTimeout(mainCtx, observationTimeout)
Expand All @@ -249,7 +249,7 @@ func Test_DataSource(t *testing.T) {
t.Run("observes each stream with success and returns values matching map argument", func(t *testing.T) {
t.Parallel()
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)

reg.mu.Lock()
sids := []streams.StreamID{1, 2, 3}
Expand Down Expand Up @@ -280,7 +280,7 @@ func Test_DataSource(t *testing.T) {
t.Run("caches nothing for a pipeline whose quote violates the bid/benchmark/ask invariant", func(t *testing.T) {
t.Parallel()
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)

reg.mu.Lock()
sids := []streams.StreamID{1, 2, 3}
Expand Down Expand Up @@ -313,7 +313,7 @@ func Test_DataSource(t *testing.T) {
t.Run("observes each stream and returns success/errors", func(t *testing.T) {
t.Parallel()
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)

reg.mu.Lock()
reg.pipelines[11] = pipelineForStream(11, 11, big.NewInt(21810), errors.New("something exploded"))
Expand All @@ -340,7 +340,7 @@ func Test_DataSource(t *testing.T) {
t.Parallel()
tm := &mockTelemeter{}
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, tm)
ds := newDataSource(lggr, reg, tm, testDonID, testCSAPublicKey)

reg.mu.Lock()
reg.pipelines[21] = pipelineForStream(21, 100, big.NewInt(2181), nil)
Expand Down Expand Up @@ -415,7 +415,7 @@ func Test_DataSource(t *testing.T) {
t.Parallel()
tm := &mockTelemeter{}
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, tm)
ds := newDataSource(lggr, reg, tm, testDonID, testCSAPublicKey)

reg.mu.Lock()
reg.pipelines[31] = pipelineForStream(31, 100, big.NewInt(2181), errors.New("something exploded"))
Expand Down Expand Up @@ -454,7 +454,7 @@ func Test_DataSource(t *testing.T) {
t.Run("uses cached values when available", func(t *testing.T) {
t.Parallel()
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)

// First observation to populate cache
reg.mu.Lock()
Expand Down Expand Up @@ -508,7 +508,7 @@ func Test_DataSource(t *testing.T) {
t.Run("refreshes cache after expiration", func(t *testing.T) {
t.Parallel()
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)

// First observation
reg.mu.Lock()
Expand Down Expand Up @@ -543,7 +543,7 @@ func Test_DataSource(t *testing.T) {
t.Parallel()
// Create a new data source
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)

// Set up pipeline to return different values
reg.mu.Lock()
Expand Down Expand Up @@ -579,7 +579,7 @@ func Test_DataSource(t *testing.T) {
t.Run("cache writes are atomic per pipeline group across observation cycles", func(t *testing.T) {
t.Parallel()
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)
mc := newMockCache(ds.cache)
ds.cache = mc
defer ds.Close()
Expand Down Expand Up @@ -662,7 +662,7 @@ func Test_DataSource(t *testing.T) {
t.Run("handles cache errors gracefully", func(t *testing.T) {
t.Parallel()
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)

// First observation with error
reg.mu.Lock()
Expand Down Expand Up @@ -708,7 +708,7 @@ func Test_DataSource_ObservationLoopWakeSkipsPacing(t *testing.T) { //nolint:par
reg.pipelines[1] = pipelineForStream(1, 1, big.NewInt(42), nil)
reg.mu.Unlock()

ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)
defer ds.Close()

// Long plugin deadline => large inter-iteration pacing; wake from Observe should advance the loop without waiting.
Expand All @@ -733,7 +733,7 @@ func Test_DataSource_ObserveWakeManyConcurrent(t *testing.T) {
reg.pipelines[1] = pipelineForStream(1, 1, big.NewInt(1), nil)
reg.mu.Unlock()

ds := newDataSource(lggr, reg, telem.NullTelemeter)
ds := newDataSource(lggr, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)
ctx, cancel := context.WithTimeout(mainCtx, observationTimeout)
defer cancel()
vals := makeStreamValues(1)
Expand Down Expand Up @@ -982,7 +982,7 @@ result3 -> result3_parse -> multiply3;
require.NoError(b, err)
}

ds := newDataSource(lggr, r, telem.NullTelemeter)
ds := newDataSource(lggr, r, telem.NullTelemeter, testDonID, testCSAPublicKey)
vals := make(map[llotypes.StreamID]lloprotocol.StreamValue)
for i := range 4 * n {
vals[i] = nil
Expand All @@ -999,7 +999,7 @@ result3 -> result3_parse -> multiply3;
func Test_DataSource_inProduction(t *testing.T) {
t.Parallel()
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
ds := newDataSource(logger.NullLogger, reg, telem.NullTelemeter)
ds := newDataSource(logger.NullLogger, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)
defer ds.Close()

require.True(t, ds.inProduction(&mockOpts{lifeCycleStage: lloprotocol.LifeCycleStageProduction}))
Expand All @@ -1014,7 +1014,7 @@ func Test_DataSource_StagingDoesNotObserve(t *testing.T) {
t.Parallel()
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
reg.pipelines[1] = pipelineForStream(1, 1, big.NewInt(42), nil)
ds := newDataSource(logger.NullLogger, reg, telem.NullTelemeter)
ds := newDataSource(logger.NullLogger, reg, telem.NullTelemeter, testDonID, testCSAPublicKey)
defer ds.Close()

ctx, cancel := context.WithTimeout(t.Context(), observationTimeout)
Expand All @@ -1025,3 +1025,72 @@ func Test_DataSource_StagingDoesNotObserve(t *testing.T) {
require.Nil(t, vals[1], "staging instance must not populate stream values")
require.Zero(t, reg.pipelines[1].runCount.Load(), "staging instance must not run pipelines")
}

func Test_DataSource_ObservedValueGauge(t *testing.T) {
t.Parallel()
newReg := func() *mockRegistry {
reg := &mockRegistry{pipelines: make(map[streams.StreamID]*mockPipeline)}
multi := makePipelineWithMultipleStreamResults([]streams.StreamID{1, 2, 3}, []any{
decimal.NewFromInt(2181),
decimal.NewFromInt(40602),
decimal.NewFromInt(15),
})
reg.pipelines[1] = multi
reg.pipelines[2] = multi
reg.pipelines[3] = multi
return reg
}

t.Run("production records and Close deletes", func(t *testing.T) {
t.Parallel()
ds := newDataSource(logger.NullLogger, newReg(), telem.NullTelemeter, testDonID, testCSAPublicKey)
vec := newTestObservedValueVec()
ds.observedValues = newTestObservedValueGauges(vec)
opts := &mockOpts{configDigest: ocr2types.ConfigDigest{6, 5, 4}}
digest := opts.configDigest.String()
want := map[[3]string]float64{
{"1", "benchmark", digest}: 2181,
{"2", "benchmark", digest}: 40602,
{"3", "benchmark", digest}: 15,
}

require.EventuallyWithT(t, func(c *assert.CollectT) {
ctx, cancel := context.WithTimeout(t.Context(), observationTimeout)
defer cancel()
assert.NoError(c, ds.Observe(ctx, makeStreamValues(), opts))
assert.Equal(c, want, observedSeries(c, vec))
}, 5*time.Second, 50*time.Millisecond)

require.NoError(t, ds.Close())
assert.Empty(t, observedSeries(t, vec))
})

t.Run("staging does not record", func(t *testing.T) {
t.Parallel()
ds := newDataSource(logger.NullLogger, newReg(), telem.NullTelemeter, testDonID, testCSAPublicKey)
vec := newTestObservedValueVec()
ds.observedValues = newTestObservedValueGauges(vec)
defer ds.Close()
production := &mockOpts{configDigest: ocr2types.ConfigDigest{6, 5, 4}}
staging := &mockOpts{configDigest: ocr2types.ConfigDigest{9}, lifeCycleStage: lloprotocol.LifeCycleStageStaging}

// Populate the shared cache through the production instance first, so a staging Observe reads real values.
require.EventuallyWithT(t, func(c *assert.CollectT) {
ctx, cancel := context.WithTimeout(t.Context(), observationTimeout)
defer cancel()
assert.NoError(c, ds.Observe(ctx, makeStreamValues(), production))
assert.Len(c, observedSeries(c, vec), 3)
}, 5*time.Second, 50*time.Millisecond)

ctx, cancel := context.WithTimeout(t.Context(), observationTimeout)
defer cancel()
vals := makeStreamValues()
require.NoError(t, ds.Observe(ctx, vals, staging))
require.NotNil(t, vals[1], "staging Observe must read the cached value for this test to be meaningful")
series := observedSeries(t, vec)
assert.Len(t, series, 3, "staging must not add series under its own digest")
for k := range series {
assert.Equal(t, production.configDigest.String(), k[2])
}
})
}
Loading
Loading