From 089701ebb672e126f4261ae9886655e5fd612482 Mon Sep 17 00:00:00 2001 From: Amy Tobey Date: Tue, 30 Nov 2021 12:17:59 -0800 Subject: [PATCH 1/2] SRE-115 add inband otel traceparent propagation Adds "_traceparent" to Cacher's hardware data structure so we can do in-band propagation of traceparent between API and boots. Signed-off-by: Amy Tobey --- hardware/hw.go | 3 ++- hardware/hw_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/hardware/hw.go b/hardware/hw.go index 20a6986..2e7c1b3 100644 --- a/hardware/hw.go +++ b/hardware/hw.go @@ -24,7 +24,7 @@ type Hardware struct { logger *log.Logger mu sync.RWMutex hw map[id]struct { - j string + j string // raw json record ips map[netaddr.IP]bool macs map[mac]bool } @@ -48,6 +48,7 @@ type hardware struct { MAC string } } `json:"network_ports"` + Traceparent string `json:"_traceparent"` // for otel trace propagation } // The Option type describes functions that operate on Hardeare during New. diff --git a/hardware/hw_test.go b/hardware/hw_test.go index 4e66f95..b7b4595 100644 --- a/hardware/hw_test.go +++ b/hardware/hw_test.go @@ -1,6 +1,7 @@ package hardware import ( + "encoding/json" "fmt" "testing" @@ -403,3 +404,33 @@ func TestByMAC(t *testing.T) { assert.Equal(j2, j) } } + +func TestTraceparent(t *testing.T) { + assert := require.New(t) + + tps := []string{ + "", + "00-00000000000000000000000000000000-0000000000000000-00", + "00-6d2cd79aa1c04f5a6f579cdb932e07b3-d376ef33e09b1e05-01", + "00-d538560f791eb96185f34aa2bf6082a9-b98f3fb3157fae01-01", + } + + jsons := []string{ + `{"id": "` + uuid.New().String() + `", "_traceparent": "` + tps[0] + `"}`, + `{"id": "` + uuid.New().String() + `", "_traceparent": "` + tps[1] + `"}`, + `{"id": "` + uuid.New().String() + `", "_traceparent": "` + tps[2] + `"}`, + `{"id": "` + uuid.New().String() + `", "_traceparent": "` + tps[3] + `"}`, + } + + hw := New() + for i, j := range jsons { + id, err := hw.Add(j) + assert.Nil(err) + js, err := hw.ByID(id) // retrieve the hw and make sure the tp survives + assert.Nil(err) + inst := hardware{} + err = json.Unmarshal([]byte(js), &inst) + assert.Nil(err) + assert.Equal(tps[i], inst.Traceparent) + } +} From f8060e5b0c2d0058ca31892b5e8609c065685e1f Mon Sep 17 00:00:00 2001 From: Amy Tobey Date: Tue, 30 Nov 2021 14:22:18 -0800 Subject: [PATCH 2/2] remove leading underscore from traceparent When signaling in-band my preference is to lead with an underscore so it's fairly clear it's not app data, but since the Otel injectors insist on setting their own 'traceparent' key we'll just use that. Signed-off-by: Amy Tobey --- hardware/hw.go | 3 ++- hardware/hw_test.go | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/hardware/hw.go b/hardware/hw.go index 2e7c1b3..9691e60 100644 --- a/hardware/hw.go +++ b/hardware/hw.go @@ -48,7 +48,8 @@ type hardware struct { MAC string } } `json:"network_ports"` - Traceparent string `json:"_traceparent"` // for otel trace propagation + // in-band propagation of w3c traceparent so it can make it to boots + Traceparent string `json:"traceparent"` } // The Option type describes functions that operate on Hardeare during New. diff --git a/hardware/hw_test.go b/hardware/hw_test.go index b7b4595..4fea971 100644 --- a/hardware/hw_test.go +++ b/hardware/hw_test.go @@ -416,10 +416,10 @@ func TestTraceparent(t *testing.T) { } jsons := []string{ - `{"id": "` + uuid.New().String() + `", "_traceparent": "` + tps[0] + `"}`, - `{"id": "` + uuid.New().String() + `", "_traceparent": "` + tps[1] + `"}`, - `{"id": "` + uuid.New().String() + `", "_traceparent": "` + tps[2] + `"}`, - `{"id": "` + uuid.New().String() + `", "_traceparent": "` + tps[3] + `"}`, + `{"id": "` + uuid.New().String() + `", "traceparent": "` + tps[0] + `"}`, + `{"id": "` + uuid.New().String() + `", "traceparent": "` + tps[1] + `"}`, + `{"id": "` + uuid.New().String() + `", "traceparent": "` + tps[2] + `"}`, + `{"id": "` + uuid.New().String() + `", "traceparent": "` + tps[3] + `"}`, } hw := New()