@@ -3,70 +3,56 @@ package internal
33import (
44 "context"
55 "errors"
6- "fmt"
76
8- "github.com/smartcontractkit/libocr/commontypes"
97 "google.golang.org/grpc"
108 "google.golang.org/protobuf/types/known/emptypb"
119
12- "github.com/smartcontractkit/chainlink-relay/pkg/logger"
1310 "github.com/smartcontractkit/chainlink-relay/pkg/loop/internal/pb"
1411 "github.com/smartcontractkit/chainlink-relay/pkg/types"
1512)
1613
17- var _ types.Telemetry = (* telemetryClient )(nil )
18- var _ types.MonitoringEndpointGenerator = (* telemetryClient )(nil )
19- var _ commontypes.MonitoringEndpoint = (* telemetryEndpoint )(nil )
14+ var _ types.TelemetryClient = (* telemetryClient )(nil )
2015
2116type TelemetryClient struct {
2217 * telemetryClient
2318}
2419
2520type telemetryClient struct {
2621 grpc pb.TelemetryClient
27-
28- lggr logger.Logger
2922}
3023
3124type telemetryEndpoint struct {
32- lggr logger.Logger
33-
3425 grpc pb.TelemetryClient
3526 relayID pb.RelayID
3627 contractID string
3728 telemetryType string
3829}
3930
40- func (t * telemetryEndpoint ) SendLog (log []byte ) {
41- _ , err := t .grpc .Send (context . Background () , & pb.TelemetryMessage {
31+ func (t * telemetryEndpoint ) SendLog (ctx context. Context , log []byte ) error {
32+ _ , err := t .grpc .Send (ctx , & pb.TelemetryMessage {
4233 RelayID : & t .relayID ,
4334 ContractID : t .contractID ,
4435 TelemetryType : t .telemetryType ,
4536 Payload : log ,
4637 })
47- if err != nil {
48- t .lggr .Errorw ("cannot send telemetry" , "err" , err )
49- }
38+ return err
5039}
5140
52- // GenMonitoringEndpoint generates a new monitoring endpoint, returns nil if one cannot be generated
53- func (t * telemetryClient ) GenMonitoringEndpoint ( network string , chainID string , contractID string , telemetryType string ) commontypes. MonitoringEndpoint {
41+ // NewEndpoint generates a new monitoring endpoint, returns nil if one cannot be generated
42+ func (t * telemetryClient ) NewEndpoint ( ctx context. Context , network string , chainID string , contractID string , telemetryType string ) (types. TelemetryClientEndpoint , error ) {
5443 if contractID == "" {
55- t .lggr .Errorw ("cannot generate monitoring endpoint, contractID is empty" , "contractID" , contractID , "telemetryType" , telemetryType , "network" , network , "chainID" , chainID )
56- return nil
44+ return nil , errors .New ("contractID cannot be empty" )
5745 }
5846 if telemetryType == "" {
59- t .lggr .Errorw ("cannot generate monitoring endpoint, telemetryType is empty" , "contractID" , contractID , "telemetryType" , telemetryType , "network" , network , "chainID" , chainID )
60- return nil
47+ return nil , errors .New ("telemetryType cannot be empty" )
6148 }
6249 if network == "" {
63- t .lggr .Errorw ("cannot generate monitoring endpoint, network is empty" , "contractID" , contractID , "telemetryType" , telemetryType , "network" , network , "chainID" , chainID )
64- return nil
50+ return nil , errors .New ("network cannot be empty" )
6551 }
6652 if chainID == "" {
67- t .lggr .Errorw ("cannot generate monitoring endpoint, chainID is empty" , "contractID" , contractID , "telemetryType" , telemetryType , "network" , network , "chainID" , chainID )
68- return nil
53+ return nil , errors .New ("chainId cannot be empty" )
6954 }
55+
7056 return & telemetryEndpoint {
7157 grpc : t .grpc ,
7258 relayID : pb.RelayID {
@@ -75,8 +61,7 @@ func (t *telemetryClient) GenMonitoringEndpoint(network string, chainID string,
7561 },
7662 contractID : contractID ,
7763 telemetryType : telemetryType ,
78- lggr : t .lggr ,
79- }
64+ }, nil
8065}
8166
8267// Send sends payload to the desired endpoint based on network and chainID
@@ -111,58 +96,28 @@ func (t *telemetryClient) Send(ctx context.Context, network string, chainID stri
11196 return nil
11297}
11398
114- func NewTelemetryClient (cc grpc.ClientConnInterface , lggr logger. Logger ) * telemetryClient {
115- return & telemetryClient {grpc : pb .NewTelemetryClient (cc ), lggr : lggr }
99+ func NewTelemetryClient (cc grpc.ClientConnInterface ) * telemetryClient {
100+ return & telemetryClient {grpc : pb .NewTelemetryClient (cc )}
116101}
117102
118103var _ pb.TelemetryServer = (* telemetryServer )(nil )
119104
120105type telemetryServer struct {
121106 pb.UnimplementedTelemetryServer
122107
123- impl types.MonitoringEndpointGenerator
124- endpoints map [string ]commontypes.MonitoringEndpoint
108+ impl types.TelemetryService
125109}
126110
127111func (t * telemetryServer ) Send (ctx context.Context , message * pb.TelemetryMessage ) (* emptypb.Empty , error ) {
128- e , err := t .getOrCreateEndpoint (message )
129- if err != nil {
130- return nil , err
131- }
132- e .SendLog (message .Payload )
133-
134- return nil , nil
135- }
136-
137- func (t * telemetryServer ) getOrCreateEndpoint (m * pb.TelemetryMessage ) (commontypes.MonitoringEndpoint , error ) {
138- if m .ContractID == "" {
139- return nil , errors .New ("contractID cannot be empty" )
140- }
141- if m .TelemetryType == "" {
142- return nil , errors .New ("telemetryType cannot be empty" )
143- }
144- if m .RelayID == nil {
145- return nil , errors .New ("RelayID cannot be nil" )
112+ var network , chainID string
113+ if message .RelayID != nil {
114+ network = message .RelayID .Network
115+ chainID = message .RelayID .ChainId
146116 }
147- if m .RelayID .Network == "" {
148- return nil , errors .New ("RelayID.Network cannot be empty" )
149- }
150- if m .RelayID .ChainId == "" {
151- return nil , errors .New ("RelayID.ChainId cannot be empty" )
152- }
153-
154- key := makeKey (m )
155- e , ok := t .endpoints [key ]
156- if ! ok {
157- e = t .impl .GenMonitoringEndpoint (m .RelayID .Network , m .RelayID .ChainId , m .ContractID , m .TelemetryType )
158- }
159- return e , nil
160- }
161-
162- func makeKey (m * pb.TelemetryMessage ) string {
163- return fmt .Sprintf ("%s_%s_%s_%s" , m .RelayID .Network , m .RelayID .ChainId , m .ContractID , m .TelemetryType )
117+ err := t .impl .Send (ctx , network , chainID , message .ContractID , message .TelemetryType , message .Payload )
118+ return & emptypb.Empty {}, err
164119}
165120
166- func NewTelemetryServer (impl types.MonitoringEndpointGenerator ) * telemetryServer {
121+ func NewTelemetryServer (impl types.TelemetryService ) * telemetryServer {
167122 return & telemetryServer {impl : impl }
168123}
0 commit comments