Repository navigation
OEV-1648 Emit whitelisted EVM chain config metric #548
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,57 @@ | ||
| package legacyevm | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/metric" | ||
| ) | ||
|
|
||
| // evmChainConfigInfoMetricName is an info-style gauge: its value is always 1 and all | ||
| // state is carried in the labels. | ||
| const evmChainConfigInfoMetricName = "evm_chain_config_info" | ||
|
|
||
| // chainConfigMetrics holds the instruments for reporting a chain's whitelisted | ||
| // configuration state. | ||
| type chainConfigMetrics struct { | ||
| configInfo metric.Int64Gauge | ||
| } | ||
|
|
||
| func newChainConfigMetrics(meter metric.Meter) (*chainConfigMetrics, error) { | ||
| configInfo, err := meter.Int64Gauge( | ||
| evmChainConfigInfoMetricName, | ||
| metric.WithDescription("Whitelisted EVM chain configuration; value is always 1, state is in the labels"), | ||
| metric.WithUnit("{info}"), | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create %s gauge: %w", evmChainConfigInfoMetricName, err) | ||
| } | ||
|
|
||
| return &chainConfigMetrics{configInfo: configInfo}, nil | ||
| } | ||
|
|
||
| // recordConfigInfo records the config gauge for a single chain. A synchronous | ||
| // gauge re-exports its last recorded value on every reader interval, so a single | ||
| // record at startup keeps the series alive. | ||
| func (m *chainConfigMetrics) recordConfigInfo(ctx context.Context, chainID string, txV2Enabled, dualBroadcast bool) { | ||
| m.configInfo.Record(ctx, 1, metric.WithAttributes(chainConfigAttributes(chainID, txV2Enabled, dualBroadcast)...)) | ||
| } | ||
|
|
||
| // chainConfigAttributes returns the exhaustive, whitelisted label set for the | ||
| // evm_chain_config_info metric for one EVM chain. | ||
| // | ||
| // The whitelist is the security boundary of this metric: it must stay limited to | ||
| // low-cardinality, non-sensitive values. In particular it must never carry an | ||
| // RPC or OFA URL (TransactionManagerV2.CustomURL/CustomURLs), because those can | ||
| // embed credentials. See docs on OEV-1648 / INCIDENT-2541. | ||
| func chainConfigAttributes(chainID string, txV2Enabled, dualBroadcast bool) []attribute.KeyValue { | ||
| return []attribute.KeyValue{ | ||
| attribute.String("chain_id", chainID), | ||
| attribute.Bool("transaction_v2_enabled", txV2Enabled), | ||
| attribute.Bool("dual_broadcast", dualBroadcast), | ||
| } | ||
| } | ||
|
|
||
| // isTrue reads an optional config bool, treating an unset value as false. | ||
| func isTrue(b *bool) bool { return b != nil && *b } |
This file contains hidden or 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,144 @@ | ||
| package legacyevm | ||
|
|
||
| import ( | ||
| stdbig "math/big" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
| "go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
|
|
||
| commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config" | ||
| "github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
| "github.com/smartcontractkit/chainlink-common/pkg/sqlutil" | ||
| "github.com/smartcontractkit/chainlink-evm/pkg/client" | ||
| "github.com/smartcontractkit/chainlink-evm/pkg/config" | ||
| "github.com/smartcontractkit/chainlink-evm/pkg/config/configtest" | ||
| "github.com/smartcontractkit/chainlink-evm/pkg/config/toml" | ||
| "github.com/smartcontractkit/chainlink-evm/pkg/heads" | ||
| "github.com/smartcontractkit/chainlink-evm/pkg/log" | ||
| "github.com/smartcontractkit/chainlink-evm/pkg/logpoller" | ||
| "github.com/smartcontractkit/chainlink-evm/pkg/txmgr" | ||
| ) | ||
|
|
||
| func TestChainConfigAttributes_exactWhitelist(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| attrs := chainConfigAttributes("1", true, false) | ||
|
|
||
| got := map[attribute.Key]attribute.Value{} | ||
| for _, kv := range attrs { | ||
| got[kv.Key] = kv.Value | ||
| } | ||
|
|
||
| // Exactly the three whitelisted keys - nothing else can leak. | ||
| require.Len(t, attrs, 3) | ||
| assert.Equal(t, "1", got["chain_id"].AsString()) | ||
| assert.True(t, got["transaction_v2_enabled"].AsBool()) | ||
| assert.False(t, got["dual_broadcast"].AsBool()) | ||
| assert.NotContains(t, got, attribute.Key("custom_url")) | ||
| assert.NotContains(t, got, attribute.Key("custom_urls")) | ||
| } | ||
|
|
||
| func TestIsTrue_nilIsFalse(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| assert.False(t, isTrue(nil)) | ||
| v := true | ||
| assert.True(t, isTrue(&v)) | ||
| } | ||
|
|
||
| func TestChainConfigMetrics_recordConfigInfo(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| reader := sdkmetric.NewManualReader() | ||
| meter := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)).Meter("test") | ||
|
|
||
| metrics, err := newChainConfigMetrics(meter) | ||
| require.NoError(t, err) | ||
|
|
||
| metrics.recordConfigInfo(t.Context(), "1", true, false) | ||
|
|
||
| dp := collectChainConfigInfo(t, reader) | ||
| assert.Equal(t, int64(1), dp.Value) | ||
| assert.Equal(t, map[string]string{ | ||
| "chain_id": "1", | ||
| "transaction_v2_enabled": "true", | ||
| "dual_broadcast": "false", | ||
| }, attrsToStrings(dp.Attributes)) | ||
| } | ||
|
|
||
| func TestChain_Start_emitsChainConfigInfo(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| reader := sdkmetric.NewManualReader() | ||
| lggr := logger.Test(t) | ||
| cfg := txV2ChainConfig(t) | ||
| c := &chain{ | ||
| id: stdbig.NewInt(42161), | ||
| cfg: cfg, | ||
| logger: lggr, | ||
| client: client.NewNullClient(stdbig.NewInt(42161), lggr), | ||
| txm: &txmgr.NullTxManager{ErrMsg: "no txm"}, | ||
| headBroadcaster: heads.NewBroadcaster(lggr), | ||
| headTracker: heads.NullTracker, | ||
| logBroadcaster: &log.NullBroadcaster{ErrMsg: "no log broadcaster"}, | ||
| logPoller: logpoller.LogPollerDisabled, | ||
| } | ||
| metrics, err := newChainConfigMetrics(sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)).Meter("test")) | ||
| require.NoError(t, err) | ||
| c.chainConfigMetrics = metrics | ||
|
|
||
| require.NoError(t, c.Start(t.Context())) | ||
| t.Cleanup(func() { assert.NoError(t, c.Close()) }) | ||
|
|
||
| dp := collectChainConfigInfo(t, reader) | ||
| assert.Equal(t, int64(1), dp.Value) | ||
| // The configured OFA URL carries a secret and must never reach the metric. | ||
| assert.Equal(t, map[string]string{ | ||
| "chain_id": "42161", | ||
| "transaction_v2_enabled": "true", | ||
| "dual_broadcast": "false", | ||
| }, attrsToStrings(dp.Attributes)) | ||
| } | ||
|
|
||
| // txV2ChainConfig is a chain config with TransactionManagerV2 enabled, dual | ||
| // broadcast off, and an OFA URL that embeds a secret. | ||
| func txV2ChainConfig(t *testing.T) *config.ChainScoped { | ||
| return configtest.NewChainScopedConfig(t, func(c *toml.EVMConfig) { | ||
| c.ChainID = sqlutil.NewI(42161) | ||
| enabled, dualBroadcast := true, false | ||
| c.Transactions.TransactionManagerV2 = toml.TransactionManagerV2Config{ | ||
| Enabled: &enabled, | ||
| DualBroadcast: &dualBroadcast, | ||
| CustomURLs: []*commonconfig.URL{commonconfig.MustParseURL("https://user:hunter2@ofa.example.com")}, | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func collectChainConfigInfo(t *testing.T, reader sdkmetric.Reader) metricdata.DataPoint[int64] { | ||
| t.Helper() | ||
|
|
||
| var rm metricdata.ResourceMetrics | ||
| require.NoError(t, reader.Collect(t.Context(), &rm)) | ||
|
|
||
| require.Len(t, rm.ScopeMetrics, 1) | ||
| require.Len(t, rm.ScopeMetrics[0].Metrics, 1) | ||
| m := rm.ScopeMetrics[0].Metrics[0] | ||
| assert.Equal(t, "evm_chain_config_info", m.Name) | ||
|
|
||
| g, ok := m.Data.(metricdata.Gauge[int64]) | ||
| require.True(t, ok, "expected an int64 gauge, got %T", m.Data) | ||
| require.Len(t, g.DataPoints, 1) | ||
| return g.DataPoints[0] | ||
| } | ||
|
|
||
| func attrsToStrings(set attribute.Set) map[string]string { | ||
| out := map[string]string{} | ||
| for _, kv := range set.ToSlice() { | ||
| out[string(kv.Key)] = kv.Value.String() | ||
| } | ||
| return out | ||
| } |
This file contains hidden or 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
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.
This TODO will still be part of the current ticket?
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.
its a chicken and egg problem. Once this lands, I can land a version of this smartcontractkit/chainlink#23726, and then undo the TODO :)
Up to you how many tickets you wanna have for the work.