Skip to content

Commit

Permalink
logger dispatcher logs payloads in json string (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
agbpatro authored Jan 11, 2024
1 parent 86e9a32 commit 9f8ce80
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion datastore/simple/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ func NewProtoLogger(logger *logrus.Logger) telemetry.Producer {

// Produce sends the data to the logger
func (p *ProtoLogger) Produce(entry *telemetry.Record) {
p.logger.Infof("logger_json_unmarshal %s %v %s\n", entry.Vin, entry.Metadata(), string(entry.Payload()))
data, err := entry.GetJSONPayload()
if err != nil {
p.logger.Errorf("json_unmarshal_error %s %v %s\n", entry.Vin, entry.Metadata(), err.Error())
return
}
p.logger.Infof("logger_json_unmarshal %s %v %s\n", entry.Vin, entry.Metadata(), string(data))
}
7 changes: 7 additions & 0 deletions telemetry/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func (record *Record) Payload() []byte {
return record.PayloadBytes
}

func (record *Record) GetJSONPayload() ([]byte, error) {
if record.transmitDecodedRecords {
return record.Payload(), nil
}
return record.toJSON()
}

// Raw returns the raw telemetry record
func (record *Record) Raw() []byte {
return record.RawBytes
Expand Down
19 changes: 19 additions & 0 deletions telemetry/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,21 @@ var _ = Describe("Socket handler test", func() {
_, err := record.GetProtoMessage()
Expect(err).To(MatchError("no mapping for txType: badTxType"))
})

It("json payload returns valid data when transmitDecodedRecords is false", func() {
message := messages.StreamMessage{TXID: []byte("1234"), SenderID: []byte("vehicle_device.42"), MessageTopic: []byte("V"), Payload: generatePayload("cybertruck", "42", nil)}
recordMsg, err := message.ToBytes()
Expect(err).NotTo(HaveOccurred())

record, err := telemetry.NewRecord(serializer, recordMsg, "1", false)
Expect(err).NotTo(HaveOccurred())
Expect(record).NotTo(BeNil())

expectedJSON := "{\"data\":[{\"key\":\"VehicleName\",\"value\":{\"stringValue\":\"cybertruck\"}}],\"createdAt\":null,\"vin\":\"42\"}"
data, err := record.GetJSONPayload()
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).To(MatchJSON(expectedJSON))
})
})

Describe("json record", func() {
Expand All @@ -280,6 +295,10 @@ var _ = Describe("Socket handler test", func() {

expectedJSON := "{\"data\":[{\"key\":\"VehicleName\",\"value\":{\"stringValue\":\"cybertruck\"}}],\"createdAt\":null,\"vin\":\"42\"}"
Expect(string(record.Payload())).To(MatchJSON(expectedJSON))

data, err := record.GetJSONPayload()
Expect(err).NotTo(HaveOccurred())
Expect(record.Payload()).To(Equal(data))
})

It("returns error on invalid txType", func() {
Expand Down

0 comments on commit 9f8ce80

Please sign in to comment.