Skip to content

Commit 1fd473a

Browse files
authored
api: support for trace flag (#253)
When the trace flag is set we print to stderr the trace ID for the request.
1 parent 83e3117 commit 1fd473a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

cmd/src/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"flag"
88
"fmt"
99
"io/ioutil"
10+
"log"
1011
"net/http"
1112
"os"
1213
"strings"
@@ -208,6 +209,9 @@ func (a *apiRequest) do() error {
208209
if cfg.AccessToken != "" {
209210
req.Header.Set("Authorization", "token "+cfg.AccessToken)
210211
}
212+
if *a.flags.trace {
213+
req.Header.Set("X-Sourcegraph-Should-Trace", "true")
214+
}
211215
for k, v := range cfg.AdditionalHeaders {
212216
req.Header.Set(k, v)
213217
}
@@ -220,6 +224,11 @@ func (a *apiRequest) do() error {
220224
}
221225
defer resp.Body.Close()
222226

227+
// Check trace header before we potentially early exit
228+
if *a.flags.trace {
229+
log.Printf("x-trace: %s", resp.Header.Get("x-trace"))
230+
}
231+
223232
// Our request may have failed before the reaching GraphQL endpoint, so
224233
// confirm the status code. You can test this easily with e.g. an invalid
225234
// endpoint like -endpoint=https://google.com
@@ -276,13 +285,15 @@ func (a *apiRequest) do() error {
276285
// API requests. e.g. the ability to turn any CLI command into a curl command.
277286
type apiFlags struct {
278287
getCurl *bool
288+
trace *bool
279289
}
280290

281291
// newAPIFlags creates the API flags. It should be invoked once at flag setup
282292
// time.
283293
func newAPIFlags(flagSet *flag.FlagSet) *apiFlags {
284294
return &apiFlags{
285295
getCurl: flagSet.Bool("get-curl", false, "Print the curl command for executing this query and exit (WARNING: includes printing your access token!)"),
296+
trace: flagSet.Bool("trace", false, "Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracing"),
286297
}
287298
}
288299

0 commit comments

Comments
 (0)