From b2b2ff6b30737a858d673e76896f07477594a0d2 Mon Sep 17 00:00:00 2001 From: Jorrit Salverda Date: Sat, 25 May 2019 15:12:27 +0200 Subject: [PATCH 1/2] Add opentracing-operation-name and opentracing-location-operation-name config settings With these settings custom span names can be used for the server span and location span Signed-off-by: Jorrit Salverda --- .../nginx-configuration/configmap.md | 14 +++ .../third-party-addons/opentracing.md | 6 + internal/ingress/controller/config/config.go | 6 + rootfs/etc/nginx/template/nginx.tmpl | 6 + test/e2e/settings/opentracing.go | 109 ++++++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 test/e2e/settings/opentracing.go diff --git a/docs/user-guide/nginx-configuration/configmap.md b/docs/user-guide/nginx-configuration/configmap.md index cb3b6798c..afe3c8782 100755 --- a/docs/user-guide/nginx-configuration/configmap.md +++ b/docs/user-guide/nginx-configuration/configmap.md @@ -115,6 +115,8 @@ The following table shows a configuration option's name, type, and the default v |[proxy-add-original-uri-header](#proxy-add-original-uri-header)|bool|"true"| |[generate-request-id](#generate-request-id)|bool|"true"| |[enable-opentracing](#enable-opentracing)|bool|"false"| +|[opentracing-operation-name](#opentracing-operation-name)|string|""| +|[opentracing-location-operation-name](#opentracing-location-operation-name)|string|""| |[zipkin-collector-host](#zipkin-collector-host)|string|""| |[zipkin-collector-port](#zipkin-collector-port)|int|9411| |[zipkin-service-name](#zipkin-service-name)|string|"nginx"| @@ -700,6 +702,18 @@ Enables the nginx Opentracing extension. _**default:**_ is disabled _References:_ [https://github.com/opentracing-contrib/nginx-opentracing](https://github.com/opentracing-contrib/nginx-opentracing) +## opentracing-operation-name + +Specifies a custom name for the server span. _**default:**_ is empty + +For example, set to "HTTP $request_method $uri". + +## opentracing-location-operation-name + +Specifies a custom name for the location span. _**default:**_ is empty + +For example, set to "HTTP $request_method $uri". + ## zipkin-collector-host Specifies the host to use when uploading traces. It must be a valid URL. diff --git a/docs/user-guide/third-party-addons/opentracing.md b/docs/user-guide/third-party-addons/opentracing.md index 18b41b00c..458068c3e 100644 --- a/docs/user-guide/third-party-addons/opentracing.md +++ b/docs/user-guide/third-party-addons/opentracing.md @@ -30,6 +30,12 @@ have been tested. Other optional configuration options: ``` +# specifies the name to use for the server span +opentracing-operation-name + +# specifies specifies the name to use for the location span +opentracing-location-operation-name + # specifies the port to use when uploading traces, Default: 9411 zipkin-collector-port diff --git a/internal/ingress/controller/config/config.go b/internal/ingress/controller/config/config.go index f4f7897a8..7d58f42e0 100644 --- a/internal/ingress/controller/config/config.go +++ b/internal/ingress/controller/config/config.go @@ -460,6 +460,12 @@ type Configuration struct { // By default this is disabled EnableOpentracing bool `json:"enable-opentracing"` + // OpentracingOperationName specifies a custom name for the server span + OpentracingOperationName string `json:"opentracing-operation-name"` + + // OpentracingOperationName specifies a custom name for the location span + OpentracingLocationOperationName string `json:"opentracing-location-operation-name"` + // ZipkinCollectorHost specifies the host to use when uploading traces ZipkinCollectorHost string `json:"zipkin-collector-host"` diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index fccb83cca..fb30623aa 100755 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -225,6 +225,12 @@ http { {{ if $cfg.EnableOpentracing }} opentracing on; + {{ if not (empty $cfg.OpentracingOperationName) }} + opentracing_operation_name "{{ $cfg.OpentracingOperationName }}"; + {{ end }} + {{ if not (empty $cfg.OpentracingLocationOperationName) }} + opentracing_location_operation_name "{{ $cfg.OpentracingLocationOperationName }}"; + {{ end }} {{ end }} {{ buildOpentracing $cfg }} diff --git a/test/e2e/settings/opentracing.go b/test/e2e/settings/opentracing.go new file mode 100644 index 000000000..10a3c1a55 --- /dev/null +++ b/test/e2e/settings/opentracing.go @@ -0,0 +1,109 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package settings + +import ( + "strings" + + . "github.com/onsi/ginkgo" + + "k8s.io/ingress-nginx/test/e2e/framework" +) + +var _ = framework.IngressNginxDescribe("Configure OpenTracing", func() { + f := framework.NewDefaultFramework("enable-opentracing") + enableOpentracing := "enable-opentracing" + opentracingOperationName := "opentracing-operation-name" + opentracingLocationOperationName := "opentracing-location-operation-name" + + BeforeEach(func() { + f.NewEchoDeployment() + }) + + AfterEach(func() { + }) + + It("should not exists opentracing directive", func() { + f.UpdateNginxConfigMapData(enableOpentracing, "false") + + f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) + + f.WaitForNginxConfiguration( + func(cfg string) bool { + return !strings.Contains(cfg, "opentracing on") + }) + }) + + It("should exists opentracing directive when is enabled", func() { + f.UpdateNginxConfigMapData(enableOpentracing, "true") + + f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) + + f.WaitForNginxConfiguration( + func(cfg string) bool { + return strings.Contains(cfg, "opentracing on") + }) + }) + + It("should not exists opentracing_operation_name directive when is empty", func() { + f.UpdateNginxConfigMapData(enableOpentracing, "true") + f.UpdateNginxConfigMapData(opentracingOperationName, "") + + f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) + + f.WaitForNginxConfiguration( + func(cfg string) bool { + return !strings.Contains(cfg, "opentracing_operation_name") + }) + }) + + It("should exists opentracing_operation_name directive when is configured", func() { + f.UpdateNginxConfigMapData(enableOpentracing, "true") + f.UpdateNginxConfigMapData(opentracingOperationName, "HTTP $request_method $uri") + + f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) + + f.WaitForNginxConfiguration( + func(cfg string) bool { + return strings.Contains(cfg, "opentracing_operation_name \"HTTP $request_method $uri\"") + }) + }) + + It("should not exists opentracing_operation_name directive when is empty", func() { + f.UpdateNginxConfigMapData(enableOpentracing, "true") + f.UpdateNginxConfigMapData(opentracingLocationOperationName, "") + + f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) + + f.WaitForNginxConfiguration( + func(cfg string) bool { + return !strings.Contains(cfg, "opentracing_location_operation_name") + }) + }) + + It("should exists opentracing_operation_name directive when is configured", func() { + f.UpdateNginxConfigMapData(enableOpentracing, "true") + f.UpdateNginxConfigMapData(opentracingLocationOperationName, "HTTP $request_method $uri") + + f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) + + f.WaitForNginxConfiguration( + func(cfg string) bool { + return strings.Contains(cfg, "opentracing_location_operation_name \"HTTP $request_method $uri\"") + }) + }) +}) From ca6f78425c81cb28f606a12975aad3d47bece140 Mon Sep 17 00:00:00 2001 From: Bernard Van De Walle Date: Thu, 23 Jul 2020 12:55:18 -0700 Subject: [PATCH 2/2] Adding Zipkin collector to the E2E opentracing test as it is required to load at least one tracer to enable opentracing Work on PR comments Add tests for template builder Signed-off-by: Bernard Van De Walle --- .../ingress/controller/template/template.go | 7 +++++ .../controller/template/template_test.go | 15 +++++++++++ rootfs/etc/nginx/template/nginx.tmpl | 10 ------- test/e2e/settings/opentracing.go | 26 ++++++++++++------- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index 3666defbf..d9dc6aec7 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -970,6 +970,13 @@ func buildOpentracing(c interface{}, s interface{}) string { buf.WriteString("\r\n") + if cfg.OpentracingOperationName != "" { + buf.WriteString(fmt.Sprintf("opentracing_operation_name \"%s\";\n", cfg.OpentracingOperationName)) + } + if cfg.OpentracingLocationOperationName != "" { + buf.WriteString(fmt.Sprintf("opentracing_location_operation_name \"%s\";\n", cfg.OpentracingLocationOperationName)) + } + return buf.String() } diff --git a/internal/ingress/controller/template/template_test.go b/internal/ingress/controller/template/template_test.go index 2f2dd670a..488636653 100644 --- a/internal/ingress/controller/template/template_test.go +++ b/internal/ingress/controller/template/template_test.go @@ -1193,6 +1193,21 @@ func TestBuildOpenTracing(t *testing.T) { t.Errorf("Expected '%v' but returned '%v'", expected, actual) } + cfgOpenTracing := config.Configuration{ + EnableOpentracing: true, + DatadogCollectorHost: "datadog-host.com", + OpentracingOperationName: "my-operation-name", + OpentracingLocationOperationName: "my-location-operation-name", + } + expected = "opentracing_load_tracer /usr/local/lib64/libdd_opentracing.so /etc/nginx/opentracing.json;\r\n" + expected += "opentracing_operation_name \"my-operation-name\";\n" + expected += "opentracing_location_operation_name \"my-location-operation-name\";\n" + actual = buildOpentracing(cfgOpenTracing, []*ingress.Server{}) + + if expected != actual { + t.Errorf("Expected '%v' but returned '%v'", expected, actual) + } + } func TestEnforceRegexModifier(t *testing.T) { diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index bf7bc1029..46c7fdbfa 100755 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -276,16 +276,6 @@ http { limit_req_status {{ $cfg.LimitReqStatusCode }}; limit_conn_status {{ $cfg.LimitConnStatusCode }}; - {{ if $cfg.EnableOpentracing }} - opentracing on; - {{ if not (empty $cfg.OpentracingOperationName) }} - opentracing_operation_name "{{ $cfg.OpentracingOperationName }}"; - {{ end }} - {{ if not (empty $cfg.OpentracingLocationOperationName) }} - opentracing_location_operation_name "{{ $cfg.OpentracingLocationOperationName }}"; - {{ end }} - {{ end }} - {{ buildOpentracing $cfg $servers }} include /etc/nginx/mime.types; diff --git a/test/e2e/settings/opentracing.go b/test/e2e/settings/opentracing.go index 10a3c1a55..7367ccdbb 100644 --- a/test/e2e/settings/opentracing.go +++ b/test/e2e/settings/opentracing.go @@ -1,5 +1,5 @@ /* -Copyright 2018 The Kubernetes Authors. +Copyright 2020 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -19,7 +19,7 @@ package settings import ( "strings" - . "github.com/onsi/ginkgo" + "github.com/onsi/ginkgo" "k8s.io/ingress-nginx/test/e2e/framework" ) @@ -27,17 +27,18 @@ import ( var _ = framework.IngressNginxDescribe("Configure OpenTracing", func() { f := framework.NewDefaultFramework("enable-opentracing") enableOpentracing := "enable-opentracing" + zipkinCollectorHost := "zipkin-collector-host" opentracingOperationName := "opentracing-operation-name" opentracingLocationOperationName := "opentracing-location-operation-name" - BeforeEach(func() { + ginkgo.BeforeEach(func() { f.NewEchoDeployment() }) - AfterEach(func() { + ginkgo.AfterEach(func() { }) - It("should not exists opentracing directive", func() { + ginkgo.It("should not exists opentracing directive", func() { f.UpdateNginxConfigMapData(enableOpentracing, "false") f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) @@ -48,8 +49,9 @@ var _ = framework.IngressNginxDescribe("Configure OpenTracing", func() { }) }) - It("should exists opentracing directive when is enabled", func() { + ginkgo.It("should exists opentracing directive when is enabled", func() { f.UpdateNginxConfigMapData(enableOpentracing, "true") + f.UpdateNginxConfigMapData(zipkinCollectorHost, "127.0.0.1") f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) @@ -59,8 +61,9 @@ var _ = framework.IngressNginxDescribe("Configure OpenTracing", func() { }) }) - It("should not exists opentracing_operation_name directive when is empty", func() { + ginkgo.It("should not exists opentracing_operation_name directive when is empty", func() { f.UpdateNginxConfigMapData(enableOpentracing, "true") + f.UpdateNginxConfigMapData(zipkinCollectorHost, "127.0.0.1") f.UpdateNginxConfigMapData(opentracingOperationName, "") f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) @@ -71,8 +74,9 @@ var _ = framework.IngressNginxDescribe("Configure OpenTracing", func() { }) }) - It("should exists opentracing_operation_name directive when is configured", func() { + ginkgo.It("should exists opentracing_operation_name directive when is configured", func() { f.UpdateNginxConfigMapData(enableOpentracing, "true") + f.UpdateNginxConfigMapData(zipkinCollectorHost, "127.0.0.1") f.UpdateNginxConfigMapData(opentracingOperationName, "HTTP $request_method $uri") f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) @@ -83,8 +87,9 @@ var _ = framework.IngressNginxDescribe("Configure OpenTracing", func() { }) }) - It("should not exists opentracing_operation_name directive when is empty", func() { + ginkgo.It("should not exists opentracing_location_operation_name directive when is empty", func() { f.UpdateNginxConfigMapData(enableOpentracing, "true") + f.UpdateNginxConfigMapData(zipkinCollectorHost, "127.0.0.1") f.UpdateNginxConfigMapData(opentracingLocationOperationName, "") f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil)) @@ -95,8 +100,9 @@ var _ = framework.IngressNginxDescribe("Configure OpenTracing", func() { }) }) - It("should exists opentracing_operation_name directive when is configured", func() { + ginkgo.It("should exists opentracing_location_operation_name directive when is configured", func() { f.UpdateNginxConfigMapData(enableOpentracing, "true") + f.UpdateNginxConfigMapData(zipkinCollectorHost, "127.0.0.1") f.UpdateNginxConfigMapData(opentracingLocationOperationName, "HTTP $request_method $uri") f.EnsureIngress(framework.NewSingleIngress(enableOpentracing, "/", enableOpentracing, f.Namespace, "http-svc", 80, nil))