Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
http out test cases (#928)
Browse files Browse the repository at this point in the history
* http spec test example

* support for local-server based spec tests

* read test cases from file:

* the same test cases file as for C#

* moved test cases file to testdata

* updated to the lateat spec

* attributes

* fixed linter errors

* fix sync issue

* use already defined map instead
  • Loading branch information
SergeyKanzhelev authored and Bogdan Drutu committed Mar 21, 2019
1 parent 8a36f74 commit 7396103
Show file tree
Hide file tree
Showing 4 changed files with 437 additions and 5 deletions.
5 changes: 5 additions & 0 deletions plugin/ochttp/testdata/download-test-cases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This script downloads latest test cases from specs

# TODO: change the link to when test cases are merged to specs repo

curl https://raw.githubusercontent.com/census-instrumentation/opencensus-specs/master/trace/http-out-test-cases.json -o http-out-test-cases.json
274 changes: 274 additions & 0 deletions plugin/ochttp/testdata/http-out-test-cases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
[
{
"name": "Successful GET call to https://example.com",
"method": "GET",
"url": "https://example.com/",
"spanName": "/",
"spanStatus": "OK",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "example.com",
"http.status_code": "200",
"http.url": "https://example.com/"
}
},
{
"name": "Successfully POST call to https://example.com",
"method": "POST",
"url": "https://example.com/",
"spanName": "/",
"spanStatus": "OK",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "POST",
"http.host": "example.com",
"http.status_code": "200",
"http.url": "https://example.com/"
}
},
{
"name": "Name is populated as a path",
"method": "GET",
"url": "http://{host}:{port}/path/to/resource/",
"responseCode": 200,
"spanName": "/path/to/resource/",
"spanStatus": "OK",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/path/to/resource/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "200",
"http.url": "http://{host}:{port}/path/to/resource/"
}
},
{
"name": "Call that cannot resolve DNS will be reported as error span",
"method": "GET",
"url": "https://sdlfaldfjalkdfjlkajdflkajlsdjf.sdlkjafsdjfalfadslkf.com/",
"spanName": "/",
"spanStatus": "UNKNOWN",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "sdlfaldfjalkdfjlkajdflkajlsdjf.sdlkjafsdjfalfadslkf.com",
"http.url": "https://sdlfaldfjalkdfjlkajdflkajlsdjf.sdlkjafsdjfalfadslkf.com/"
}
},
{
"name": "Response code: 199. This test case is not possible to implement on some platforms as they don't allow to return this status code. Keeping this test case for visibility, but it actually simply a fallback into 200 test case",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 200,
"spanName": "/",
"spanStatus": "OK",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "200",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 200",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 200,
"spanName": "/",
"spanStatus": "OK",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "200",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 399",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 399,
"spanName": "/",
"spanStatus": "OK",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "399",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 400",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 400,
"spanName": "/",
"spanStatus": "INVALID_ARGUMENT",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "400",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 401",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 401,
"spanName": "/",
"spanStatus": "UNAUTHENTICATED",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "401",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 403",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 403,
"spanName": "/",
"spanStatus": "PERMISSION_DENIED",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "403",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 404",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 404,
"spanName": "/",
"spanStatus": "NOT_FOUND",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "404",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 429",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 429,
"spanName": "/",
"spanStatus": "RESOURCE_EXHAUSTED",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "429",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 501",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 501,
"spanName": "/",
"spanStatus": "UNIMPLEMENTED",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "501",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 503",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 503,
"spanName": "/",
"spanStatus": "UNAVAILABLE",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "503",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 504",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 504,
"spanName": "/",
"spanStatus": "DEADLINE_EXCEEDED",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "504",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "Response code: 600",
"method": "GET",
"url": "http://{host}:{port}/",
"responseCode": 600,
"spanName": "/",
"spanStatus": "UNKNOWN",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "600",
"http.url": "http://{host}:{port}/"
}
},
{
"name": "User agent attribute populated",
"method": "GET",
"url": "http://{host}:{port}/",
"headers": {
"User-Agent": "test-user-agent"
},
"responseCode": 200,
"spanName": "/",
"spanStatus": "OK",
"spanKind": "Client",
"spanAttributes": {
"http.path": "/",
"http.method": "GET",
"http.host": "{host}:{port}",
"http.status_code": "200",
"http.user_agent": "test-user-agent",
"http.url": "http://{host}:{port}/"
}
}
]
14 changes: 12 additions & 2 deletions plugin/ochttp/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
HostAttribute = "http.host"
MethodAttribute = "http.method"
PathAttribute = "http.path"
URLAttribute = "http.url"
UserAgentAttribute = "http.user_agent"
StatusCodeAttribute = "http.status_code"
)
Expand Down Expand Up @@ -150,12 +151,21 @@ func spanNameFromURL(req *http.Request) string {
}

func requestAttrs(r *http.Request) []trace.Attribute {
return []trace.Attribute{
userAgent := r.UserAgent()

attrs := make([]trace.Attribute, 0, 5)
attrs = append(attrs,
trace.StringAttribute(PathAttribute, r.URL.Path),
trace.StringAttribute(URLAttribute, r.URL.String()),
trace.StringAttribute(HostAttribute, r.Host),
trace.StringAttribute(MethodAttribute, r.Method),
trace.StringAttribute(UserAgentAttribute, r.UserAgent()),
)

if userAgent != "" {
attrs = append(attrs, trace.StringAttribute(UserAgentAttribute, userAgent))
}

return attrs
}

func responseAttrs(resp *http.Response) []trace.Attribute {
Expand Down
Loading

0 comments on commit 7396103

Please sign in to comment.