|
| 1 | +package writetarget |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "google.golang.org/protobuf/proto" |
| 8 | + |
| 9 | + "github.com/smartcontractkit/chainlink-common/pkg/beholder" |
| 10 | + "github.com/smartcontractkit/chainlink-common/pkg/logger" |
| 11 | + |
| 12 | + "github.com/smartcontractkit/chainlink-common/pkg/beholder/monitor" |
| 13 | + |
| 14 | + "github.com/smartcontractkit/chainlink-common/pkg/capabilities/write_target/pb/data-feeds/on-chain/registry" |
| 15 | + wt "github.com/smartcontractkit/chainlink-common/pkg/capabilities/write_target/pb/platform" |
| 16 | + "github.com/smartcontractkit/chainlink-common/pkg/capabilities/write_target/pb/platform/on-chain/forwarder" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + repoCLLCommon = "https://raw.githubusercontent.com/smartcontractkit/chainlink-common" |
| 21 | + // TODO: replace with main when merged |
| 22 | + versionRefsDevelop = "refs/heads/generalized-monitoring-extraction" |
| 23 | + schemaBasePath = repoCLLCommon + "/" + versionRefsDevelop + "/pkg/capabilities/write_target/pb" |
| 24 | +) |
| 25 | + |
| 26 | +// NewWriteTargetMonitor initializes a Beholder client for the Write Target |
| 27 | +// |
| 28 | +// The client is initialized as a BeholderClient extension with a custom ProtoEmitter. |
| 29 | +// The ProtoEmitter is proxied with additional processing for emitted messages. This processing |
| 30 | +// includes decoding messages as specific types and deriving metrics based on the decoded messages. |
| 31 | +func NewWriteTargetMonitor(ctx context.Context, lggr logger.Logger) (*monitor.BeholderClient, error) { |
| 32 | + // Initialize the Beholder client with a local logger a custom Emitter |
| 33 | + client := beholder.GetClient().ForPackage("write_target") |
| 34 | + |
| 35 | + registryMetrics, err := registry.NewMetrics() |
| 36 | + if err != nil { |
| 37 | + return nil, fmt.Errorf("failed to create new registry metrics: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + forwarderMetrics, err := forwarder.NewMetrics() |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("failed to create new forwarder metrics: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + wtMetrics, err := wt.NewMetrics() |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("failed to create new write target metrics: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + // Underlying ProtoEmitter |
| 51 | + emitter := monitor.NewProtoEmitter(lggr, &client, schemaBasePath) |
| 52 | + |
| 53 | + // Proxy ProtoEmitter with additional processing |
| 54 | + protoEmitterProxy := protoEmitter{ |
| 55 | + lggr: lggr, |
| 56 | + emitter: emitter, |
| 57 | + processors: []monitor.ProtoProcessor{ |
| 58 | + &wtProcessor{wtMetrics}, |
| 59 | + &keystoneProcessor{emitter, forwarderMetrics}, |
| 60 | + &dataFeedsProcessor{emitter, registryMetrics}, |
| 61 | + }, |
| 62 | + } |
| 63 | + return &monitor.BeholderClient{Client: &client, ProtoEmitter: &protoEmitterProxy}, nil |
| 64 | +} |
| 65 | + |
| 66 | +// ProtoEmitter proxy specific to the WT |
| 67 | +type protoEmitter struct { |
| 68 | + lggr logger.Logger |
| 69 | + emitter monitor.ProtoEmitter |
| 70 | + processors []monitor.ProtoProcessor |
| 71 | +} |
| 72 | + |
| 73 | +// Emit emits a proto.Message and runs additional processing |
| 74 | +func (e *protoEmitter) Emit(ctx context.Context, m proto.Message, attrKVs ...any) error { |
| 75 | + err := e.emitter.Emit(ctx, m, attrKVs...) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("failed to emit: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + // Notice: we skip processing errors (and continue) so this will never error |
| 81 | + return e.Process(ctx, m, attrKVs...) |
| 82 | +} |
| 83 | + |
| 84 | +// EmitWithLog emits a proto.Message and runs additional processing |
| 85 | +func (e *protoEmitter) EmitWithLog(ctx context.Context, m proto.Message, attrKVs ...any) error { |
| 86 | + err := e.emitter.EmitWithLog(ctx, m, attrKVs...) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to emit with log: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + // Notice: we skip processing errors (and continue) so this will never error |
| 92 | + return e.Process(ctx, m, attrKVs...) |
| 93 | +} |
| 94 | + |
| 95 | +// Process aggregates further processing for emitted messages |
| 96 | +func (e *protoEmitter) Process(ctx context.Context, m proto.Message, attrKVs ...any) error { |
| 97 | + // Further processing for emitted messages |
| 98 | + for _, p := range e.processors { |
| 99 | + err := p.Process(ctx, m, attrKVs...) |
| 100 | + if err != nil { |
| 101 | + // Notice: we swallow and log processing errors |
| 102 | + // These should be investigated and fixed, but are not critical to product runtime, |
| 103 | + // and shouldn't block further processing of the emitted message. |
| 104 | + e.lggr.Errorw("failed to process emitted message", "err", err) |
| 105 | + return nil |
| 106 | + } |
| 107 | + } |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// Write-Target specific processor decodes write messages to derive metrics |
| 112 | +type wtProcessor struct { |
| 113 | + metrics *wt.Metrics |
| 114 | +} |
| 115 | + |
| 116 | +func (p *wtProcessor) Process(ctx context.Context, m proto.Message, attrKVs ...any) error { |
| 117 | + // Switch on the type of the proto.Message |
| 118 | + switch msg := m.(type) { |
| 119 | + case *wt.WriteInitiated: |
| 120 | + err := p.metrics.OnWriteInitiated(ctx, msg, attrKVs...) |
| 121 | + if err != nil { |
| 122 | + return fmt.Errorf("failed to publish write initiated metrics: %w", err) |
| 123 | + } |
| 124 | + return nil |
| 125 | + case *wt.WriteError: |
| 126 | + err := p.metrics.OnWriteError(ctx, msg, attrKVs...) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("failed to publish write error metrics: %w", err) |
| 129 | + } |
| 130 | + return nil |
| 131 | + case *wt.WriteSent: |
| 132 | + err := p.metrics.OnWriteSent(ctx, msg, attrKVs...) |
| 133 | + if err != nil { |
| 134 | + return fmt.Errorf("failed to publish write sent metrics: %w", err) |
| 135 | + } |
| 136 | + return nil |
| 137 | + case *wt.WriteConfirmed: |
| 138 | + err := p.metrics.OnWriteConfirmed(ctx, msg, attrKVs...) |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("failed to publish write confirmed metrics: %w", err) |
| 141 | + } |
| 142 | + return nil |
| 143 | + default: |
| 144 | + return nil // fallthrough |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// Keystone specific processor decodes writes as 'platform.forwarder.ReportProcessed' messages + metrics |
| 149 | +type keystoneProcessor struct { |
| 150 | + emitter monitor.ProtoEmitter |
| 151 | + metrics *forwarder.Metrics |
| 152 | +} |
| 153 | + |
| 154 | +func (p *keystoneProcessor) Process(ctx context.Context, m proto.Message, attrKVs ...any) error { |
| 155 | + // Switch on the type of the proto.Message |
| 156 | + switch msg := m.(type) { |
| 157 | + case *wt.WriteConfirmed: |
| 158 | + // TODO: detect the type of write payload (support more than one type of write, first multiple Keystone report versions) |
| 159 | + // https://smartcontract-it.atlassian.net/browse/NONEVM-817 |
| 160 | + // Q: Will this msg ever contain different (non-Keystone) types of writes? Hmm. |
| 161 | + // Notice: we assume all writes are Keystone (v1) writes for now |
| 162 | + |
| 163 | + // Decode as a 'platform.forwarder.ReportProcessed' message |
| 164 | + reportProcessed, err := forwarder.DecodeAsReportProcessed(msg) |
| 165 | + if err != nil { |
| 166 | + return fmt.Errorf("failed to decode as 'platform.forwarder.ReportProcessed': %w", err) |
| 167 | + } |
| 168 | + // Emit the 'platform.forwarder.ReportProcessed' message |
| 169 | + err = p.emitter.EmitWithLog(ctx, reportProcessed, attrKVs...) |
| 170 | + if err != nil { |
| 171 | + return fmt.Errorf("failed to emit with log: %w", err) |
| 172 | + } |
| 173 | + // Process emit and derive metrics |
| 174 | + err = p.metrics.OnReportProcessed(ctx, reportProcessed, attrKVs...) |
| 175 | + if err != nil { |
| 176 | + return fmt.Errorf("failed to publish report processed metrics: %w", err) |
| 177 | + } |
| 178 | + return nil |
| 179 | + default: |
| 180 | + return nil // fallthrough |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +// Data-Feeds specific processor decodes writes as 'data-feeds.registry.FeedUpdated' messages + metrics |
| 185 | +type dataFeedsProcessor struct { |
| 186 | + emitter monitor.ProtoEmitter |
| 187 | + metrics *registry.Metrics |
| 188 | +} |
| 189 | + |
| 190 | +func (p *dataFeedsProcessor) Process(ctx context.Context, m proto.Message, attrKVs ...any) error { |
| 191 | + // Switch on the type of the proto.Message |
| 192 | + switch msg := m.(type) { |
| 193 | + case *wt.WriteConfirmed: |
| 194 | + // TODO: fallthrough if not a write containing a DF report |
| 195 | + // https://smartcontract-it.atlassian.net/browse/NONEVM-818 |
| 196 | + // Notice: we assume all writes are Data-Feeds (static schema) writes for now |
| 197 | + |
| 198 | + // Decode as an array of 'data-feeds.registry.FeedUpdated' messages |
| 199 | + updates, err := registry.DecodeAsFeedUpdated(msg) |
| 200 | + if err != nil { |
| 201 | + return fmt.Errorf("failed to decode as 'data-feeds.registry.FeedUpdated': %w", err) |
| 202 | + } |
| 203 | + // Emit the 'data-feeds.registry.FeedUpdated' messages |
| 204 | + for _, update := range updates { |
| 205 | + err = p.emitter.EmitWithLog(ctx, update, attrKVs...) |
| 206 | + if err != nil { |
| 207 | + return fmt.Errorf("failed to emit with log: %w", err) |
| 208 | + } |
| 209 | + // Process emit and derive metrics |
| 210 | + err = p.metrics.OnFeedUpdated(ctx, update, attrKVs...) |
| 211 | + if err != nil { |
| 212 | + return fmt.Errorf("failed to publish feed updated metrics: %w", err) |
| 213 | + } |
| 214 | + } |
| 215 | + return nil |
| 216 | + default: |
| 217 | + return nil // fallthrough |
| 218 | + } |
| 219 | +} |
0 commit comments