From 94adb97ac1660a9e680be216c2c1f7e8e42587f1 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Thu, 9 Jul 2020 14:15:20 +0200 Subject: [PATCH 1/4] create root span on ocis-proxy --- go.sum | 1 + pkg/command/server.go | 3 +++ pkg/flagset/flagset.go | 2 +- pkg/proxy/proxy.go | 21 +++++++++++++++++++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/go.sum b/go.sum index 75aeda852..1abba4df5 100644 --- a/go.sum +++ b/go.sum @@ -175,6 +175,7 @@ github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.0 h1:yTUvW7Vhb89inJ+8irsUqiWjh8iT6sQPZiQzI6ReGkA= github.com/cespare/xxhash/v2 v2.1.0/go.mod h1:dgIUBU3pDso/gPgZ1osOZ0iQf77oPR28Tjxl5dIMyVM= +github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cheekybits/genny v1.0.0/go.mod h1:+tQajlRqAUrPI7DOSpB0XAqZYtQakVtB7wXkRAgjxjQ= github.com/cheggaaa/pb v1.0.28/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s= diff --git a/pkg/command/server.go b/pkg/command/server.go index 916efc787..59f3424e1 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "os/signal" + "strconv" "strings" "time" @@ -42,6 +43,8 @@ func Server(cfg *config.Config) *cli.Command { Usage: "Start integrated server", Flags: flagset.ServerWithConfig(cfg), Before: func(ctx *cli.Context) error { + l := NewLogger(cfg) + l.Debug().Str("tracing", strconv.FormatBool(cfg.Tracing.Enabled)).Msg("init: before") if cfg.HTTP.Root != "/" { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") } diff --git a/pkg/flagset/flagset.go b/pkg/flagset/flagset.go index bb72c29a3..ad9b71429 100644 --- a/pkg/flagset/flagset.go +++ b/pkg/flagset/flagset.go @@ -77,7 +77,7 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { }, &cli.StringFlag{ Name: "tracing-collector", - Value: "", + Value: "http://localhost:14268/api/traces", Usage: "Endpoint for the collector", EnvVars: []string{"PROXY_TRACING_COLLECTOR"}, Destination: &cfg.Tracing.Collector, diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index 5d1340bdf..9b1fc83bd 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -1,13 +1,17 @@ package proxy import ( - "github.com/owncloud/ocis-proxy/pkg/proxy/policy" + "context" "net/http" "net/http/httputil" "net/url" "regexp" "strings" + "github.com/owncloud/ocis-proxy/pkg/proxy/policy" + "go.opencensus.io/plugin/ochttp/propagation/tracecontext" + "go.opencensus.io/trace" + "github.com/owncloud/ocis-pkg/v2/log" "github.com/owncloud/ocis-proxy/pkg/config" ) @@ -18,6 +22,8 @@ type MultiHostReverseProxy struct { Directors map[string]map[config.RouteType]map[string]func(req *http.Request) PolicySelector policy.Selector logger log.Logger + propagator tracecontext.HTTPFormat + config *config.Config } // NewMultiHostReverseProxy undocummented @@ -27,6 +33,7 @@ func NewMultiHostReverseProxy(opts ...Option) *MultiHostReverseProxy { rp := &MultiHostReverseProxy{ Directors: make(map[string]map[config.RouteType]map[string]func(req *http.Request)), logger: options.Logger, + config: options.Config, } if options.Config.Policies == nil { @@ -140,6 +147,16 @@ func (p *MultiHostReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request Msgf("policy %v is not configured", pol) } + var ctx context.Context + var span *trace.Span + + // Start root span. + if p.config.Tracing.Enabled { + ctx, span = trace.StartSpan(context.Background(), r.URL.String()) + defer span.End() + p.propagator.SpanContextToRequest(span.SpanContext(), r) + } + Loop: for _, rt := range config.RouteTypes { var handler func(string, url.URL) bool @@ -175,7 +192,7 @@ Loop: } // Call upstream ServeHTTP - p.ReverseProxy.ServeHTTP(w, r) + p.ReverseProxy.ServeHTTP(w, r.WithContext(ctx)) } func (p MultiHostReverseProxy) queryRouteMatcher(endpoint string, target url.URL) bool { From 9ddf0d555bda34b1eb7ab60fbc60d4966284958e Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Thu, 9 Jul 2020 14:29:42 +0200 Subject: [PATCH 2/4] avoid setting a nil context --- pkg/proxy/proxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index 9b1fc83bd..9835e6dba 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -147,7 +147,7 @@ func (p *MultiHostReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request Msgf("policy %v is not configured", pol) } - var ctx context.Context + ctx := context.Background() var span *trace.Span // Start root span. From ffd4e96bf0a19a98c0da3ee080972f5f4a4c5728 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Thu, 9 Jul 2020 14:38:50 +0200 Subject: [PATCH 3/4] add changelog --- changelog/unreleased/root-tracing.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/unreleased/root-tracing.md diff --git a/changelog/unreleased/root-tracing.md b/changelog/unreleased/root-tracing.md new file mode 100644 index 000000000..5b5a67cba --- /dev/null +++ b/changelog/unreleased/root-tracing.md @@ -0,0 +1,5 @@ +Enhancement: Create a root span on proxy that propagates down to consumers. + +In order to propagate and correctly associate a span with a request we need a root span that gets sent to other services. + +https://github.com/owncloud/ocis-proxy/pull/64 From b85bd3b54538d385e2369035835572df4435c491 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Thu, 9 Jul 2020 14:48:41 +0200 Subject: [PATCH 4/4] remove final punctuation --- changelog/unreleased/root-tracing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/root-tracing.md b/changelog/unreleased/root-tracing.md index 5b5a67cba..56f556bb7 100644 --- a/changelog/unreleased/root-tracing.md +++ b/changelog/unreleased/root-tracing.md @@ -1,4 +1,4 @@ -Enhancement: Create a root span on proxy that propagates down to consumers. +Enhancement: Create a root span on proxy that propagates down to consumers In order to propagate and correctly associate a span with a request we need a root span that gets sent to other services.