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

[TT-7902] Detailed Tracing #5294

Merged
merged 26 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1d86a23
Detailed Tracing
mativm02 Jul 7, 2023
10c54c7
reverting transport changes
mativm02 Jul 7, 2023
45f08e1
Merge branch 'master' into TT-7902
mativm02 Jul 7, 2023
cc5fb1d
removing omitempty from json tag
mativm02 Jul 10, 2023
6027a77
Merge branch 'TT-7902' of github.com:TykTechnologies/tyk into TT-7902
mativm02 Jul 10, 2023
0b50bb7
Merge branch 'master' into TT-7902
mativm02 Jul 10, 2023
b828b57
Merge branch 'master' into TT-7902
mativm02 Jul 10, 2023
f1df8e1
saving errors on non-detailed traces as well
mativm02 Jul 11, 2023
eaa1139
Merge branch 'TT-7902' of github.com:TykTechnologies/tyk into TT-7902
mativm02 Jul 11, 2023
6bd64b7
linting
mativm02 Jul 11, 2023
f0d6d7c
Merge branch 'master' into TT-7902
mativm02 Jul 11, 2023
8bd46b4
fixing test
mativm02 Jul 11, 2023
b24ad00
Merge branch 'TT-7902' of github.com:TykTechnologies/tyk into TT-7902
mativm02 Jul 11, 2023
ae25d2a
importing otel library from tag
mativm02 Jul 11, 2023
81d8702
linting
mativm02 Jul 11, 2023
de7556f
Merge branch 'master' into TT-7902
mativm02 Jul 12, 2023
bb582fc
Merge branch 'master' into TT-7902
mativm02 Jul 20, 2023
5ffd2c6
linting
mativm02 Jul 20, 2023
1ee9a1f
updating tests to use detailed tracing
mativm02 Jul 21, 2023
3ff2a8b
Merge branch 'master' into TT-7902
mativm02 Jul 21, 2023
a175801
Merge branch 'master' of github.com:TykTechnologies/tyk into TT-7902
mativm02 Jul 21, 2023
3120cdc
Merge branch 'master' of github.com:TykTechnologies/tyk into TT-7902
mativm02 Jul 24, 2023
84ef4de
adding e2e tests
mativm02 Jul 24, 2023
c3284a5
Merge branch 'TT-7902' of github.com:TykTechnologies/tyk into TT-7902
mativm02 Jul 24, 2023
6b84656
moving imports to internal package
mativm02 Jul 24, 2023
8c83546
Merge branch 'master' into TT-7902
mativm02 Jul 31, 2023
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
21 changes: 12 additions & 9 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ tasks:
sources:
- go.mod
- go.sum
- './**/*.go'

- "./**/*.go"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change made by the linter


test:plugin-compiler:
desc: "Plugin compiler local build/test"
Expand Down Expand Up @@ -51,7 +50,6 @@ tasks:
- docker compose -f ci/tests/tracing/docker-compose.yml up -d
- tracetest configure -g --endpoint http://localhost:11633


test:opentelemetry:test:
desc: "run e2e opentelemetry tests"
deps:
Expand All @@ -63,16 +61,21 @@ tasks:
- tracetest test run -d ./ci/tests/tracing/scenarios/tyk_tykprotocol_200.yml -w -o pretty
- tracetest test run -d ./ci/tests/tracing/scenarios/tyk_tykprotocol-auth_401.yml -w -o pretty
- tracetest test run -d ./ci/tests/tracing/scenarios/tyk_grpcapi_200.yml -w -o pretty

- tracetest test run -d ./ci/tests/tracing/scenarios/tyk_versioned_200.yml -w -o pretty
- tracetest test run -d ./ci/tests/tracing/scenarios/tyk_versioned_403.yml -w -o pretty
- tracetest test run -d ./ci/tests/tracing/scenarios/tyk_versioned_not_detailed_200.yml -w -o pretty
- tracetest test run -d ./ci/tests/tracing/scenarios/tyk_versioned_not_detailed_403.yml -w -o pretty

test:opentelemetry:teardown:
desc: "teardown e2e opentelemetry tests enviroment"
cmds:
- docker compose -f ci/tests/tracing/docker-compose.yml down --remove-orphans

test:opentelemetry:
desc : "setup, execute and shutdown e2e opentelemetry tests"
desc: "setup, execute and shutdown e2e opentelemetry tests"
cmds:
- task: test:opentelemetry:setup
- task: test:opentelemetry:test
- task: test:opentelemetry:teardown
- |
set -e
task test:opentelemetry:setup
trap 'task test:opentelemetry:teardown' EXIT
Comment on lines +77 to +81
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ensuring teardown is executed no matter if the test failed or not

task test:opentelemetry:test
2 changes: 2 additions & 0 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ type APIDefinition struct {
// IsOAS is set to true when API has an OAS definition (created in OAS or migrated to OAS)
IsOAS bool `bson:"is_oas" json:"is_oas,omitempty"`
VersionName string `bson:"-" json:"-"`

DetailedTracing bool `bson:"detailed_tracing" json:"detailed_tracing"`
}

type AnalyticsPluginConfig struct {
Expand Down
5 changes: 4 additions & 1 deletion apidef/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,10 @@ const Schema = `{
},
"is_oas": {
"type": "boolean"
}
},
"detailed_tracing": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this linted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is!

"type": "boolean"
}
},
"required": [
"name",
Expand Down
3 changes: 2 additions & 1 deletion ci/tests/tracing/apps/grpcapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -471,5 +471,6 @@
"oidc": {}
},
"response_processors": [],
"use_mutual_tls_auth": false
"use_mutual_tls_auth": false,
"detailed_tracing":true
}
3 changes: 2 additions & 1 deletion ci/tests/tracing/apps/test-auth.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"target_url": "http://httpbin:80/",
"strip_listen_path": true
},
"enable_batch_request_support": true
"enable_batch_request_support": true,
"detailed_tracing":true
}
3 changes: 2 additions & 1 deletion ci/tests/tracing/apps/test-ratelimit.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
"target_url": "http://httpbin:80/",
"strip_listen_path": true
},
"enable_batch_request_support": true
"enable_batch_request_support": true,
"detailed_tracing":true
}
3 changes: 2 additions & 1 deletion ci/tests/tracing/apps/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"target_url": "http://httpbin:80/",
"strip_listen_path": true
},
"do_not_track": true
"do_not_track": true,
"detailed_tracing":true
}
3 changes: 2 additions & 1 deletion ci/tests/tracing/apps/tykprotocol-auth.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"target_url": "tyk://1/",
"strip_listen_path": true
},
"do_not_track": true
"do_not_track": true,
"detailed_tracing":true
}
3 changes: 2 additions & 1 deletion ci/tests/tracing/apps/tykprotocol.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"target_url": "tyk://3/",
"strip_listen_path": true
},
"do_not_track": true
"do_not_track": true,
"detailed_tracing":true
}
109 changes: 109 additions & 0 deletions ci/tests/tracing/apps/versioned-not-detailed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"name": "VersionedNotDetailedAPI",
"api_id": "7",
"org_id": "default",
"definition": {
"enabled": false,
"name": "",
"default": "",
"location": "url-param",
"key": "version",
"strip_path": false,
"strip_versioning_data": false,
"versions": {}
},
"use_keyless": true,
"auth": {
"auth_header_name": ""
},
"version_data": {
"not_versioned": false,
"default_version": "v1",
"versions": {
"v1": {
"name": "v1",
"expires": "2080-07-28 15:00",
"paths": {
"ignored": [],
"white_list": [],
"black_list": []
},
"use_extended_paths": true,
"extended_paths": {
"ignored": [],
"white_list": [],
"black_list": [],
"transform": [],
"transform_response": [],
"transform_jq": [],
"transform_jq_response": [],
"transform_headers": [],
"transform_response_headers": [],
"hard_timeouts": [],
"circuit_breakers": [],
"url_rewrites": [],
"virtual": [],
"size_limits": [],
"method_transforms": [],
"track_endpoints": [],
"do_not_track_endpoints": [],
"validate_json": [],
"internal": [],
"persist_graphql": []
},
"global_headers": {},
"global_headers_remove": [],
"global_response_headers": {},
"global_response_headers_remove": [],
"ignore_endpoint_case": false,
"global_size_limit": 0,
"override_target": "https://httpbin.org"
},
"v2": {
"name": "v2",
"expires": "2080-07-29 15:00",
"paths": {
"ignored": [],
"white_list": [],
"black_list": []
},
"use_extended_paths": true,
"extended_paths": {
"ignored": [],
"white_list": [],
"black_list": [],
"transform": [],
"transform_response": [],
"transform_jq": [],
"transform_jq_response": [],
"transform_headers": [],
"transform_response_headers": [],
"hard_timeouts": [],
"circuit_breakers": [],
"url_rewrites": [],
"virtual": [],
"size_limits": [],
"method_transforms": [],
"track_endpoints": [],
"do_not_track_endpoints": [],
"validate_json": [],
"internal": [],
"persist_graphql": []
},
"global_headers": {},
"global_headers_remove": [],
"global_response_headers": {},
"global_response_headers_remove": [],
"ignore_endpoint_case": false,
"global_size_limit": 0,
"override_target": "https://httpbin.org"
}
}
},
"proxy": {
"listen_path": "/versioned-not-detailed/",
"target_url": "http://httpbin:80/",
"strip_listen_path": true
},
"do_not_track": true
}
3 changes: 2 additions & 1 deletion ci/tests/tracing/apps/versioned.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@
"target_url": "http://httpbin:80/",
"strip_listen_path": true
},
"do_not_track": true
"do_not_track": true,
"detailed_tracing":true
}
21 changes: 21 additions & 0 deletions ci/tests/tracing/scenarios/tyk_versioned_not_detailed_200.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type: Test
spec:
id: qS_lLV3Vg
name: Versioned not detailed API
trigger:
type: http
httpRequest:
method: GET
url: tyk:8080/versioned-not-detailed/ip?version=v1
headers:
- key: Content-Type
value: application/json
specs:
- selector: span[tracetest.span.type="http" name="GET /versioned-not-detailed/ip" http.method="GET"]
name: Checking API attributes + version
assertions:
- attr:tyk.api.id = 7
- attr:tyk.api.name = "VersionedNotDetailedAPI"
- attr:tyk.api.orgid = "default"
- attr:tyk.api.path = "/versioned-not-detailed/"
- attr:tyk.api.version = "v1"
22 changes: 22 additions & 0 deletions ci/tests/tracing/scenarios/tyk_versioned_not_detailed_403.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type: Test
spec:
id: p1BLP4q4g
name: Versioned not detailed error
trigger:
type: http
httpRequest:
method: GET
url: tyk:8080/versioned-not-detailed/ip?version=v3
headers:
- key: Content-Type
value: application/json
specs:
- selector: span[tracetest.span.type="http" name="GET /versioned-not-detailed/ip" http.method="GET"]
name: Checking attributes + wrong version in attributes
assertions:
- attr:tyk.api.version = "v3"
- attr:http.status_code = 403
- attr:tyk.api.id = 7
- attr:tyk.api.name = "VersionedNotDetailedAPI"
- attr:tyk.api.orgid = "default"
- attr:tyk.api.path = "/versioned-not-detailed/"
19 changes: 11 additions & 8 deletions gateway/api_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ func TestAllApisAreMTLS(t *testing.T) {
}

func TestOpenTelemetry(t *testing.T) {

t.Run("Opentelemetry enabled - check if we are sending traces", func(t *testing.T) {
otelCollectorMock := httpCollectorMock(t, func(w http.ResponseWriter, r *http.Request) {
//check the body
Expand Down Expand Up @@ -466,15 +465,19 @@ func TestOpenTelemetry(t *testing.T) {
globalConf.OpenTelemetry.SpanProcessorType = "simple"
})
defer ts.Close()
detailedTracing := []bool{true, false}
for _, detailed := range detailedTracing {
ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.APIID = "test"
spec.Proxy.ListenPath = "/my-api/"
spec.UseKeylessAccess = true
spec.DetailedTracing = detailed
})

ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.APIID = "test"
spec.Proxy.ListenPath = "/my-api/"
spec.UseKeylessAccess = true
})
_, _ = ts.Run(t, test.TestCase{Path: "/my-api/", Code: http.StatusOK})
assert.Equal(t, "otel", ts.Gw.TracerProvider.Type())
}

_, _ = ts.Run(t, test.TestCase{Path: "/my-api/", Code: http.StatusOK})
assert.Equal(t, "otel", ts.Gw.TracerProvider.Type())
})

t.Run("Opentelemetry disabled - check if we are not sending traces", func(t *testing.T) {
Expand Down
19 changes: 12 additions & 7 deletions gateway/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gateway

import (
"bytes"
"context"
"crypto/md5"
"errors"
"fmt"
Expand Down Expand Up @@ -66,17 +67,21 @@ func (tr TraceMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request,
setContext(r, ctx)
return tr.TykMiddleware.ProcessRequest(w, r, conf)
} else if baseMw := tr.Base(); baseMw != nil {
if cfg := baseMw.Gw.GetConfig(); cfg.OpenTelemetry.Enabled {
ctx, span := baseMw.Gw.TracerProvider.Tracer().Start(r.Context(), tr.Name())
defer span.End()

setContext(r, ctx)
cfg := baseMw.Gw.GetConfig()
if cfg.OpenTelemetry.Enabled {
var span otel.Span
if baseMw.Spec.DetailedTracing {
var ctx context.Context
ctx, span = baseMw.Gw.TracerProvider.Tracer().Start(r.Context(), tr.Name())
defer span.End()
setContext(r, ctx)
} else {
span = otel.SpanFromContext(r.Context())
}

err, i := tr.TykMiddleware.ProcessRequest(w, r, conf)
if err != nil {
span.SetStatus(otel.SPAN_STATUS_ERROR, err.Error())
} else {
span.SetStatus(otel.SPAN_STATUS_OK, "")
}

attrs := ctxGetSpanAttributes(r, tr.TykMiddleware.Name())
Expand Down
6 changes: 6 additions & 0 deletions internal/otel/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type (
Sampling = otelconfig.Sampling

SpanAttribute = tyktrace.Attribute

Span = tyktrace.Span
)

// HTTP Handlers
Expand Down Expand Up @@ -117,3 +119,7 @@ var APIKeyAttribute = semconv.TykAPIKey
var APIKeyAliasAttribute = semconv.TykAPIKeyAlias

var OAuthClientIDAttribute = semconv.TykOauthID

func SpanFromContext(ctx context.Context) tyktrace.Span {
return tyktrace.SpanFromContext(ctx)
}
Loading