-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
CCIP-4447 Promwrapper for OCR3 plugins and factories #15521
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
94f2275
PoC
mateusz-sekara 92eecd5
Basic implementation
mateusz-sekara a511ee9
Basic implementation
mateusz-sekara f51a9ea
Basic implementation
mateusz-sekara a680a9c
fixes
mateusz-sekara 1d18562
fixes
mateusz-sekara 54de082
fixes
mateusz-sekara 98185b3
fixes
mateusz-sekara 85a4436
fixes
mateusz-sekara 97d657f
fixes
mateusz-sekara 357f5df
fixes
mateusz-sekara 89ef5c7
fixes
mateusz-sekara c46179a
fixes
mateusz-sekara 7853a03
fixes
mateusz-sekara f663df8
fixes
mateusz-sekara dae4203
Merge remote-tracking branch 'origin/develop' into ocr3-promwrapper
mateusz-sekara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"chainlink": patch | ||
--- | ||
|
||
Prometheus observability layer added to OCR3 Reporting Plugins #internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package promwrapper | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" | ||
) | ||
|
||
var _ ocr3types.ReportingPluginFactory[any] = &ReportingPluginFactory[any]{} | ||
|
||
type ReportingPluginFactory[RI any] struct { | ||
origin ocr3types.ReportingPluginFactory[RI] | ||
chainID string | ||
plugin string | ||
} | ||
|
||
func NewReportingPluginFactory[RI any]( | ||
origin ocr3types.ReportingPluginFactory[RI], | ||
chainID string, | ||
plugin string, | ||
) *ReportingPluginFactory[RI] { | ||
return &ReportingPluginFactory[RI]{ | ||
origin: origin, | ||
chainID: chainID, | ||
plugin: plugin, | ||
} | ||
} | ||
|
||
func (r ReportingPluginFactory[RI]) NewReportingPlugin(ctx context.Context, config ocr3types.ReportingPluginConfig) (ocr3types.ReportingPlugin[RI], ocr3types.ReportingPluginInfo, error) { | ||
plugin, info, err := r.origin.NewReportingPlugin(ctx, config) | ||
if err != nil { | ||
return nil, ocr3types.ReportingPluginInfo{}, err | ||
} | ||
wrapped := newReportingPlugin( | ||
plugin, | ||
r.chainID, | ||
r.plugin, | ||
promOCR3ReportsGenerated, | ||
promOCR3Durations, | ||
) | ||
return wrapped, info, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package promwrapper | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests" | ||
) | ||
|
||
func Test_WrapperFactory(t *testing.T) { | ||
validFactory := NewReportingPluginFactory(fakeFactory[uint]{}, "solana", "plugin") | ||
failingFactory := NewReportingPluginFactory(fakeFactory[uint]{err: errors.New("error")}, "123", "plugin") | ||
|
||
plugin, _, err := validFactory.NewReportingPlugin(tests.Context(t), ocr3types.ReportingPluginConfig{}) | ||
require.NoError(t, err) | ||
|
||
_, err = plugin.Outcome(tests.Context(t), ocr3types.OutcomeContext{}, nil, nil) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, 1, counterFromHistogramByLabels(t, promOCR3Durations, "solana", "plugin", "outcome", "true")) | ||
require.Equal(t, 0, counterFromHistogramByLabels(t, promOCR3Durations, "solana", "plugin", "outcome", "false")) | ||
|
||
_, _, err = failingFactory.NewReportingPlugin(tests.Context(t), ocr3types.ReportingPluginConfig{}) | ||
require.Error(t, err) | ||
} | ||
|
||
type fakeFactory[RI any] struct { | ||
err error | ||
} | ||
|
||
func (f fakeFactory[RI]) NewReportingPlugin(context.Context, ocr3types.ReportingPluginConfig) (ocr3types.ReportingPlugin[RI], ocr3types.ReportingPluginInfo, error) { | ||
if f.err != nil { | ||
return nil, ocr3types.ReportingPluginInfo{}, f.err | ||
} | ||
return fakePlugin[RI]{}, ocr3types.ReportingPluginInfo{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package promwrapper | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" | ||
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types" | ||
) | ||
|
||
var _ ocr3types.ReportingPlugin[any] = &reportingPlugin[any]{} | ||
|
||
type reportingPlugin[RI any] struct { | ||
ocr3types.ReportingPlugin[RI] | ||
chainID string | ||
plugin string | ||
|
||
// Prometheus components for tracking metrics | ||
reportsGenerated *prometheus.CounterVec | ||
durations *prometheus.HistogramVec | ||
} | ||
|
||
func newReportingPlugin[RI any]( | ||
origin ocr3types.ReportingPlugin[RI], | ||
chainID string, | ||
plugin string, | ||
reportsGenerated *prometheus.CounterVec, | ||
durations *prometheus.HistogramVec, | ||
) *reportingPlugin[RI] { | ||
return &reportingPlugin[RI]{ | ||
ReportingPlugin: origin, | ||
chainID: chainID, | ||
plugin: plugin, | ||
reportsGenerated: reportsGenerated, | ||
durations: durations, | ||
} | ||
} | ||
|
||
func (p *reportingPlugin[RI]) Query(ctx context.Context, outctx ocr3types.OutcomeContext) (ocrtypes.Query, error) { | ||
return withObservedExecution(p, query, func() (ocrtypes.Query, error) { | ||
return p.ReportingPlugin.Query(ctx, outctx) | ||
}) | ||
} | ||
|
||
func (p *reportingPlugin[RI]) Observation(ctx context.Context, outctx ocr3types.OutcomeContext, query ocrtypes.Query) (ocrtypes.Observation, error) { | ||
return withObservedExecution(p, observation, func() (ocrtypes.Observation, error) { | ||
return p.ReportingPlugin.Observation(ctx, outctx, query) | ||
}) | ||
} | ||
|
||
func (p *reportingPlugin[RI]) ValidateObservation(ctx context.Context, outctx ocr3types.OutcomeContext, query ocrtypes.Query, ao ocrtypes.AttributedObservation) error { | ||
_, err := withObservedExecution(p, validateObservation, func() (any, error) { | ||
err := p.ReportingPlugin.ValidateObservation(ctx, outctx, query, ao) | ||
return nil, err | ||
}) | ||
return err | ||
} | ||
|
||
func (p *reportingPlugin[RI]) Outcome(ctx context.Context, outctx ocr3types.OutcomeContext, query ocrtypes.Query, aos []ocrtypes.AttributedObservation) (ocr3types.Outcome, error) { | ||
return withObservedExecution(p, outcome, func() (ocr3types.Outcome, error) { | ||
return p.ReportingPlugin.Outcome(ctx, outctx, query, aos) | ||
}) | ||
} | ||
|
||
func (p *reportingPlugin[RI]) Reports(ctx context.Context, seqNr uint64, outcome ocr3types.Outcome) ([]ocr3types.ReportPlus[RI], error) { | ||
result, err := withObservedExecution(p, reports, func() ([]ocr3types.ReportPlus[RI], error) { | ||
return p.ReportingPlugin.Reports(ctx, seqNr, outcome) | ||
}) | ||
p.trackReports(reports, len(result)) | ||
return result, err | ||
} | ||
|
||
func (p *reportingPlugin[RI]) ShouldAcceptAttestedReport(ctx context.Context, seqNr uint64, reportWithInfo ocr3types.ReportWithInfo[RI]) (bool, error) { | ||
result, err := withObservedExecution(p, shouldAccept, func() (bool, error) { | ||
return p.ReportingPlugin.ShouldAcceptAttestedReport(ctx, seqNr, reportWithInfo) | ||
}) | ||
p.trackReports(shouldAccept, boolToInt(result)) | ||
return result, err | ||
} | ||
|
||
func (p *reportingPlugin[RI]) ShouldTransmitAcceptedReport(ctx context.Context, seqNr uint64, reportWithInfo ocr3types.ReportWithInfo[RI]) (bool, error) { | ||
result, err := withObservedExecution(p, shouldTransmit, func() (bool, error) { | ||
return p.ReportingPlugin.ShouldTransmitAcceptedReport(ctx, seqNr, reportWithInfo) | ||
}) | ||
p.trackReports(shouldTransmit, boolToInt(result)) | ||
return result, err | ||
} | ||
|
||
func (p *reportingPlugin[RI]) trackReports( | ||
function functionType, | ||
count int, | ||
) { | ||
p.reportsGenerated. | ||
WithLabelValues(p.chainID, p.plugin, string(function)). | ||
Add(float64(count)) | ||
} | ||
|
||
func boolToInt(arg bool) int { | ||
if arg { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
func withObservedExecution[RI, R any]( | ||
p *reportingPlugin[RI], | ||
function functionType, | ||
exec func() (R, error), | ||
) (R, error) { | ||
start := time.Now() | ||
result, err := exec() | ||
|
||
success := err == nil | ||
|
||
p.durations. | ||
WithLabelValues(p.chainID, p.plugin, string(function), strconv.FormatBool(success)). | ||
Observe(float64(time.Since(start))) | ||
|
||
return result, err | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Does it mean that we are tracking report even when we have an error?
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.
reports should be nil or empty in that case