Skip to content

Commit

Permalink
[v2] Require and change description for error.id (elastic#1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
simitt authored and Ron cohen committed Oct 15, 2018
1 parent 1aab36e commit 308e046
Show file tree
Hide file tree
Showing 16 changed files with 477 additions and 180 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ https://github.com/elastic/apm-server/compare/6.4\...master[View commits]
- Rename `transaction.span_count.dropped.total` to `transaction.span_count.dropped` on Intake API v2 {pull}1351[1351]
- Require either `message` or `type` for `error.exception` {pull}1354[1354].
- Require `span.parent_id`, forbid `null` for `span.trace_id`, `transaction.trace_id` {pull}1391[1391]
- Require `error.id` and changed description to 128 random bits ID {pull}1384[1384]

[[release-notes-6.4]]
== APM Server version 6.4
Expand Down
7 changes: 4 additions & 3 deletions docs/spec/errors/v2_error.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
{
"properties": {
"id": {
"type": ["string", "null"],
"description": "Hex encoded 64 random bits ID of the error.",
"type": ["string"],
"description": "Hex encoded 128 random bits ID of the error.",
"maxLength": 1024
},
"trace_id": {
Expand All @@ -27,7 +27,8 @@
"type": ["string", "null"],
"maxLength": 1024
}
}
},
"required": ["id"]
}
]
}
7 changes: 4 additions & 3 deletions model/error/generated/schema/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ const ModelSchema = `{
{
"properties": {
"id": {
"type": ["string", "null"],
"description": "Hex encoded 64 random bits ID of the error.",
"type": ["string"],
"description": "Hex encoded 128 random bits ID of the error.",
"maxLength": 1024
},
"trace_id": {
Expand All @@ -442,7 +442,8 @@ const ModelSchema = `{
"type": ["string", "null"],
"maxLength": 1024
}
}
},
"required": ["id"]
}
]
}
Expand Down
80 changes: 58 additions & 22 deletions model/span/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package span

import (
"errors"
"math"
"strconv"
"time"

"github.com/santhosh-tekuri/jsonschema"
Expand Down Expand Up @@ -61,16 +63,16 @@ type Event struct {
Stacktrace m.Stacktrace

Timestamp time.Time
TransactionId *string
TransactionId string

// new in v2
HexId *string
ParentId *string
TraceId *string
HexId string
ParentId string
TraceId string

// deprecated in v2
Id *int
Parent *int
Id *int64
Parent *int64
}

func V1DecodeEvent(input interface{}, err error) (transform.Transformable, error) {
Expand All @@ -79,8 +81,11 @@ func V1DecodeEvent(input interface{}, err error) (transform.Transformable, error
return nil, err
}
decoder := utility.ManualDecoder{}
e.Id = decoder.IntPtr(raw, "id")
e.Parent = decoder.IntPtr(raw, "parent")
e.Id = decoder.Int64Ptr(raw, "id")
e.Parent = decoder.Int64Ptr(raw, "parent")
if tid := decoder.StringPtr(raw, "transaction_id"); tid != nil {
e.TransactionId = *tid
}
return e, decoder.Err
}

Expand All @@ -90,12 +95,42 @@ func V2DecodeEvent(input interface{}, err error) (transform.Transformable, error
return nil, err
}
decoder := utility.ManualDecoder{}
e.HexId = decoder.StringPtr(raw, "id")
e.ParentId = decoder.StringPtr(raw, "parent_id")
e.TraceId = decoder.StringPtr(raw, "trace_id")
e.HexId = decoder.String(raw, "id")
e.ParentId = decoder.String(raw, "parent_id")
e.TraceId = decoder.String(raw, "trace_id")
e.TransactionId = decoder.String(raw, "transaction_id")
if decoder.Err != nil {
return nil, decoder.Err
}

// HexId must be a 64 bit hex encoded string. The id is set to the integer
// converted value of the hexId
if idInt, err := hexToInt(e.HexId, 64); err == nil {
e.Id = &idInt
} else {
return nil, err
}

// set parent to parentId
if id, err := hexToInt(e.ParentId, 64); err == nil {
e.Parent = &id
} else {
return nil, err
}

return e, decoder.Err
}

var shift = uint64(math.Pow(2, 63))

func hexToInt(s string, bitSize int) (int64, error) {
us, err := strconv.ParseUint(s, 16, bitSize)
if err != nil {
return 0, err
}
return int64(us - shift), nil
}

func decodeEvent(input interface{}, err error) (*Event, map[string]interface{}, error) {
if err != nil {
return nil, nil, err
Expand All @@ -110,13 +145,12 @@ func decodeEvent(input interface{}, err error) (*Event, map[string]interface{},

decoder := utility.ManualDecoder{}
event := Event{
Name: decoder.String(raw, "name"),
Type: decoder.String(raw, "type"),
Start: decoder.Float64(raw, "start"),
Duration: decoder.Float64(raw, "duration"),
Context: decoder.MapStr(raw, "context"),
Timestamp: decoder.TimeRFC3339(raw, "timestamp"),
TransactionId: decoder.StringPtr(raw, "transaction_id"),
Name: decoder.String(raw, "name"),
Type: decoder.String(raw, "type"),
Start: decoder.Float64(raw, "start"),
Duration: decoder.Float64(raw, "duration"),
Context: decoder.MapStr(raw, "context"),
Timestamp: decoder.TimeRFC3339(raw, "timestamp"),
}
var stacktr *m.Stacktrace
stacktr, err = m.DecodeStacktrace(raw["stacktrace"], decoder.Err)
Expand All @@ -142,9 +176,9 @@ func (e *Event) Transform(tctx *transform.Context) []beat.Event {
spanDocType: e.fields(tctx),
"context": tctx.Metadata.MergeMinimal(e.Context),
}
utility.AddId(fields, "transaction", e.TransactionId)
utility.AddId(fields, "parent", e.ParentId)
utility.AddId(fields, "trace", e.TraceId)
utility.AddId(fields, "transaction", &e.TransactionId)
utility.AddId(fields, "parent", &e.ParentId)
utility.AddId(fields, "trace", &e.TraceId)

return []beat.Event{
beat.Event{
Expand All @@ -163,7 +197,9 @@ func (s *Event) fields(tctx *transform.Context) common.MapStr {
utility.Add(tr, "id", s.Id)
utility.Add(tr, "parent", s.Parent)
// v2
utility.Add(tr, "hex_id", s.HexId)
if s.HexId != "" {
utility.Add(tr, "hex_id", s.HexId)
}

utility.Add(tr, "name", s.Name)
utility.Add(tr, "type", s.Type)
Expand Down
Loading

0 comments on commit 308e046

Please sign in to comment.