Skip to content

Commit

Permalink
Addres gitbot concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoffl01 committed Jul 10, 2024
1 parent b55d560 commit 74f7cb6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
31 changes: 13 additions & 18 deletions ddtrace/opentelemetry/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type span struct {
finishOpts []tracer.FinishOption
statusInfo
*oteltracer
events []spanEvent
events []SpanEvent
}

func (s *span) TracerProvider() oteltrace.TracerProvider { return s.oteltracer.provider }
Expand All @@ -48,16 +48,16 @@ func (s *span) SetName(name string) {
s.attributes[ext.SpanName] = strings.ToLower(name)
}

// spanEvent holds information about otelsdk.Event types, with some fields altered and renamed to fit Datadog needs
// SpanEvent holds information about otelsdk.Event types, with some fields altered and renamed to fit Datadog needs
// along with json tags for easy marshaling
type spanEvent struct {
Name string `json:"name"`
Time_unix_nano int64 `json:"time_unix_nano"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
type SpanEvent struct {
Name string `json:"name"`
TimeUnixNano int64 `json:"time_unix_nano"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
}

// stringifySpanEvents transforms a slice of otelsdk.Events into a comma separated string
func stringifySpanEvents(evts []spanEvent) (s string) {
func stringifySpanEvents(evts []SpanEvent) (s string) {
for i, e := range evts {
if i == 0 {
s += marshalSpanEvent(e)
Expand All @@ -69,7 +69,7 @@ func stringifySpanEvents(evts []spanEvent) (s string) {
}

// marshalSpanEvent transforms an otelsdk.Event into a JSON-encoded object with "name" and "time_unix_nano" fields, and an optional "attributes" field
func marshalSpanEvent(evt spanEvent) string {
func marshalSpanEvent(evt SpanEvent) string {
s, err := json.Marshal(evt)
if err != nil {
log.Debug(fmt.Sprintf("Issue marshaling span event %v:%v", evt, err))
Expand Down Expand Up @@ -216,16 +216,11 @@ func (s *span) AddEvent(name string, opts ...oteltrace.EventOption) {
for _, a := range c.Attributes() {
attrs[string(a.Key)] = a.Value.AsInterface()
}
s.events = append(s.events, NewSpanEvent(name, c.Timestamp().Unix(), attrs))
}

// NewSpanEvent returns a spanEvent with the provided name, time_unix_nano and attributes
func NewSpanEvent(name string, time_unix_nano int64, attrs map[string]interface{}) spanEvent {
return spanEvent{
Name: name,
Time_unix_nano: time_unix_nano,
Attributes: attrs,
}
s.events = append(s.events, SpanEvent{
Name: name,
TimeUnixNano: c.Timestamp().Unix(),
Attributes: attrs,
})
}

// SetAttributes sets the key-value pairs as tags on the span.
Expand Down
18 changes: 9 additions & 9 deletions ddtrace/opentelemetry/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ func TestMarshalSpanEvent(t *testing.T) {
// now := time.Now()
nowUnix := time.Now().Unix()
want := fmt.Sprintf("{\"name\":\"evt\",\"time_unix_nano\":%v,\"attributes\":{\"attribute1\":\"value1\",\"attribute2\":123,\"attribute3\":[1,2,3],\"attribute4\":true}}", nowUnix)
s := marshalSpanEvent(spanEvent{
Name: "evt",
Time_unix_nano: nowUnix,
s := marshalSpanEvent(SpanEvent{
Name: "evt",
TimeUnixNano: nowUnix,
Attributes: map[string]interface{}{
"attribute1": "value1",
"attribute2": 123,
Expand All @@ -206,13 +206,13 @@ func TestMarshalSpanEvent(t *testing.T) {
func TestStringifySpanEvent(t *testing.T) {
assert := assert.New(t)
t.Run("multiple events", func(t *testing.T) {
evt1 := spanEvent{
evt1 := SpanEvent{
Name: "abc",
}
evt2 := spanEvent{
evt2 := SpanEvent{
Name: "def",
}
evts := []spanEvent{evt1, evt2}
evts := []SpanEvent{evt1, evt2}
want := marshalSpanEvent(evt1) + "," + marshalSpanEvent(evt2)

s := stringifySpanEvents(evts)
Expand Down Expand Up @@ -374,8 +374,8 @@ func TestSpanAddEvent(t *testing.T) {
e := dd.events[0]
assert.Equal(e.Name, "My event!")
// assert event timestamp is [around] the expected time
fmt.Printf("event time: %v, should be bound btwn start: %v and end: %v\n", e.Time_unix_nano, timeStartBound, timeEndBound)
assert.True((e.Time_unix_nano) >= timeStartBound && e.Time_unix_nano <= timeEndBound)
fmt.Printf("event time: %v, should be bound btwn start: %v and end: %v\n", e.TimeUnixNano, timeStartBound, timeEndBound)
assert.True((e.TimeUnixNano) >= timeStartBound && e.TimeUnixNano <= timeEndBound)
// Assert both attributes exist on the event
assert.Len(e.Attributes, 3)
// Assert attribute key-value fields
Expand All @@ -398,7 +398,7 @@ func TestSpanAddEvent(t *testing.T) {
dd := sp.(*span)
assert.Len(dd.events, 1)
e := dd.events[0]
assert.Equal(e.Time_unix_nano, now.Unix())
assert.Equal(e.TimeUnixNano, now.Unix())
})
}

Expand Down

0 comments on commit 74f7cb6

Please sign in to comment.