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

module/apmhttp: add status code to client spans #388

Merged
merged 3 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -6,7 +6,7 @@
"properties": {
"samples": {
"type": ["object"],
"description": "Sampled application metrics collected from the agent",
"description": "Sampled application metrics collected from the agent.",
"patternProperties": {
"^[^*\"]*$": {
"$ref": "sample.json"
Expand All @@ -16,7 +16,7 @@
},
"tags": {
"type": ["object", "null"],
"description": "A flat mapping of user-defined tags with string values",
"description": "A flat mapping of user-defined tags with string values.",
"patternProperties": {
"^[^*\"]*$": {
"type": ["string", "null"],
Expand Down
7 changes: 3 additions & 4 deletions internal/apmschema/jsonschema/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
"type": ["object", "null"],
"properties": {
"name": {
"type": "string",
"type": ["string", "null"],
"maxLength": 1024
},
"version": {
"type": "string",
"type": ["string", "null"],
"maxLength": 1024
}
},
"required": ["name", "version"]
}
},
"language": {
"description": "Name and version of the programming language used",
Expand Down
13 changes: 13 additions & 0 deletions internal/apmschema/jsonschema/spans/common_span.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
"url": {
"type": ["string", "null"],
"description": "The raw url of the correlating http request."
},
"status_code": {
"type": ["integer", "null"],
"description": "The status code of the http request."
},
"method": {
"type": ["string", "null"],
"maxLength": 1024,
"description": "The method of the http request."
}
}
},
Expand Down Expand Up @@ -64,6 +73,10 @@
"type": "string",
"description": "Keyword of specific relevance in the service's domain (eg: 'db.postgresql.query', 'template.erb', etc)",
"maxLength": 1024
},
"sync": {
"type": ["boolean", "null"],
"description": "Indicates whether the span was executed synchronously or asynchronously."
}
},
"required": ["duration", "name", "type"]
Expand Down
10 changes: 10 additions & 0 deletions internal/apmschema/jsonschema/spans/v2_span.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
"start": {
"type": ["number", "null"],
"description": "Offset relative to the transaction's timestamp identifying the start of the span, in milliseconds"
},
"subtype": {
"type": ["string", "null"],
"description": "A further sub-division of the type (e.g. postgresql, elasticsearch)",
"maxLength": 1024
},
"action": {
"type": ["string", "null"],
"description": "The specific kind of event within the sub-type represented by the span (e.g. query, connect)",
"maxLength": 1024
}
},
"required": ["id", "transaction_id", "trace_id", "parent_id"]
Expand Down
8 changes: 7 additions & 1 deletion model/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (t *Time) UnmarshalJSON(data []byte) error {
// UnmarshalJSON unmarshals the JSON data into v.
func (v *HTTPSpanContext) UnmarshalJSON(data []byte) error {
var httpSpanContext struct {
URL string
URL string
StatusCode int `json:"status_code"`
}
if err := json.Unmarshal(data, &httpSpanContext); err != nil {
return err
Expand All @@ -45,6 +46,7 @@ func (v *HTTPSpanContext) UnmarshalJSON(data []byte) error {
return err
}
v.URL = u
v.StatusCode = httpSpanContext.StatusCode
return nil
}

Expand All @@ -58,6 +60,10 @@ func (v *HTTPSpanContext) MarshalFastJSON(w *fastjson.Writer) error {
} else {
w.Rewind(beforeURL)
}
if v.StatusCode > 0 {
w.RawString(`,"status_code":`)
w.Int64(int64(v.StatusCode))
}
w.RawByte('}')
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ type DatabaseSpanContext struct {
type HTTPSpanContext struct {
// URL is the request URL.
URL *url.URL

// StatusCode holds the HTTP response status code.
StatusCode int `json:"status_code,omitempty"`
}

// Context holds contextual information relating to a transaction or error.
Expand Down
3 changes: 2 additions & 1 deletion module/apmhttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {

name := r.requestName(req)
span := tx.StartSpan(name, "external.http", apm.SpanFromContext(ctx))
span.Context.SetHTTPRequest(req)
if !span.Dropped() {
traceContext = span.TraceContext()
ctx = apm.ContextWithSpan(ctx, span)
req = RequestWithContext(ctx, req)
span.Context.SetHTTPRequest(req)
} else {
span.End()
span = nil
Expand All @@ -97,6 +97,7 @@ func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if err != nil {
span.End()
} else {
span.Context.SetHTTPStatusCode(resp.StatusCode)
resp.Body = &responseBody{span: span, body: resp.Body}
}
}
Expand Down
3 changes: 2 additions & 1 deletion module/apmhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func TestClient(t *testing.T) {
assert.Equal(t, &model.SpanContext{
HTTP: &model.HTTPSpanContext{
// Note no user info included in server.URL.
URL: serverURL,
URL: serverURL,
StatusCode: statusCode,
},
}, span.Context)

Expand Down
6 changes: 6 additions & 0 deletions spancontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ func (c *SpanContext) SetHTTPRequest(req *http.Request) {
c.http.URL = req.URL
c.model.HTTP = &c.http
}

// SetHTTPStatusCode records the HTTP response status code.
func (c *SpanContext) SetHTTPStatusCode(statusCode int) {
c.http.StatusCode = statusCode
c.model.HTTP = &c.http
}