Skip to content

Commit

Permalink
model: make ID fields non-pointer string types (#3853) (#3856)
Browse files Browse the repository at this point in the history
* model: make IDs non-pointer fields
* model: stop recording ParentIdx in model types
  • Loading branch information
axw authored Jun 8, 2020
1 parent 410447f commit 252e975
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 83 deletions.
16 changes: 9 additions & 7 deletions model/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ const (

type Error struct {
ID *string
TransactionID *string
TraceID *string
ParentID *string
TransactionID string
TraceID string
ParentID string

Timestamp time.Time
Metadata Metadata
Expand Down Expand Up @@ -122,16 +122,18 @@ func (e *Error) Transform(ctx context.Context, tctx *transform.Context) []beat.E

// sampled and type is nil if an error happens outside a transaction or an (old) agent is not sending sampled info
// agents must send semantically correct data
if e.TransactionSampled != nil || e.TransactionType != nil || (e.TransactionID != nil && *e.TransactionID != "") {
if e.TransactionSampled != nil || e.TransactionType != nil || e.TransactionID != "" {
transaction := common.MapStr{}
utility.Set(transaction, "id", e.TransactionID)
if e.TransactionID != "" {
transaction["id"] = e.TransactionID
}
utility.Set(transaction, "type", e.TransactionType)
utility.Set(transaction, "sampled", e.TransactionSampled)
utility.Set(fields, "transaction", transaction)
}

utility.AddId(fields, "parent", e.ParentID)
utility.AddId(fields, "trace", e.TraceID)
utility.AddID(fields, "parent", e.ParentID)
utility.AddID(fields, "trace", e.TraceID)
utility.Set(fields, "timestamp", utility.TimeAsMicros(e.Timestamp))

return []beat.Event{
Expand Down
4 changes: 2 additions & 2 deletions model/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestEventFields(t *testing.T) {
Culprit: &culprit,
Exception: &exception,
Log: &log,
TransactionID: &trID,
TransactionID: trID,

// Service name and version are required for sourcemapping.
Metadata: Metadata{
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestEvents(t *testing.T) {
Message: &exMsg,
Stacktrace: Stacktrace{&StacktraceFrame{Filename: tests.StringPtr("myFile")}},
},
TransactionID: &trID,
TransactionID: trID,
TransactionSampled: &sampledTrue,
Labels: &labels,
Page: &Page{Url: &url, Referer: &referer},
Expand Down
6 changes: 3 additions & 3 deletions model/modeldecoder/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ func decodeError(input Input, schema *jsonschema.Schema) (*m.Error, error) {
Custom: ctx.Custom,
Experimental: ctx.Experimental,
Timestamp: decoder.TimeEpochMicro(raw, "timestamp"),
TransactionID: decoder.StringPtr(raw, fieldName("transaction_id")),
ParentID: decoder.StringPtr(raw, fieldName("parent_id")),
TraceID: decoder.StringPtr(raw, fieldName("trace_id")),
TransactionSampled: decoder.BoolPtr(raw, fieldName("sampled"), fieldName("transaction")),
TransactionType: decoder.StringPtr(raw, fieldName("type"), fieldName("transaction")),
}
decodeString(raw, fieldName("parent_id"), &e.ParentID)
decodeString(raw, fieldName("trace_id"), &e.TraceID)
decodeString(raw, fieldName("transaction_id"), &e.TransactionID)

ex := decoder.MapStr(raw, fieldName("exception"))
e.Exception = decodeException(&decoder, input.Config.HasShortFieldNames)(ex)
Expand Down
6 changes: 3 additions & 3 deletions model/modeldecoder/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ func TestErrorEventDecode(t *testing.T) {
},
},
ID: &id,
TransactionID: &transactionID,
TransactionID: transactionID,
TransactionSampled: &transactionSampled,
TransactionType: &transactionType,
ParentID: &parentID,
TraceID: &traceID,
ParentID: parentID,
TraceID: traceID,
Culprit: &culprit,
},
},
Expand Down
43 changes: 24 additions & 19 deletions model/modeldecoder/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,26 @@ var (
rumV3SpanSchema = validation.CreateSchema(schema.RUMV3Schema, "span")
)

// decodeRUMV3Span decodes a v3 RUM span.
func decodeRUMV3Span(input Input) (*model.Span, error) {
// decodeRUMV3Span decodes a v3 RUM span, and optional parent index.
// If parent index wasn't specified, then the value will be negative.
func decodeRUMV3Span(input Input) (_ *model.Span, parentIndex int, _ error) {
return decodeSpan(input, rumV3SpanSchema)
}

// DecodeSpan decodes a span.
func DecodeSpan(input Input, batch *model.Batch) error {
span, err := decodeSpan(input, spanSchema)
span, _, err := decodeSpan(input, spanSchema)
if err != nil {
return err
}
batch.Spans = append(batch.Spans, span)
return nil
}

func decodeSpan(input Input, schema *jsonschema.Schema) (*model.Span, error) {
func decodeSpan(input Input, schema *jsonschema.Schema) (_ *model.Span, parentIndex int, _ error) {
raw, err := validation.ValidateObject(input.Raw, schema)
if err != nil {
return nil, errors.Wrap(err, "failed to validate span")
return nil, -1, errors.Wrap(err, "failed to validate span")
}

fieldName := field.Mapper(input.Config.HasShortFieldNames)
Expand All @@ -68,16 +69,14 @@ func decodeSpan(input Input, schema *jsonschema.Schema) (*model.Span, error) {
Sync: decoder.BoolPtr(raw, fieldName("sync")),
Timestamp: decoder.TimeEpochMicro(raw, fieldName("timestamp")),
ID: decoder.String(raw, fieldName("id")),
ParentID: decoder.StringPtr(raw, "parent_id"),
ChildIDs: decoder.StringArr(raw, "child_ids"),
// ParentIdx comes from RUM V3 payloads only, and used to populate ParentID
ParentIdx: decoder.IntPtr(raw, fieldName("parent_idx")),
TraceID: decoder.StringPtr(raw, "trace_id"),
TransactionID: decoder.StringPtr(raw, "transaction_id"),
Type: decoder.String(raw, fieldName("type")),
Subtype: decoder.StringPtr(raw, fieldName("subtype")),
Action: decoder.StringPtr(raw, fieldName("action")),
Type: decoder.String(raw, fieldName("type")),
Subtype: decoder.StringPtr(raw, fieldName("subtype")),
Action: decoder.StringPtr(raw, fieldName("action")),
}
decodeString(raw, fieldName("parent_id"), &event.ParentID)
decodeString(raw, fieldName("trace_id"), &event.TraceID)
decodeString(raw, fieldName("transaction_id"), &event.TransactionID)

ctx := decoder.MapStr(raw, fieldName("context"))
if ctx != nil {
Expand All @@ -87,19 +86,19 @@ func decodeSpan(input Input, schema *jsonschema.Schema) (*model.Span, error) {

db, err := decodeDB(ctx, decoder.Err)
if err != nil {
return nil, err
return nil, -1, err
}
event.DB = db

http, err := decodeSpanHTTP(ctx, input.Config.HasShortFieldNames, decoder.Err)
if err != nil {
return nil, err
return nil, -1, err
}
event.HTTP = http

dest, destService, err := decodeDestination(ctx, input.Config.HasShortFieldNames, decoder.Err)
if err != nil {
return nil, err
return nil, -1, err
}
event.Destination = dest
event.DestinationService = destService
Expand All @@ -111,7 +110,7 @@ func decodeSpan(input Input, schema *jsonschema.Schema) (*model.Span, error) {
}

if event.Message, err = decodeMessage(ctx, decoder.Err); err != nil {
return nil, err
return nil, -1, err
}

if input.Config.Experimental {
Expand All @@ -124,7 +123,7 @@ func decodeSpan(input Input, schema *jsonschema.Schema) (*model.Span, error) {
var stacktr *m.Stacktrace
stacktr, decoder.Err = decodeStacktrace(raw[fieldName("stacktrace")], input.Config.HasShortFieldNames, decoder.Err)
if decoder.Err != nil {
return nil, decoder.Err
return nil, -1, decoder.Err
}
if stacktr != nil {
event.Stacktrace = *stacktr
Expand Down Expand Up @@ -152,7 +151,13 @@ func decodeSpan(input Input, schema *jsonschema.Schema) (*model.Span, error) {
event.Timestamp = timestamp
}

return &event, nil
// parent_idx comes from RUM V3 payloads only. It is used only during
// decoding to populate ParentID. We initialise to -1 to indicate lack
// of parent index.
parentIndex = -1
decodeInt(raw, fieldName("parent_idx"), &parentIndex)

return &event, parentIndex, nil
}

func decodeDB(input interface{}, err error) (*model.DB, error) {
Expand Down
32 changes: 16 additions & 16 deletions model/modeldecoder/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func TestDecodeSpan(t *testing.T) {
Action: &action2,
Duration: duration,
Timestamp: spanTime,
ParentID: &parentID,
ParentID: parentID,
ID: id,
TraceID: &traceID,
TraceID: traceID,
},
},
"no timestamp specified, request time + start used": {
Expand All @@ -113,9 +113,9 @@ func TestDecodeSpan(t *testing.T) {
Name: name,
Type: "db",
Duration: duration,
ParentID: &parentID,
ParentID: parentID,
ID: id,
TraceID: &traceID,
TraceID: traceID,
Start: &start,
Timestamp: requestTime.Add(time.Duration(start * float64(time.Millisecond))),
},
Expand All @@ -135,10 +135,10 @@ func TestDecodeSpan(t *testing.T) {
Start: &start,
Duration: duration,
Timestamp: spanTime,
ParentID: &parentID,
ParentID: parentID,
ID: id,
TraceID: &traceID,
TransactionID: &transactionID,
TraceID: traceID,
TransactionID: transactionID,
},
},
"event experimental=true, no experimental payload": {
Expand All @@ -156,10 +156,10 @@ func TestDecodeSpan(t *testing.T) {
Start: &start,
Duration: duration,
Timestamp: spanTime,
ParentID: &parentID,
ParentID: parentID,
ID: id,
TraceID: &traceID,
TransactionID: &transactionID,
TraceID: traceID,
TransactionID: transactionID,
},
cfg: Config{Experimental: true},
},
Expand All @@ -178,10 +178,10 @@ func TestDecodeSpan(t *testing.T) {
Start: &start,
Duration: duration,
Timestamp: spanTime,
ParentID: &parentID,
ParentID: parentID,
ID: id,
TraceID: &traceID,
TransactionID: &transactionID,
TraceID: traceID,
TransactionID: transactionID,
Experimental: 123,
},
cfg: Config{Experimental: true},
Expand All @@ -206,9 +206,9 @@ func TestDecodeSpan(t *testing.T) {
},
Labels: common.MapStr{"a": "tag", "tag_key": 17},
ID: id,
TraceID: &traceID,
ParentID: &parentID,
TransactionID: &transactionID,
TraceID: traceID,
ParentID: parentID,
TransactionID: transactionID,
HTTP: &m.HTTP{Method: &method, StatusCode: &statusCode, URL: &url},
DB: &m.DB{
Instance: &instance,
Expand Down
18 changes: 9 additions & 9 deletions model/modeldecoder/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func decodeRUMV3Spans(raw map[string]interface{}, input Input, tr *model.Transac
rawSpans := decoder.InterfaceArr(raw, fieldName("span"))
var spans = make([]*model.Span, len(rawSpans))
for idx, rawSpan := range rawSpans {
span, err := decodeRUMV3Span(Input{
span, parentIndex, err := decodeRUMV3Span(Input{
Raw: rawSpan,
RequestTime: input.RequestTime,
Metadata: input.Metadata,
Expand All @@ -104,12 +104,12 @@ func decodeRUMV3Spans(raw map[string]interface{}, input Input, tr *model.Transac
if err != nil {
return spans, err
}
span.TransactionID = &tr.ID
span.TraceID = &tr.TraceID
if span.ParentIdx == nil {
span.ParentID = &tr.ID
} else if *span.ParentIdx < idx {
span.ParentID = &spans[*span.ParentIdx].ID
span.TransactionID = tr.ID
span.TraceID = tr.TraceID
if parentIndex >= 0 && parentIndex < idx {
span.ParentID = spans[parentIndex].ID
} else {
span.ParentID = tr.ID
}
spans[idx] = span
}
Expand Down Expand Up @@ -158,12 +158,12 @@ func decodeTransaction(input Input, schema *jsonschema.Schema) (*model.Transacti
SpanCount: model.SpanCount{
Dropped: decoder.IntPtr(raw, fieldName("dropped"), fieldName("span_count")),
Started: decoder.IntPtr(raw, fieldName("started"), fieldName("span_count"))},
ParentID: decoder.StringPtr(raw, fieldName("parent_id")),
TraceID: decoder.String(raw, fieldName("trace_id")),
TraceID: decoder.String(raw, fieldName("trace_id")),
}
if decoder.Err != nil {
return nil, decoder.Err
}
decodeString(raw, fieldName("parent_id"), &e.ParentID)
if e.Timestamp.IsZero() {
e.Timestamp = input.RequestTime
}
Expand Down
2 changes: 1 addition & 1 deletion model/modeldecoder/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func TestTransactionEventDecode(t *testing.T) {
Type: trType,
Name: &name,
Result: &result,
ParentID: &parentID,
ParentID: parentID,
TraceID: traceID,
Duration: duration,
Timestamp: timestampParsed,
Expand Down
13 changes: 6 additions & 7 deletions model/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ var (
type Span struct {
Metadata Metadata
ID string
TransactionID *string
ParentID *string
TransactionID string
ParentID string
ChildIDs []string
ParentIdx *int
TraceID *string
TraceID string

Timestamp time.Time

Expand Down Expand Up @@ -191,12 +190,12 @@ func (e *Span) Transform(ctx context.Context, tctx *transform.Context) []beat.Ev
utility.DeepUpdate(fields, "agent", e.Service.AgentFields())
// merges with metadata labels, overrides conflicting keys
utility.DeepUpdate(fields, "labels", e.Labels)
utility.AddId(fields, "parent", e.ParentID)
utility.AddID(fields, "parent", e.ParentID)
if e.ChildIDs != nil {
utility.Set(fields, "child", common.MapStr{"id": e.ChildIDs})
}
utility.AddId(fields, "trace", e.TraceID)
utility.AddId(fields, "transaction", e.TransactionID)
utility.AddID(fields, "trace", e.TraceID)
utility.AddID(fields, "transaction", e.TransactionID)
utility.Set(fields, "experimental", e.Experimental)
utility.Set(fields, "destination", e.Destination.fields())
utility.Set(fields, "timestamp", utility.TimeAsMicros(e.Timestamp))
Expand Down
4 changes: 2 additions & 2 deletions model/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func TestSpanTransform(t *testing.T) {
Span: Span{
Metadata: metadata,
ID: hexID,
TraceID: &traceID,
ParentID: &parentID,
TraceID: traceID,
ParentID: parentID,
Name: "myspan",
Type: "myspantype",
Subtype: &subtype,
Expand Down
6 changes: 3 additions & 3 deletions model/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Transaction struct {
Metadata Metadata

ID string
ParentID *string
ParentID string
TraceID string

Timestamp time.Time
Expand Down Expand Up @@ -120,8 +120,8 @@ func (e *Transaction) Transform(ctx context.Context, tctx *transform.Context) []
utility.Set(fields, "source", fields["client"])

// then merge event specific information
utility.AddId(fields, "parent", e.ParentID)
utility.AddId(fields, "trace", &e.TraceID)
utility.AddID(fields, "parent", e.ParentID)
utility.AddID(fields, "trace", e.TraceID)
utility.Set(fields, "timestamp", utility.TimeAsMicros(e.Timestamp))
// merges with metadata labels, overrides conflicting keys
utility.DeepUpdate(fields, "labels", e.Labels.Fields())
Expand Down
Loading

0 comments on commit 252e975

Please sign in to comment.