Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Trace field in DecoderIn protobuf message #228

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/go/message/thingspect_decoder_in.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/protobuf/message/thingspect_decoder_in.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ message DecoderIn {
google.protobuf.Timestamp ts = 3;

// Trace ID (UUID).
string trace_id = 4;
bytes trace_id = 4;
}
10 changes: 8 additions & 2 deletions internal/atlas-decoder/decoder/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"time"

"github.com/google/uuid"
"github.com/thingspect/atlas/api/go/message"
"github.com/thingspect/atlas/pkg/alog"
"github.com/thingspect/atlas/pkg/dao"
Expand Down Expand Up @@ -38,9 +39,13 @@ func (dec *Decoder) decodeMessages() {
continue
}

// Trace IDs have been authenticated and are safe to copy.
var traceID uuid.UUID
copy(traceID[:], dIn.TraceId)

// Set up logging fields.
logger := alog.
WithField("traceID", dIn.TraceId).
WithField("traceID", traceID.String()).
WithField("uniqID", dIn.UniqId)

// Retrieve device.
Expand Down Expand Up @@ -77,7 +82,8 @@ func (dec *Decoder) decodeMessages() {
// Build and publish ValidatorIn messages.
var successCount int
for _, point := range points {
vIn := registry.PointToVIn(dIn.TraceId, dIn.UniqId, point, dIn.Ts)
vIn := registry.PointToVIn(traceID.String(), dIn.UniqId, point,
dIn.Ts)

bVIn, err := proto.Marshal(vIn)
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions internal/atlas-decoder/decoder/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestDecodeMessages(t *testing.T) {

uniqID := "dec-" + random.String(16)
now := timestamppb.New(time.Now().Add(-15 * time.Minute))
traceID := uuid.NewString()
traceID := uuid.New()

tests := []struct {
inpDIn *message.DecoderIn
Expand All @@ -39,39 +39,39 @@ func TestDecodeMessages(t *testing.T) {
{
&message.DecoderIn{
UniqId: uniqID, Data: []byte{0x19, 0x03, 0x01}, Ts: now,
TraceId: traceID,
TraceId: traceID[:],
}, api.Decoder_RADIO_BRIDGE_DOOR_V1, []*message.ValidatorIn{
{
Point: &common.DataPoint{
UniqId: uniqID, Attr: "count",
ValOneof: &common.DataPoint_IntVal{IntVal: 9}, Ts: now,
TraceId: traceID,
TraceId: traceID.String(),
}, SkipToken: true,
}, {
Point: &common.DataPoint{
UniqId: uniqID, Attr: "open",
ValOneof: &common.DataPoint_BoolVal{BoolVal: true},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
},
},
{
&message.DecoderIn{
UniqId: uniqID, Data: []byte{0x1a, 0x03, 0x00}, Ts: now,
TraceId: traceID,
TraceId: traceID[:],
}, api.Decoder_RADIO_BRIDGE_DOOR_V2, []*message.ValidatorIn{
{
Point: &common.DataPoint{
UniqId: uniqID, Attr: "count",
ValOneof: &common.DataPoint_IntVal{IntVal: 10}, Ts: now,
TraceId: traceID,
TraceId: traceID.String(),
}, SkipToken: true,
}, {
Point: &common.DataPoint{
UniqId: uniqID, Attr: "open",
ValOneof: &common.DataPoint_BoolVal{BoolVal: false},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
},
Expand All @@ -80,34 +80,34 @@ func TestDecodeMessages(t *testing.T) {
&message.DecoderIn{
UniqId: uniqID, Data: []byte{
0x01, 0x09, 0x61, 0x13, 0x95, 0x02, 0x92,
}, Ts: now, TraceId: traceID,
}, Ts: now, TraceId: traceID[:],
}, api.Decoder_GLOBALSAT_CO2, []*message.ValidatorIn{
{
Point: &common.DataPoint{
UniqId: uniqID, Attr: "temp_c",
ValOneof: &common.DataPoint_Fl64Val{Fl64Val: 24},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
{
Point: &common.DataPoint{
UniqId: uniqID, Attr: "temp_f",
ValOneof: &common.DataPoint_Fl64Val{Fl64Val: 75.2},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
{
Point: &common.DataPoint{
UniqId: uniqID, Attr: "humidity_pct",
ValOneof: &common.DataPoint_Fl64Val{Fl64Val: 50.13},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
{
Point: &common.DataPoint{
UniqId: uniqID, Attr: "co2_ppm",
ValOneof: &common.DataPoint_IntVal{IntVal: 658},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
},
Expand Down
32 changes: 16 additions & 16 deletions internal/atlas-decoder/test/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const testTimeout = 6 * time.Second

func TestDecodeMessages(t *testing.T) {
now := timestamppb.New(time.Now().Add(-15 * time.Minute))
traceID := uuid.NewString()
traceID := uuid.New()

ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()
Expand Down Expand Up @@ -53,39 +53,39 @@ func TestDecodeMessages(t *testing.T) {
{
&message.DecoderIn{
UniqId: doorDev.UniqId, Data: []byte{0x19, 0x03, 0x01}, Ts: now,
TraceId: traceID,
TraceId: traceID[:],
}, []*message.ValidatorIn{
{
Point: &common.DataPoint{
UniqId: doorDev.UniqId, Attr: "count",
ValOneof: &common.DataPoint_IntVal{IntVal: 9}, Ts: now,
TraceId: traceID,
TraceId: traceID.String(),
}, SkipToken: true,
}, {
Point: &common.DataPoint{
UniqId: doorDev.UniqId, Attr: "open",
ValOneof: &common.DataPoint_BoolVal{BoolVal: true},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
},
},
{
&message.DecoderIn{
UniqId: doorDev.UniqId, Data: []byte{0x1a, 0x03, 0x00}, Ts: now,
TraceId: traceID,
TraceId: traceID[:],
}, []*message.ValidatorIn{
{
Point: &common.DataPoint{
UniqId: doorDev.UniqId, Attr: "count",
ValOneof: &common.DataPoint_IntVal{IntVal: 10}, Ts: now,
TraceId: traceID,
TraceId: traceID.String(),
}, SkipToken: true,
}, {
Point: &common.DataPoint{
UniqId: doorDev.UniqId, Attr: "open",
ValOneof: &common.DataPoint_BoolVal{BoolVal: false},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
},
Expand All @@ -94,34 +94,34 @@ func TestDecodeMessages(t *testing.T) {
&message.DecoderIn{
UniqId: co2Dev.UniqId, Data: []byte{
0x01, 0x09, 0x61, 0x13, 0x95, 0x02, 0x92,
}, Ts: now, TraceId: traceID,
}, Ts: now, TraceId: traceID[:],
}, []*message.ValidatorIn{
{
Point: &common.DataPoint{
UniqId: co2Dev.UniqId, Attr: "temp_c",
ValOneof: &common.DataPoint_Fl64Val{Fl64Val: 24},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
{
Point: &common.DataPoint{
UniqId: co2Dev.UniqId, Attr: "temp_f",
ValOneof: &common.DataPoint_Fl64Val{Fl64Val: 75.2},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
{
Point: &common.DataPoint{
UniqId: co2Dev.UniqId, Attr: "humidity_pct",
ValOneof: &common.DataPoint_Fl64Val{Fl64Val: 50.13},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
{
Point: &common.DataPoint{
UniqId: co2Dev.UniqId, Attr: "co2_ppm",
ValOneof: &common.DataPoint_IntVal{IntVal: 658},
Ts: now, TraceId: traceID,
Ts: now, TraceId: traceID.String(),
}, SkipToken: true,
},
},
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestDecodeMessages(t *testing.T) {

func TestDecodeMessagesError(t *testing.T) {
now := timestamppb.New(time.Now().Add(-15 * time.Minute))
traceID := uuid.NewString()
traceID := uuid.New()

ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()
Expand Down Expand Up @@ -196,21 +196,21 @@ func TestDecodeMessagesError(t *testing.T) {
// Empty data.
{
&message.DecoderIn{
UniqId: createDev.UniqId, Ts: now, TraceId: traceID,
UniqId: createDev.UniqId, Ts: now, TraceId: traceID[:],
},
},
// Bad payload.
{nil},
// Device not found.
{
&message.DecoderIn{
UniqId: random.String(16), Ts: now, TraceId: traceID,
UniqId: random.String(16), Ts: now, TraceId: traceID[:],
},
},
// Decode error, defaults to Decoder zero value when not in registry.
{
&message.DecoderIn{
UniqId: createInvDev.UniqId, Ts: now, TraceId: traceID,
UniqId: createInvDev.UniqId, Ts: now, TraceId: traceID[:],
},
},
}
Expand Down
9 changes: 5 additions & 4 deletions internal/atlas-lora-ingestor/ingestor/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ func (ing *Ingestor) decodeDevices() {
metric.Incr("received", map[string]string{"type": "device"})

// Set up logging fields.
traceID := uuid.NewString()
traceID := uuid.New()
logger := alog.
WithField("type", "device").
WithField("traceID", traceID)
WithField("traceID", traceID.String())

// Parse and validate topic in format:
// 'lora/application/+/device/+/event/+'.
Expand Down Expand Up @@ -136,7 +136,8 @@ func (ing *Ingestor) decodeDevices() {
var successCount int

for _, point := range points {
vIn := registry.PointToVIn(traceID, topicParts[4], point, ts)
vIn := registry.PointToVIn(traceID.String(), topicParts[4], point,
ts)

bVIn, err := proto.Marshal(vIn)
if err != nil {
Expand Down Expand Up @@ -167,7 +168,7 @@ func (ing *Ingestor) decodeDevices() {
UniqId: topicParts[4],
Data: data,
Ts: ts,
TraceId: traceID,
TraceId: traceID[:],
}

bPIn, err := proto.Marshal(pIn)
Expand Down