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 aad117d9..e744aa25 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) 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 {