-
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.
[BCF-2727] Add telemetry adapter service (#11125)
- Loading branch information
1 parent
7cadec0
commit 8fba9c8
Showing
8 changed files
with
173 additions
and
9 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
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,56 @@ | ||
package generic | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/smartcontractkit/libocr/commontypes" | ||
|
||
"github.com/smartcontractkit/chainlink-relay/pkg/types" | ||
) | ||
|
||
var _ types.TelemetryService = (*TelemetryAdapter)(nil) | ||
|
||
type TelemetryAdapter struct { | ||
endpointGenerator types.MonitoringEndpointGenerator | ||
endpoints map[[4]string]commontypes.MonitoringEndpoint | ||
} | ||
|
||
func NewTelemetryAdapter(endpointGen types.MonitoringEndpointGenerator) *TelemetryAdapter { | ||
return &TelemetryAdapter{ | ||
endpoints: make(map[[4]string]commontypes.MonitoringEndpoint), | ||
endpointGenerator: endpointGen, | ||
} | ||
} | ||
|
||
func (t *TelemetryAdapter) Send(ctx context.Context, network string, chainID string, contractID string, telemetryType string, payload []byte) error { | ||
e, err := t.getOrCreateEndpoint(contractID, telemetryType, network, chainID) | ||
if err != nil { | ||
return err | ||
} | ||
e.SendLog(payload) | ||
return nil | ||
} | ||
|
||
func (t *TelemetryAdapter) getOrCreateEndpoint(contractID string, telemetryType string, network string, chainID string) (commontypes.MonitoringEndpoint, error) { | ||
if contractID == "" { | ||
return nil, errors.New("contractID cannot be empty") | ||
} | ||
if telemetryType == "" { | ||
return nil, errors.New("telemetryType cannot be empty") | ||
} | ||
if network == "" { | ||
return nil, errors.New("network cannot be empty") | ||
} | ||
if chainID == "" { | ||
return nil, errors.New("chainID cannot be empty") | ||
} | ||
|
||
key := [4]string{network, chainID, contractID, telemetryType} | ||
e, ok := t.endpoints[key] | ||
if !ok { | ||
e = t.endpointGenerator.GenMonitoringEndpoint(network, chainID, contractID, telemetryType) | ||
t.endpoints[key] = e | ||
} | ||
return e, nil | ||
} |
108 changes: 108 additions & 0 deletions
108
core/services/ocr2/plugins/generic/telemetry_adapter_test.go
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,108 @@ | ||
package generic | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/smartcontractkit/libocr/commontypes" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockEndpoint struct { | ||
network string | ||
chainID string | ||
contractID string | ||
telemetryType string | ||
payload []byte | ||
} | ||
|
||
func (m *mockEndpoint) SendLog(payload []byte) { m.payload = payload } | ||
|
||
type mockGenerator struct{} | ||
|
||
func (m *mockGenerator) GenMonitoringEndpoint(network, chainID, contractID, telemetryType string) commontypes.MonitoringEndpoint { | ||
return &mockEndpoint{ | ||
network: network, | ||
chainID: chainID, | ||
contractID: contractID, | ||
telemetryType: telemetryType, | ||
} | ||
} | ||
|
||
func TestTelemetryAdapter(t *testing.T) { | ||
ta := NewTelemetryAdapter(&mockGenerator{}) | ||
|
||
tests := []struct { | ||
name string | ||
contractID string | ||
telemetryType string | ||
networkID string | ||
chainID string | ||
payload []byte | ||
errorMsg string | ||
}{ | ||
{ | ||
name: "valid request", | ||
contractID: "contract", | ||
telemetryType: "mercury", | ||
networkID: "solana", | ||
chainID: "1337", | ||
payload: []byte("uh oh"), | ||
}, | ||
{ | ||
name: "no valid contractID", | ||
telemetryType: "mercury", | ||
networkID: "solana", | ||
chainID: "1337", | ||
payload: []byte("uh oh"), | ||
errorMsg: "contractID cannot be empty", | ||
}, | ||
{ | ||
name: "no valid chainID", | ||
contractID: "contract", | ||
telemetryType: "mercury", | ||
networkID: "solana", | ||
payload: []byte("uh oh"), | ||
errorMsg: "chainID cannot be empty", | ||
}, | ||
{ | ||
name: "no valid telemetryType", | ||
contractID: "contract", | ||
networkID: "solana", | ||
chainID: "1337", | ||
payload: []byte("uh oh"), | ||
errorMsg: "telemetryType cannot be empty", | ||
}, | ||
{ | ||
name: "no valid network", | ||
contractID: "contract", | ||
telemetryType: "mercury", | ||
chainID: "1337", | ||
payload: []byte("uh oh"), | ||
errorMsg: "network cannot be empty", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := ta.Send(context.Background(), test.networkID, test.chainID, test.contractID, test.telemetryType, test.payload) | ||
if test.errorMsg != "" { | ||
assert.ErrorContains(t, err, test.errorMsg) | ||
} else { | ||
require.NoError(t, err) | ||
key := [4]string{test.networkID, test.chainID, test.contractID, test.telemetryType} | ||
fmt.Printf("%+v", ta.endpoints) | ||
endpoint, ok := ta.endpoints[key] | ||
require.True(t, ok) | ||
|
||
me := endpoint.(*mockEndpoint) | ||
assert.Equal(t, test.networkID, me.network) | ||
assert.Equal(t, test.chainID, me.chainID) | ||
assert.Equal(t, test.contractID, me.contractID) | ||
assert.Equal(t, test.telemetryType, me.telemetryType) | ||
assert.Equal(t, test.payload, me.payload) | ||
} | ||
}) | ||
} | ||
} |
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
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