-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving core Labeler functionality to core/monitoring. Consuming in wo…
…rkflows and registrysyncer packages
- Loading branch information
1 parent
be6a82e
commit 2be3b36
Showing
15 changed files
with
319 additions
and
224 deletions.
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,127 @@ | ||
package monitoring | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/smartcontractkit/chainlink-common/pkg/beholder" | ||
beholderpb "github.com/smartcontractkit/chainlink-common/pkg/beholder/pb" | ||
valuespb "github.com/smartcontractkit/chainlink-common/pkg/values/pb" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
type CustomMessageLabeler struct { | ||
labels map[string]string | ||
} | ||
|
||
func NewCustomMessageLabeler() CustomMessageLabeler { | ||
return CustomMessageLabeler{labels: make(map[string]string)} | ||
} | ||
|
||
// With adds multiple key-value pairs to the CustomMessageLabeler for transmission With SendLogAsCustomMessage | ||
func (c CustomMessageLabeler) With(keyValues ...string) CustomMessageLabeler { | ||
newCustomMessageLabeler := NewCustomMessageLabeler() | ||
|
||
if len(keyValues)%2 != 0 { | ||
// If an odd number of key-value arguments is passed, return the original CustomMessageLabeler unchanged | ||
return c | ||
} | ||
|
||
// Copy existing labels from the current agent | ||
for k, v := range c.labels { | ||
newCustomMessageLabeler.labels[k] = v | ||
} | ||
|
||
// Add new key-value pairs | ||
for i := 0; i < len(keyValues); i += 2 { | ||
key := keyValues[i] | ||
value := keyValues[i+1] | ||
newCustomMessageLabeler.labels[key] = value | ||
} | ||
|
||
return newCustomMessageLabeler | ||
} | ||
|
||
// SendLogAsCustomMessage emits a BaseMessage With msg and labels as data. | ||
// any key in labels that is not part of orderedLabelKeys will not be transmitted | ||
func (c CustomMessageLabeler) SendLogAsCustomMessage(msg string) error { | ||
return sendLogAsCustomMessageW(msg, c.labels) | ||
} | ||
|
||
type MetricsLabeler struct { | ||
Labels map[string]string | ||
} | ||
|
||
func NewMetricsLabeler() MetricsLabeler { | ||
return MetricsLabeler{Labels: make(map[string]string)} | ||
} | ||
|
||
// With adds multiple key-value pairs to the CustomMessageLabeler for transmission With SendLogAsCustomMessage | ||
func (c MetricsLabeler) With(keyValues ...string) MetricsLabeler { | ||
newCustomMetricsLabeler := NewMetricsLabeler() | ||
|
||
if len(keyValues)%2 != 0 { | ||
// If an odd number of key-value arguments is passed, return the original CustomMessageLabeler unchanged | ||
return c | ||
} | ||
|
||
// Copy existing labels from the current agent | ||
for k, v := range c.Labels { | ||
newCustomMetricsLabeler.Labels[k] = v | ||
} | ||
|
||
// Add new key-value pairs | ||
for i := 0; i < len(keyValues); i += 2 { | ||
key := keyValues[i] | ||
value := keyValues[i+1] | ||
newCustomMetricsLabeler.Labels[key] = value | ||
} | ||
|
||
return newCustomMetricsLabeler | ||
} | ||
|
||
// sendLogAsCustomMessageF formats into a msg to be consumed by sendLogAsCustomMessageW | ||
func sendLogAsCustomMessageF(labels map[string]string, format string, values ...any) error { | ||
return sendLogAsCustomMessageW(fmt.Sprintf(format, values...), labels) | ||
} | ||
|
||
// sendLogAsCustomMessageV allows the consumer to pass in variable number of label key value pairs | ||
func sendLogAsCustomMessageV(msg string, labelKVs ...string) error { | ||
if len(labelKVs)%2 != 0 { | ||
return fmt.Errorf("labelKVs must be provided in key-value pairs") | ||
} | ||
|
||
labels := make(map[string]string) | ||
for i := 0; i < len(labelKVs); i += 2 { | ||
key := labelKVs[i] | ||
value := labelKVs[i+1] | ||
labels[key] = value | ||
} | ||
|
||
return sendLogAsCustomMessageF(labels, msg) | ||
} | ||
|
||
func sendLogAsCustomMessageW(msg string, labels map[string]string) error { | ||
protoLabels := make(map[string]*valuespb.Value) | ||
for _, l := range labels { | ||
protoLabels[l] = &valuespb.Value{Value: &valuespb.Value_StringValue{StringValue: labels[l]}} | ||
} | ||
// Define a custom protobuf payload to emit | ||
payload := &beholderpb.BaseMessage{ | ||
Msg: msg, | ||
Labels: protoLabels, | ||
} | ||
payloadBytes, err := proto.Marshal(payload) | ||
if err != nil { | ||
return fmt.Errorf("sending custom message failed to marshal protobuf: %w", err) | ||
} | ||
|
||
err = beholder.GetEmitter().Emit(context.Background(), payloadBytes, | ||
"beholder_data_schema", "/beholder-base-message/versions/1", // required | ||
"beholder_data_type", "custom_message", | ||
) | ||
if err != nil { | ||
return fmt.Errorf("sending custom message failed on emit: %w", err) | ||
} | ||
|
||
return 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,15 @@ | ||
package monitoring | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
// tests CustomMessageAgent does not share state across new instances created by `With` | ||
func Test_CustomMessageAgent(t *testing.T) { | ||
cma := NewCustomMessageLabeler() | ||
cma1 := cma.With("key1", "value1") | ||
cma2 := cma1.With("key2", "value2") | ||
|
||
assert.NotEqual(t, cma1.labels, cma2.labels) | ||
} |
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,11 @@ | ||
package monitoring | ||
|
||
import "go.opentelemetry.io/otel/attribute" | ||
|
||
func KvMapToOtelAttributes(kvmap map[string]string) []attribute.KeyValue { | ||
otelKVs := make([]attribute.KeyValue, len(kvmap)) | ||
for k, v := range kvmap { | ||
otelKVs = append(otelKVs, attribute.String(k, v)) | ||
} | ||
return otelKVs | ||
} |
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,7 @@ | ||
package monitoring | ||
|
||
import "testing" | ||
|
||
func Test_KvMapToOtelAttributes(t *testing.T) { | ||
|
||
} |
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,46 @@ | ||
package registrysyncer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/smartcontractkit/chainlink-common/pkg/beholder" | ||
"github.com/smartcontractkit/chainlink/v2/core/monitoring" | ||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
var remoteRegistrySyncFailureCounter metric.Int64Counter | ||
var launcherFailureCounter metric.Int64Counter | ||
|
||
func initMonitoringResources() (err error) { | ||
remoteRegistrySyncFailureCounter, err = beholder.GetMeter().Int64Counter("RemoteRegistrySyncFailure") | ||
if err != nil { | ||
return fmt.Errorf("failed to register sync failure counter: %w", err) | ||
} | ||
|
||
launcherFailureCounter, err = beholder.GetMeter().Int64Counter("LauncherFailureCounter") | ||
if err != nil { | ||
return fmt.Errorf("failed to register launcher failure counter: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// syncerMetricLabeler wraps monitoring.MetricsLabeler to provide workflow specific utilities | ||
// for monitoring resources | ||
type syncerMetricLabeler struct { | ||
monitoring.MetricsLabeler | ||
} | ||
|
||
func (c syncerMetricLabeler) with(keyValues ...string) syncerMetricLabeler { | ||
return syncerMetricLabeler{c.With(keyValues...)} | ||
} | ||
|
||
func (c syncerMetricLabeler) incrementRemoteRegistryFailureCounter(ctx context.Context) { | ||
otelLabels := monitoring.KvMapToOtelAttributes(c.Labels) | ||
remoteRegistrySyncFailureCounter.Add(ctx, 1, metric.WithAttributes(otelLabels...)) | ||
} | ||
|
||
func (c syncerMetricLabeler) incrementLauncherFailureCounter(ctx context.Context) { | ||
otelLabels := monitoring.KvMapToOtelAttributes(c.Labels) | ||
launcherFailureCounter.Add(ctx, 1, metric.WithAttributes(otelLabels...)) | ||
} |
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,17 @@ | ||
package registrysyncer | ||
|
||
import ( | ||
"github.com/smartcontractkit/chainlink/v2/core/monitoring" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func Test_InitMonitoringResources(t *testing.T) { | ||
require.NoError(t, initMonitoringResources()) | ||
} | ||
|
||
func Test_SyncerMetricsLabeler(t *testing.T) { | ||
testSyncerMetricLabeler := syncerMetricLabeler{monitoring.NewMetricsLabeler()} | ||
testSyncerMetricLabeler2 := testSyncerMetricLabeler.with("foo", "baz") | ||
require.EqualValues(t, testSyncerMetricLabeler2.Labels["foo"], "baz") | ||
} |
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
Oops, something went wrong.