Skip to content

Commit fe41c40

Browse files
committed
Marshal to custom message to JSON to see Beholder logs in Loki
1 parent 235758e commit fe41c40

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0
1414
github.com/go-playground/validator/v10 v10.4.1
1515
github.com/go-viper/mapstructure/v2 v2.1.0
16+
github.com/golang/protobuf v1.5.4
1617
github.com/google/go-cmp v0.6.0
1718
github.com/google/uuid v1.6.0
1819
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1
@@ -81,7 +82,6 @@ require (
8182
github.com/go-playground/universal-translator v0.17.0 // indirect
8283
github.com/goccy/go-json v0.10.3 // indirect
8384
github.com/goccy/go-yaml v1.12.0 // indirect
84-
github.com/golang/protobuf v1.5.4 // indirect
8585
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
8686
github.com/google/flatbuffers v24.3.25+incompatible // indirect
8787
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect

pkg/custmsg/custom_message.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package custmsg
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"maps"
78

8-
"google.golang.org/protobuf/proto"
9-
9+
"github.com/golang/protobuf/jsonpb"
1010
"github.com/smartcontractkit/chainlink-common/pkg/beholder"
1111
"github.com/smartcontractkit/chainlink-common/pkg/beholder/pb"
1212
)
@@ -99,18 +99,21 @@ func sendLogAsCustomMessageW(ctx context.Context, msg string, labels map[string]
9999
//if err != nil {
100100
// return fmt.Errorf("could not wrap labels to map: %w", err)
101101
//}
102-
103102
// Define a custom protobuf payload to emit
104103
payload := &pb.BaseMessage{
105104
Msg: msg,
106105
Labels: labels,
107106
}
108-
payloadBytes, err := proto.Marshal(payload)
107+
108+
// We marshal to JSON so we can read these messages in Loki
109+
marshaler := &jsonpb.Marshaler{}
110+
var buf bytes.Buffer
111+
err := marshaler.Marshal(&buf, payload)
109112
if err != nil {
110-
return fmt.Errorf("sending custom message failed to marshal protobuf: %w", err)
113+
return fmt.Errorf("sending custom message failed to marshal to JSON: %w", err)
111114
}
112115

113-
err = beholder.GetEmitter().Emit(ctx, payloadBytes,
116+
err = beholder.GetEmitter().Emit(ctx, buf.Bytes(),
114117
"beholder_data_schema", "/beholder-base-message/versions/1", // required
115118
"beholder_domain", "platform", // required
116119
"beholder_entity", "BaseMessage", // required

0 commit comments

Comments
 (0)