Skip to content

Commit

Permalink
refactor(wasm+sdk): EmitLabeler to MessageEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
MStreet3 committed Oct 22, 2024
1 parent 5792179 commit 5f29a10
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions pkg/workflows/sdk/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (

var BreakErr = capabilities.ErrStopExecution

type EmitLabeler interface {
type MessageEmitter interface {
// Emit sends a message to the labeler's destination.
Emit(string) error

// With sets the labels for the message to be emitted. Labels are passed as key-value pairs
// and are cumulative.
With(kvs ...string) EmitLabeler
With(kvs ...string) MessageEmitter
}

// Guest interface
Expand All @@ -22,7 +22,7 @@ type Runtime interface {
Fetch(req FetchRequest) (FetchResponse, error)

// Emitter sends the given message and labels to the configured collector.
Emitter() EmitLabeler
Emitter() MessageEmitter
}

type FetchRequest struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/workflows/sdk/testutils/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func (nr *NoopRuntime) Logger() logger.Logger {
return l
}

func (nr *NoopRuntime) Emitter() sdk.EmitLabeler {
func (nr *NoopRuntime) Emitter() sdk.MessageEmitter {
return nil
}
16 changes: 8 additions & 8 deletions pkg/workflows/wasm/host/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ type DeterminismConfig struct {
Seed int64
}

type EmitLabeler interface {
type MessageEmitter interface {
// Emit sends a message to the labeler's destination.
Emit(string) error

// WithMapLabels sets the labels for the message to be emitted. Labels are cumulative.
WithMapLabels(map[string]string) EmitLabeler
WithMapLabels(map[string]string) MessageEmitter
}

type ModuleConfig struct {
Expand All @@ -84,7 +84,7 @@ type ModuleConfig struct {
Fetch func(*wasmpb.FetchRequest) (*wasmpb.FetchResponse, error)

// Labeler is used to emit messages from the module.
Labeler EmitLabeler
Labeler MessageEmitter

// If Determinism is set, the module will override the random_get function in the WASI API with
// the provided seed to ensure deterministic behavior.
Expand Down Expand Up @@ -136,7 +136,7 @@ func NewModule(modCfg *ModuleConfig, binary []byte, opts ...func(*ModuleConfig))
}

if modCfg.Labeler == nil {
modCfg.Labeler = &unimplementedEmitLabeler{}
modCfg.Labeler = &unimplementedMessageEmitter{}
}

logger := modCfg.Logger
Expand Down Expand Up @@ -445,7 +445,7 @@ func fetchFn(logger logger.Logger, modCfg *ModuleConfig) func(caller *wasmtime.C
// Emit, if any, are returned in the Error Message of the response.
func createEmitFn(
l logger.Logger,
e EmitLabeler,
e MessageEmitter,
reader unsafeReaderFunc,
writer unsafeWriterFunc,
sizeWriter unsafeFixedLengthWriterFunc,
Expand Down Expand Up @@ -502,13 +502,13 @@ func createEmitFn(
}
}

type unimplementedEmitLabeler struct{}
type unimplementedMessageEmitter struct{}

func (u *unimplementedEmitLabeler) Emit(string) error {
func (u *unimplementedMessageEmitter) Emit(string) error {
return errors.New("unimplemented")
}

func (u *unimplementedEmitLabeler) WithMapLabels(map[string]string) EmitLabeler {
func (u *unimplementedMessageEmitter) WithMapLabels(map[string]string) MessageEmitter {
return u
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/workflows/wasm/host/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import (
wasmpb "github.com/smartcontractkit/chainlink-common/pkg/workflows/wasm/pb"
)

type mockEmitLabeler struct {
type mockMessageEmitter struct {
e func(string, map[string]string) error
labels map[string]string
}

func (m *mockEmitLabeler) Emit(msg string) error {
func (m *mockMessageEmitter) Emit(msg string) error {
return m.e(msg, m.labels)
}

func (m *mockEmitLabeler) WithMapLabels(labels map[string]string) EmitLabeler {
func (m *mockMessageEmitter) WithMapLabels(labels map[string]string) MessageEmitter {
m.labels = labels
return m
}

func newMockEmitLabeler(e func(string, map[string]string) error) EmitLabeler {
return &mockEmitLabeler{e: e}
func newMockMessageEmitter(e func(string, map[string]string) error) MessageEmitter {
return &mockMessageEmitter{e: e}
}

// Test_createEmitFn tests that the emit function used by the module is created correctly. Memory
Expand All @@ -37,7 +37,7 @@ func Test_createEmitFn(t *testing.T) {
t.Run("success", func(t *testing.T) {
emitFn := createEmitFn(
logger.Test(t),
newMockEmitLabeler(func(_ string, _ map[string]string) error {
newMockMessageEmitter(func(_ string, _ map[string]string) error {
return nil
}),
unsafeReaderFunc(func(_ *wasmtime.Caller, _, _ int32) ([]byte, error) {
Expand Down Expand Up @@ -70,7 +70,7 @@ func Test_createEmitFn(t *testing.T) {
t.Run("success without labels", func(t *testing.T) {
emitFn := createEmitFn(
logger.Test(t),
newMockEmitLabeler(func(_ string, _ map[string]string) error {
newMockMessageEmitter(func(_ string, _ map[string]string) error {
return nil
}),
unsafeReaderFunc(func(_ *wasmtime.Caller, _, _ int32) ([]byte, error) {
Expand Down Expand Up @@ -126,7 +126,7 @@ func Test_createEmitFn(t *testing.T) {

emitFn := createEmitFn(
logger.Test(t),
newMockEmitLabeler(func(_ string, _ map[string]string) error {
newMockMessageEmitter(func(_ string, _ map[string]string) error {
return assert.AnError
}),
unsafeReaderFunc(func(_ *wasmtime.Caller, _, _ int32) ([]byte, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/workflows/wasm/host/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func Test_Compute_Emit(t *testing.T) {
m, err := NewModule(&ModuleConfig{
Logger: lggr,
Fetch: fetchFunc,
Labeler: newMockEmitLabeler(func(msg string, kvs map[string]string) error {
Labeler: newMockMessageEmitter(func(msg string, kvs map[string]string) error {
t.Helper()

assert.Equal(t, "testing emit", msg)
Expand All @@ -247,7 +247,7 @@ func Test_Compute_Emit(t *testing.T) {
m, err := NewModule(&ModuleConfig{
Logger: lggr,
Fetch: fetchFunc,
Labeler: newMockEmitLabeler(func(msg string, kvs map[string]string) error {
Labeler: newMockMessageEmitter(func(msg string, kvs map[string]string) error {
t.Helper()

assert.Equal(t, "testing emit", msg)
Expand Down Expand Up @@ -287,7 +287,7 @@ func Test_Compute_Emit(t *testing.T) {
m, err := NewModule(&ModuleConfig{
Logger: lggr,
Fetch: fetchFunc,
Labeler: newMockEmitLabeler(func(msg string, labels map[string]string) error {
Labeler: newMockMessageEmitter(func(msg string, labels map[string]string) error {
return nil
}), // never called
}, binary)
Expand Down
4 changes: 2 additions & 2 deletions pkg/workflows/wasm/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *Runtime) Logger() logger.Logger {
return r.logger
}

func (r *Runtime) Emitter() sdk.EmitLabeler {
func (r *Runtime) Emitter() sdk.MessageEmitter {
return newWasmGuestEmitter(r.emitFn)
}

Expand All @@ -87,7 +87,7 @@ func (w wasmGuestEmitter) Emit(msg string) error {
return w.emitFn(msg, newLabels)
}

func (w wasmGuestEmitter) With(keyValues ...string) sdk.EmitLabeler {
func (w wasmGuestEmitter) With(keyValues ...string) sdk.MessageEmitter {
newEmitter := newWasmGuestEmitter(w.emitFn)
newEmitter.base = w.base.With(keyValues...)
newEmitter.labels = newEmitter.base.Labels()
Expand Down

0 comments on commit 5f29a10

Please sign in to comment.