Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

[Tracing]: Start root span on Proxy #64

Merged
merged 4 commits into from
Jul 9, 2020
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
5 changes: 5 additions & 0 deletions changelog/unreleased/root-tracing.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 3 additions & 0 deletions pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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, "/")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/flagset/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 19 additions & 2 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -140,6 +147,16 @@ func (p *MultiHostReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request
Msgf("policy %v is not configured", pol)
}

ctx := context.Background()
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
Expand Down Expand Up @@ -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 {
Expand Down