Skip to content

Commit

Permalink
Merge pull request #354 from hramazani/v0.27.0
Browse files Browse the repository at this point in the history
Add further attributes to span in tracing package
  • Loading branch information
hramazani committed Dec 8, 2022
2 parents d3e5cab + 451e5e6 commit a9c055e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tracing/cogs/cogs.go
Original file line number Diff line number Diff line change
@@ -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"
)
5 changes: 5 additions & 0 deletions tracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion tracing/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a9c055e

Please sign in to comment.