Skip to content

Commit

Permalink
Add otel type (#1210)
Browse files Browse the repository at this point in the history
* add otel + marshal/unmarshal

otel type will be populated with data when using
`apmotel` module

cf. #1203
  • Loading branch information
stuartnelson3 authored Feb 9, 2022
1 parent d9386ac commit 640a916
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
17 changes: 17 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Context struct {
user model.User
service model.Service
serviceFramework model.Framework
otel *model.OTel
captureHeaders bool
captureBodyMask CaptureBodyMode
sanitizedFieldNames wildcard.Matchers
Expand Down Expand Up @@ -83,6 +84,22 @@ func (c *Context) reset() {
}
}

// SetOTelAttributes sets the provided OpenTelemetry attributes.
func (c *Context) SetOTelAttributes(m map[string]interface{}) {
if c.otel == nil {
c.otel = &model.OTel{}
}
c.otel.Attributes = m
}

// SetOTelSpanKind sets the provided SpanKind.
func (c *Context) SetOTelSpanKind(spanKind string) {
if c.otel == nil {
c.otel = &model.OTel{}
}
c.otel.SpanKind = spanKind
}

// SetTag calls SetLabel(key, value).
//
// SetTag is deprecated, and will be removed in a future major version.
Expand Down
41 changes: 41 additions & 0 deletions model/marshal_fastjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions model/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ func TestMarshalTransaction(t *testing.T) {
"outcome": "success",
},
},
"otel": map[string]interface{}{
"span_kind": "MESSAGING",
"attributes": map[string]interface{}{
"messaging.system": "messaging",
},
},
}
assert.Equal(t, expect, decoded)
}
Expand Down Expand Up @@ -143,6 +149,12 @@ func TestMarshalSpan(t *testing.T) {
"user": "barb",
},
},
"otel": map[string]interface{}{
"span_kind": "MESSAGING",
"attributes": map[string]interface{}{
"messaging.system": "messaging",
},
},
}, decoded)

w.Reset()
Expand All @@ -152,6 +164,14 @@ func TestMarshalSpan(t *testing.T) {
span.ParentID = model.SpanID{} // parent_id is optional
span.TransactionID = model.SpanID{} // transaction_id is optional
span.Context = fakeHTTPSpanContext()
span.OTel = &model.OTel{
SpanKind: "SERVER",
Attributes: map[string]interface{}{
"numeric.data": 123.456,
"boolean.data": true,
"slice.data": []string{"one", "two"},
},
}
span.MarshalFastJSON(&w)

decoded = mustUnmarshalJSON(w)
Expand All @@ -167,6 +187,14 @@ func TestMarshalSpan(t *testing.T) {
"url": "http://testing.invalid:8000/path?query#fragment",
},
},
"otel": map[string]interface{}{
"span_kind": "SERVER",
"attributes": map[string]interface{}{
"numeric.data": 123.456,
"boolean.data": true,
"slice.data": []interface{}{"one", "two"},
},
},
}, decoded)
}

Expand All @@ -193,6 +221,12 @@ func TestMarshalSpanHTTPStatusCode(t *testing.T) {
"status_code": 200.0,
},
},
"otel": map[string]interface{}{
"span_kind": "MESSAGING",
"attributes": map[string]interface{}{
"messaging.system": "messaging",
},
},
}, decoded)
}

Expand Down Expand Up @@ -669,6 +703,12 @@ func fakeTransaction() model.Transaction {
},
},
},
OTel: &model.OTel{
SpanKind: "MESSAGING",
Attributes: map[string]interface{}{
"messaging.system": "messaging",
},
},
}
}

Expand All @@ -683,6 +723,12 @@ func fakeSpan() model.Span {
Duration: 3,
Type: "db.postgresql.query",
Context: fakeDatabaseSpanContext(),
OTel: &model.OTel{
SpanKind: "MESSAGING",
Attributes: map[string]interface{}{
"messaging.system": "messaging",
},
},
}
}

Expand Down
12 changes: 12 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ type Transaction struct {

// Outcome holds the transaction outcome: success, failure, or unknown.
Outcome string `json:"outcome,omitempty"`

// OTel holds information bridged from OpenTelemetry.
OTel *OTel `json:"otel,omitempty"`
}

// OTel holds bridged OpenTelemetry information.
type OTel struct {
SpanKind string `json:"span_kind"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
}

// SpanCount holds statistics on spans within a transaction.
Expand Down Expand Up @@ -366,6 +375,9 @@ type Span struct {
// Composite is set when the span is a composite span and represents an
// aggregated set of spans as defined by `composite.compression_strategy`.
Composite *CompositeSpan `json:"composite,omitempty"`

// OTel holds information bridged from OpenTelemetry.
OTel *OTel `json:"otel,omitempty"`
}

// SpanContext holds contextual information relating to the span.
Expand Down
2 changes: 2 additions & 0 deletions modelwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (w *modelWriter) buildModelTransaction(out *model.Transaction, tx *Transact
out.Duration = td.Duration.Seconds() * 1000
out.SpanCount.Started = td.spansCreated
out.SpanCount.Dropped = td.spansDropped
out.OTel = td.Context.otel
if dss := buildDroppedSpansStats(td.droppedSpansStats); len(dss) > 0 {
out.DroppedSpansStats = dss
}
Expand Down Expand Up @@ -150,6 +151,7 @@ func (w *modelWriter) buildModelSpan(out *model.Span, span *Span, sd *SpanData)
out.Duration = sd.Duration.Seconds() * 1000
out.Outcome = normalizeOutcome(sd.Outcome)
out.Context = sd.Context.build()
out.OTel = sd.Context.otel
if sd.composite.count > 1 {
out.Composite = sd.composite.build()
}
Expand Down
17 changes: 17 additions & 0 deletions spancontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SpanContext struct {
databaseRowsAffected int64
database model.DatabaseSpanContext
http model.HTTPSpanContext
otel *model.OTel

// If SetDestinationService has been called, we do not auto-set its
// resource value on span end.
Expand Down Expand Up @@ -104,6 +105,22 @@ func (c *SpanContext) reset() {
}
}

// SetOTelAttributes sets the provided OpenTelemetry attributes.
func (c *SpanContext) SetOTelAttributes(m map[string]interface{}) {
if c.otel == nil {
c.otel = &model.OTel{}
}
c.otel.Attributes = m
}

// SetOTelSpanKind sets the provided SpanKind.
func (c *SpanContext) SetOTelSpanKind(spanKind string) {
if c.otel == nil {
c.otel = &model.OTel{}
}
c.otel.SpanKind = spanKind
}

// SetTag calls SetLabel(key, value).
//
// SetTag is deprecated, and will be removed in a future major version.
Expand Down

0 comments on commit 640a916

Please sign in to comment.