From b8bbe44ff264304fd5708f1d9594b40480c99c07 Mon Sep 17 00:00:00 2001 From: Hamid Ramazani Date: Thu, 8 Dec 2022 11:46:17 -0800 Subject: [PATCH 1/2] Add further attributes to span in tracing package --- tracing/cogs/cogs.go | 8 ++++++++ tracing/http.go | 5 +++++ 2 files changed, 13 insertions(+) create mode 100644 tracing/cogs/cogs.go diff --git a/tracing/cogs/cogs.go b/tracing/cogs/cogs.go new file mode 100644 index 00000000..21a66ced --- /dev/null +++ b/tracing/cogs/cogs.go @@ -0,0 +1,8 @@ +package cogs + +// const values used in setting span attributes for COGS: Cost Of Goods Sold. +const ( + CostOfGoodsKey = "cogs" + CostOfGoodsMethodKey = "cogs.method" + CostOfGoodsAccountID = "cogs.accountID" +) diff --git a/tracing/http.go b/tracing/http.go index 0a9db2ce..9bf6e97c 100644 --- a/tracing/http.go +++ b/tracing/http.go @@ -11,6 +11,7 @@ import ( "go.opencensus.io/trace" "github.com/infobloxopen/atlas-app-toolkit/auth" + "github.com/infobloxopen/atlas-app-toolkit/tracing/cogs" ) const ( @@ -137,6 +138,10 @@ type Handler struct { func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { span := trace.FromContext(r.Context()) + // Add accountID to span attributes + accountID, _ := auth.GetAccountID(r.Context(), nil) + span.AddAttributes(trace.StringAttribute(cogs.CostOfGoodsAccountID, accountID)) + withHeaders := h.options.spanWithHeaders != nil && h.options.spanWithHeaders(r) withPayload := h.options.spanWithPayload != nil && h.options.spanWithPayload(r) From d48618df379b0d24d005b333e5900c554f585649 Mon Sep 17 00:00:00 2001 From: Hamid Ramazani Date: Thu, 8 Dec 2022 11:49:46 -0800 Subject: [PATCH 2/2] update test-case --- tracing/http_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracing/http_test.go b/tracing/http_test.go index 8313f2fe..a99f8b09 100644 --- a/tracing/http_test.go +++ b/tracing/http_test.go @@ -177,7 +177,7 @@ func TestHandler_ServeHTTP(t *testing.T) { // Test that Span attributes were populated with Headers reflectAttrMap := reflect.ValueOf(trace.FromContext(result.request.Context())).Elem().Field(3).Elem().Field(0) reflectKeys := reflectAttrMap.MapKeys() - assert.Len(t, reflectKeys, 8) + assert.Len(t, reflectKeys, 9) resultHeadersMap := make(map[string]string) for _, k := range reflectKeys {