Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.
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
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ All notable changes to Sourcegraph are documented in this file.
### Added

- Users and site administrators can now view a log of their actions/events in the user settings.
- Distributed tracing is a powerful tool for investigating performance issues. The following changes have been made with the goal of making it easier to use distributed tracing with Sourcegraph:

- The site configuration field `"observability.tracing": { "sampling": "..." }` allows a site admin to control which requests generate tracing data.
- `"all"` will trace all requests.
Copy link
Contributor

Choose a reason for hiding this comment

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

How does this relate to the retention policy in jaeger? How long is data kept there and what is the default storage capacity in our Kubernetes deployment?

Copy link
Member Author

Choose a reason for hiding this comment

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

It has no bearing on the data retention policy in Jaeger, which must be configured in Jaeger itself. This only affects behavior of the Jaeger client. This point will be addressed elsewhere, in the Jaeger install docs we provide in the Sourcegraph docs. I've added it to the TODO in this other PR: sourcegraph/deploy-sourcegraph#559.

- `"selective"` (recommended) will trace all requests initiated from an end-user URL with `?trace=1`. Non-end-user-initiated requests can set a HTTP header `X-Sourcegraph-Should-Trace: true`. This is the recommended setting, as `"all"` can generate large amounts of tracing data that may cause network and memory resource contention in the Sourcegraph instance.
- `"none"` (default) turns off tracing.
- Jaeger is now the officially supported distributed tracer. The following is the recommended site configuration to connect Sourcegraph to a Jaeger agent (which must be deployed on the same host and listening on the default ports):

```
"observability.tracing": {
"sampling": "selective"
}
```

- The site configuration field, `useJaeger`, is deprecated in favor of `observability.tracing`.
- Support for configuring Lightstep as a distributed tracer is deprecated and will be removed in a subsequent release. Instances that use Lightstep with Sourcegraph are encouraged to migrate to Jaeger (directions for running Jaeger alongside Sourcegraph are included in the installation instructions).

### Changed

Expand Down
5 changes: 2 additions & 3 deletions cmd/frontend/backend/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package backend
import (
"context"

opentracing "github.com/opentracing/opentracing-go"

"github.com/sourcegraph/sourcegraph/cmd/frontend/db"
"github.com/sourcegraph/sourcegraph/internal/actor"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
"github.com/sourcegraph/sourcegraph/internal/vcs/git"
)

Expand All @@ -24,7 +23,7 @@ func testContext() context.Context {

ctx := context.Background()
ctx = actor.WithActor(ctx, &actor.Actor{UID: 1})
_, ctx = opentracing.StartSpanFromContext(ctx, "dummy")
_, ctx = ot.StartSpanFromContext(ctx, "dummy")

return ctx
}
4 changes: 2 additions & 2 deletions cmd/frontend/backend/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"time"

"github.com/inconshreveable/log15"
opentracing "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"

"github.com/sourcegraph/sourcegraph/internal/actor"
tracepkg "github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
)

var metricLabels = []string{"method", "success"}
Expand All @@ -38,7 +38,7 @@ func init() {
func trace(ctx context.Context, server, method string, arg interface{}, err *error) (context.Context, func()) {
requestGauge.WithLabelValues(server + "." + method).Inc()

span, ctx := opentracing.StartSpanFromContext(ctx, server+"."+method)
span, ctx := ot.StartSpanFromContext(ctx, server+"."+method)
span.SetTag("Server", server)
span.SetTag("Method", method)
span.SetTag("Argument", fmt.Sprintf("%#v", arg))
Expand Down
4 changes: 2 additions & 2 deletions cmd/frontend/graphqlbackend/codemod.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/inconshreveable/log15"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
"github.com/sourcegraph/go-diff/diff"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/search"
"github.com/sourcegraph/sourcegraph/internal/search/query"
"github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
"github.com/sourcegraph/sourcegraph/internal/vcs/git"
"golang.org/x/net/context/ctxhttp"
)
Expand Down Expand Up @@ -259,7 +259,7 @@ func callCodemodInRepo(ctx context.Context, repoRevs *search.RepositoryRevisions
}
req = req.WithContext(ctx)

req, ht := nethttp.TraceRequest(opentracing.GlobalTracer(), req,
req, ht := nethttp.TraceRequest(ot.GetTracer(ctx), req,
nethttp.OperationName("Codemod client"),
nethttp.ClientTrace(false))
defer ht.Finish()
Expand Down
4 changes: 2 additions & 2 deletions cmd/frontend/graphqlbackend/externallink/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/inconshreveable/log15"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/prometheus/client_golang/prometheus"
"github.com/sourcegraph/sourcegraph/cmd/frontend/backend"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/errcode"
"github.com/sourcegraph/sourcegraph/internal/repoupdater"
"github.com/sourcegraph/sourcegraph/internal/repoupdater/protocol"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
"github.com/sourcegraph/sourcegraph/internal/vcs/git"
)

Expand Down Expand Up @@ -104,7 +104,7 @@ func Commit(ctx context.Context, repo *types.Repo, commitID api.CommitID) (links
// It logs errors to the trace but does not return errors, because external links are not worth
// failing any request for.
func linksForRepository(ctx context.Context, repo *types.Repo) (phabRepo *types.PhabricatorRepo, link *protocol.RepoLinks, serviceType string) {
span, ctx := opentracing.StartSpanFromContext(ctx, "externallink.linksForRepository")
span, ctx := ot.StartSpanFromContext(ctx, "externallink.linksForRepository")
defer span.Finish()
span.SetTag("Repo", repo.Name)
span.SetTag("ExternalRepo", repo.ExternalRepo)
Expand Down
9 changes: 9 additions & 0 deletions cmd/frontend/graphqlbackend/graphqlbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/errcode"
sgtrace "github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
)

var graphqlFieldHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Expand Down Expand Up @@ -48,6 +49,10 @@ type prometheusTracer struct {
}

func (prometheusTracer) TraceQuery(ctx context.Context, queryString string, operationName string, variables map[string]interface{}, varTypes map[string]*introspection.Type) (context.Context, trace.TraceQueryFinishFunc) {
if !ot.ShouldTrace(ctx) {
return ctx, trace.TraceQueryFinishFunc(func([]*gqlerrors.QueryError) {})
}

traceCtx, finish := trace.OpenTracingTracer{}.TraceQuery(ctx, queryString, operationName, variables, varTypes)

// Note: We don't care about the error here, we just extract the username if
Expand Down Expand Up @@ -85,6 +90,10 @@ VARIABLES
}

func (prometheusTracer) TraceField(ctx context.Context, label, typeName, fieldName string, trivial bool, args map[string]interface{}) (context.Context, trace.TraceFieldFinishFunc) {
if !ot.ShouldTrace(ctx) {
return ctx, trace.TraceFieldFinishFunc(func(*gqlerrors.QueryError) {})
}

traceCtx, finish := trace.OpenTracingTracer{}.TraceField(ctx, label, typeName, fieldName, trivial, args)
start := time.Now()
return traceCtx, func(err *gqlerrors.QueryError) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/frontend/graphqlbackend/search_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/search"
"github.com/sourcegraph/sourcegraph/internal/search/query"
"github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
"github.com/sourcegraph/sourcegraph/internal/vcs/git"
)

Expand Down Expand Up @@ -360,7 +361,7 @@ func (sf *searchFilterResolver) Kind() string {
// blameFileMatch blames the specified file match to produce the time at which
// the first line match inside of it was authored.
func (sr *SearchResultsResolver) blameFileMatch(ctx context.Context, fm *FileMatchResolver) (t time.Time, err error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "blameFileMatch")
span, ctx := ot.StartSpanFromContext(ctx, "blameFileMatch")
defer func() {
if err != nil {
ext.Error.Set(span, true)
Expand Down
4 changes: 2 additions & 2 deletions cmd/frontend/graphqlbackend/search_symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/google/zoekt"
"github.com/inconshreveable/log15"
"github.com/neelance/parallel"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/search/query"
"github.com/sourcegraph/sourcegraph/internal/symbols/protocol"
"github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
"github.com/sourcegraph/sourcegraph/internal/vcs/git"
)

Expand Down Expand Up @@ -252,7 +252,7 @@ func symbolCount(fmrs []*FileMatchResolver) int {
}

func searchSymbolsInRepo(ctx context.Context, repoRevs *search.RepositoryRevisions, patternInfo *search.TextPatternInfo, query query.QueryInfo, limit int) (res []*FileMatchResolver, err error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "Search symbols in repo")
span, ctx := ot.StartSpanFromContext(ctx, "Search symbols in repo")
defer func() {
if err != nil {
ext.Error.Set(span, true)
Expand Down
10 changes: 5 additions & 5 deletions cmd/frontend/graphqlbackend/textsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"github.com/pkg/errors"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/metrics"
"github.com/sourcegraph/sourcegraph/internal/trace"

"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log"

"github.com/inconshreveable/log15"
Expand All @@ -32,7 +32,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/search"
"github.com/sourcegraph/sourcegraph/internal/search/query"
querytypes "github.com/sourcegraph/sourcegraph/internal/search/query/types"
"github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
"github.com/sourcegraph/sourcegraph/internal/vcs/git"
)

Expand All @@ -43,8 +43,8 @@ var (
requestCounter = metrics.NewRequestMeter("textsearch", "Total number of requests sent to the textsearch API.")

searchHTTPClient = &http.Client{
// nethttp.Transport will propagate opentracing spans
Transport: &nethttp.Transport{
// ot.Transport will propagate opentracing spans
Transport: &ot.Transport{
RoundTripper: requestCounter.Transport(&http.Transport{
// Default is 2, but we can send many concurrent requests
MaxIdleConnsPerHost: 500,
Expand Down Expand Up @@ -302,7 +302,7 @@ func textSearchURL(ctx context.Context, url string) ([]*FileMatchResolver, bool,
}
req = req.WithContext(ctx)

req, ht := nethttp.TraceRequest(opentracing.GlobalTracer(), req,
req, ht := nethttp.TraceRequest(ot.GetTracer(ctx), req,
nethttp.OperationName("Searcher Client"),
nethttp.ClientTrace(false))
defer ht.Finish()
Expand Down
7 changes: 0 additions & 7 deletions cmd/frontend/internal/app/jscontext/jscontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,6 @@ func NewJSContextFromRequest(req *http.Request) JSContext {
headers["x-sourcegraph-client"] = globals.ExternalURL().String()
headers["X-Requested-With"] = "Sourcegraph" // required for httpapi to use cookie auth

// -- currently we don't associate XHR calls with the parent page's span --
// if span := opentracing.SpanFromContext(req.Context()); span != nil {
// if err := opentracing.GlobalTracer().Inject(span.Context(), opentracing.HTTPHeaders, opentracing.TextMapCarrier(headers)); err != nil {
// return JSContext{}, err
// }
// }

// Propagate Cache-Control no-cache and max-age=0 directives
// to the requests made by our client-side JavaScript. This is
// not a perfect parser, but it catches the important cases.
Expand Down
4 changes: 2 additions & 2 deletions cmd/frontend/internal/app/ui/landing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"github.com/inconshreveable/log15"

"github.com/gorilla/mux"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/pkg/handlerutil"
"github.com/sourcegraph/sourcegraph/internal/errcode"
"github.com/sourcegraph/sourcegraph/internal/lazyregexp"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
)

var goSymbolReg = lazyregexp.New("/info/GoPackage/(.+)$")
Expand All @@ -34,7 +34,7 @@ func serveRepoLanding(w http.ResponseWriter, r *http.Request) error {
}

func serveDefLanding(w http.ResponseWriter, r *http.Request) (err error) {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "serveDefLanding")
span, ctx := ot.StartSpanFromContext(r.Context(), "serveDefLanding")
r = r.WithContext(ctx)
defer func() {
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions cmd/frontend/internal/cli/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/actor"
"github.com/sourcegraph/sourcegraph/internal/conf"
tracepkg "github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
"github.com/sourcegraph/sourcegraph/internal/version"
)

Expand Down Expand Up @@ -73,7 +74,8 @@ func newExternalHTTPHandler(schema *graphql.Schema, githubWebhook, bitbucketServ
// 🚨 SECURITY: Auth middleware that must run before other auth middlewares.
h = internalauth.OverrideAuthMiddleware(h)
h = internalauth.ForbidAllRequestsMiddleware(h)
h = tracepkg.Middleware(h)
h = tracepkg.HTTPTraceMiddleware(h)
h = ot.Middleware(h)
h = middleware.SourcegraphComGoGetHandler(h)
h = middleware.BlackHole(h)
h = secureHeadersMiddleware(h)
Expand Down Expand Up @@ -165,7 +167,7 @@ func secureHeadersMiddleware(next http.Handler) http.Handler {

if r.Method == "OPTIONS" {
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", corsAllowHeader+", X-Sourcegraph-Client, Content-Type, Authorization")
w.Header().Set("Access-Control-Allow-Headers", corsAllowHeader+", X-Sourcegraph-Client, Content-Type, Authorization, X-Sourcegraph-Should-Trace")
w.WriteHeader(http.StatusOK)
return // do not invoke next handler
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/gitserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import (
"github.com/pkg/errors"

"github.com/inconshreveable/log15"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"

"github.com/sourcegraph/sourcegraph/cmd/gitserver/server"
"github.com/sourcegraph/sourcegraph/internal/debugserver"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
"github.com/sourcegraph/sourcegraph/internal/tracer"
)

Expand Down Expand Up @@ -63,7 +62,7 @@ func main() {
}

// Create Handler now since it also initializes state
handler := nethttp.Middleware(opentracing.GlobalTracer(), gitserver.Handler())
handler := ot.Middleware(gitserver.Handler())

go debugserver.Start()

Expand Down
6 changes: 3 additions & 3 deletions cmd/gitserver/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"time"

"github.com/inconshreveable/log15"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
Expand All @@ -40,6 +39,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/mutablelimiter"
"github.com/sourcegraph/sourcegraph/internal/repotrackutil"
"github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
)

// tempDirName is the name used for the temporary directory under ReposDir.
Expand Down Expand Up @@ -80,7 +80,7 @@ func runCommand(ctx context.Context, cmd *exec.Cmd) (exitCode int, err error) {
if runCommandMock != nil {
return runCommandMock(ctx, cmd)
}
span, _ := opentracing.StartSpanFromContext(ctx, "runCommand")
span, _ := ot.StartSpanFromContext(ctx, "runCommand")
span.SetTag("path", cmd.Path)
span.SetTag("args", cmd.Args)
span.SetTag("dir", cmd.Dir)
Expand Down Expand Up @@ -1048,7 +1048,7 @@ func init() {
var headBranchPattern = lazyregexp.New(`HEAD branch: (.+?)\n`)

func (s *Server) doRepoUpdate(ctx context.Context, repo api.RepoName, url string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "Server.doRepoUpdate")
span, ctx := ot.StartSpanFromContext(ctx, "Server.doRepoUpdate")
span.SetTag("repo", repo)
span.SetTag("url", url)
defer span.Finish()
Expand Down
5 changes: 2 additions & 3 deletions cmd/replacer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import (
"time"

"github.com/inconshreveable/log15"
"github.com/opentracing-contrib/go-stdlib/nethttp"
opentracing "github.com/opentracing/opentracing-go"

"github.com/sourcegraph/sourcegraph/cmd/replacer/replace"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/debugserver"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/gitserver"
"github.com/sourcegraph/sourcegraph/internal/store"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"

"github.com/sourcegraph/sourcegraph/internal/tracer"
)
Expand Down Expand Up @@ -61,7 +60,7 @@ func main() {
Store: &store,
Log: log15.Root(),
}
handler := nethttp.Middleware(opentracing.GlobalTracer(), service)
handler := ot.Middleware(service)

host := ""
if env.InsecureDev {
Expand Down
Loading