Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive outcome from the HTTP status code when available #4165

Merged
merged 9 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": "1.5.0"
},
"event": {
"outcome": "unknown"
"outcome": "success"
},
"host": {
"architecture": "amd64",
Expand Down Expand Up @@ -210,7 +210,7 @@
"version": "1.5.0"
},
"event": {
"outcome": "unknown"
"outcome": "success"
},
"host": {
"architecture": "amd64",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@
"version": "1.5.0"
},
"event": {
"outcome": "unknown"
"outcome": "success"
},
"host": {
"architecture": "x64",
Expand Down
18 changes: 14 additions & 4 deletions model/modeldecoder/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package modeldecoder

import (
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -93,10 +94,6 @@ func decodeSpan(input Input, schema *jsonschema.Schema) (_ *model.Span, parentIn
decodeString(raw, fieldName("parent_id"), &event.ParentID)
decodeString(raw, fieldName("trace_id"), &event.TraceID)
decodeString(raw, fieldName("transaction_id"), &event.TransactionID)
decodeString(raw, fieldName("outcome"), &event.Outcome)
if event.Outcome == "" {
event.Outcome = "unknown"
}

ctx := decoder.MapStr(raw, fieldName("context"))
if ctx != nil {
Expand Down Expand Up @@ -139,6 +136,19 @@ func decodeSpan(input Input, schema *jsonschema.Schema) (_ *model.Span, parentIn
}
}
}
decodeString(raw, fieldName("outcome"), &event.Outcome)
if event.Outcome == "" {
if event.HTTP != nil && event.HTTP.StatusCode != nil {
statusCode := *event.HTTP.StatusCode
if statusCode >= http.StatusBadRequest {
event.Outcome = "failure"
} else {
event.Outcome = "success"
}
} else {
event.Outcome = "unknown"
}
}

var stacktr *model.Stacktrace
stacktr, decoder.Err = decodeStacktrace(raw[fieldName("stacktrace")], input.Config.HasShortFieldNames, decoder.Err)
Expand Down
47 changes: 46 additions & 1 deletion model/modeldecoder/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/elastic/beats/v7/libbeat/common"

"github.com/elastic/apm-server/model"
m "github.com/elastic/apm-server/model"
jalvz marked this conversation as resolved.
Show resolved Hide resolved
"github.com/elastic/apm-server/tests"
)

Expand All @@ -41,14 +42,16 @@ func TestDecodeSpan(t *testing.T) {
name, spType := "foo", "db"
start, duration := 1.2, 3.4
method, statusCode, url := "get", 200, "http://localhost"
badRequestStatusCode := 400
instance, statement, dbType, user, link, rowsAffected := "db01", "select *", "sql", "joe", "other.db.com", 34
address, port := "localhost", 8080
destServiceType, destServiceName, destServiceResource := "db", "elasticsearch", "elasticsearch"
outcome := "success"
httpCtx := map[string]interface{}{"method": "GET", "status_code": json.Number("200"), "url": url}
context := map[string]interface{}{
"a": "b",
"tags": map[string]interface{}{"a": "tag", "tag_key": 17},
"http": map[string]interface{}{"method": "GET", "status_code": json.Number("200"), "url": url},
"http": httpCtx,
"db": map[string]interface{}{
"instance": instance, "statement": statement, "type": dbType,
"user": user, "link": link, "rows_affected": json.Number("34")},
Expand Down Expand Up @@ -191,6 +194,48 @@ func TestDecodeSpan(t *testing.T) {
},
cfg: Config{Experimental: true},
},
"with derived success outcome": {
input: map[string]interface{}{
"name": name, "type": "db.postgresql.query.custom", "duration": duration, "parent_id": parentID,
"timestamp": timestampEpoch, "id": id, "trace_id": traceID,
"context": map[string]interface{}{"http": httpCtx},
},
e: &m.Span{
jalvz marked this conversation as resolved.
Show resolved Hide resolved
Metadata: metadata,
Name: name,
Type: "db",
Subtype: &subtype,
Action: &action2,
Duration: duration,
HTTP: &m.HTTP{Method: &method, StatusCode: &statusCode, URL: &url},
jalvz marked this conversation as resolved.
Show resolved Hide resolved
Timestamp: spanTime,
ParentID: parentID,
ID: id,
TraceID: traceID,
Outcome: "success",
},
},
"with derived failure outcome": {
input: map[string]interface{}{
"name": name, "type": "db.postgresql.query.custom", "duration": duration, "parent_id": parentID,
"timestamp": timestampEpoch, "id": id, "trace_id": traceID,
"context": map[string]interface{}{"http": map[string]interface{}{"status_code": json.Number("400")}},
},
e: &m.Span{
jalvz marked this conversation as resolved.
Show resolved Hide resolved
Metadata: metadata,
Name: name,
Type: "db",
Subtype: &subtype,
Action: &action2,
Duration: duration,
HTTP: &m.HTTP{StatusCode: &badRequestStatusCode},
jalvz marked this conversation as resolved.
Show resolved Hide resolved
Timestamp: spanTime,
ParentID: parentID,
ID: id,
TraceID: traceID,
Outcome: "failure",
},
},
"full valid payload": {
input: map[string]interface{}{
"name": name, "type": "messaging", "subtype": subtype, "action": action, "start": start,
Expand Down
12 changes: 11 additions & 1 deletion model/modeldecoder/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package modeldecoder

import (
"encoding/json"
"net/http"

"github.com/pkg/errors"
"github.com/santhosh-tekuri/jsonschema"
Expand Down Expand Up @@ -166,7 +167,16 @@ func decodeTransaction(input Input, schema *jsonschema.Schema) (*model.Transacti
decodeString(raw, fieldName("outcome"), &e.Outcome)
decodeFloat64(raw, fieldName("duration"), &e.Duration)
if e.Outcome == "" {
e.Outcome = "unknown"
if ctx.Http != nil && ctx.Http.Response != nil && ctx.Http.Response.StatusCode != nil {
statusCode := *ctx.Http.Response.StatusCode
if statusCode >= http.StatusInternalServerError {
e.Outcome = "failure"
} else {
e.Outcome = "success"
}
} else {
e.Outcome = "unknown"
}
}

if obj := getObject(raw, fieldName("experience")); obj != nil {
Expand Down
47 changes: 47 additions & 0 deletions model/modeldecoder/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func TestTransactionEventDecode(t *testing.T) {
page := model.Page{URL: model.ParseURL(url, ""), Referer: &referer}
request := model.Req{Method: "post", Socket: &model.Socket{}, Headers: http.Header{"User-Agent": []string{ua}}}
response := model.Resp{Finished: new(bool), MinimalResp: model.MinimalResp{Headers: http.Header{"Content-Type": []string{"text/html"}}}}
badRequestResp, internalErrorResp := 400, 500
h := model.Http{Request: &request, Response: &response}
ctxURL := model.URL{Original: &origURL}
custom := model.Custom{"abc": 1}
Expand Down Expand Up @@ -374,6 +375,52 @@ func TestTransactionEventDecode(t *testing.T) {
},
},
},
"with derived success outcome": {
input: map[string]interface{}{
"context": map[string]interface{}{
"response": map[string]interface{}{
"status_code": json.Number("400"),
},
},
},
e: &model.Transaction{
Metadata: inputMetadata,
ID: id,
Type: trType,
Name: name,
TraceID: traceID,
Duration: duration,
HTTP: &model.Http{Response: &model.Resp{
MinimalResp: model.MinimalResp{StatusCode: &badRequestResp},
jalvz marked this conversation as resolved.
Show resolved Hide resolved
}},
Timestamp: requestTime,
SpanCount: model.SpanCount{Dropped: &dropped, Started: &started},
Outcome: "success",
},
},
"with derived failure outcome": {
input: map[string]interface{}{
"context": map[string]interface{}{
"response": map[string]interface{}{
"status_code": json.Number("500"),
},
},
},
e: &model.Transaction{
Metadata: inputMetadata,
ID: id,
Type: trType,
Name: name,
TraceID: traceID,
Duration: duration,
Timestamp: requestTime,
HTTP: &model.Http{Response: &model.Resp{
MinimalResp: model.MinimalResp{StatusCode: &internalErrorResp},
}},
SpanCount: model.SpanCount{Dropped: &dropped, Started: &started},
Outcome: "failure",
},
},
} {
t.Run(name, func(t *testing.T) {
input := make(map[string]interface{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"id": "8ec7ceb990749e79b37f6dc6cd3628633618d6ce412553a552a0fa6b69419ad4"
},
"event": {
"outcome": "unknown"
"outcome": "success"
},
"host": {
"architecture": "amd64",
Expand Down Expand Up @@ -195,7 +195,7 @@
"id": "8ec7ceb990749e79b37f6dc6cd3628633618d6ce412553a552a0fa6b69419ad4"
},
"event": {
"outcome": "unknown"
"outcome": "success"
},
"host": {
"architecture": "amd64",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@
"port": 5432
},
"event": {
"outcome": "unknown"
"outcome": "success"
},
"host": {
"architecture": "x64",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"ip": "192.0.0.1"
},
"event": {
"outcome": "unknown"
"outcome": "success"
},
"http": {
"request": {
Expand Down Expand Up @@ -451,7 +451,7 @@
"port": 8000
},
"event": {
"outcome": "unknown"
"outcome": "success"
},
"labels": {
"testTagKey": "testTagValue"
Expand Down Expand Up @@ -541,7 +541,7 @@
"port": 8003
},
"event": {
"outcome": "unknown"
"outcome": "success"
},
"labels": {
"testTagKey": "testTagValue"
Expand Down Expand Up @@ -631,7 +631,7 @@
"port": 8003
},
"event": {
"outcome": "unknown"
"outcome": "success"
},
"labels": {
"testTagKey": "testTagValue"
Expand Down