| 1 |
package gcp |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"log/slog" |
| 8 |
"net" |
| 9 |
"net/http" |
| 10 |
"os" |
| 11 |
"runtime" |
| 12 |
"strings" |
| 13 |
"sync" |
| 14 |
"time" |
| 15 |
) |
| 16 |
|
| 17 |
func insideTest() bool { |
| 18 |
|
| 19 |
pc := make([]uintptr, 10) |
| 20 |
n := runtime.Callers(0, pc) |
| 21 |
if n == 0 { |
| 22 |
slog.Debug("WithCloudTraceContext: no PCs available") |
| 23 |
return true |
| 24 |
} |
| 25 |
frames := runtime.CallersFrames(pc[:n]) |
| 26 |
for { |
| 27 |
frame, more := frames.Next() |
| 28 |
if !more { |
| 29 |
break |
| 30 |
} |
| 31 |
if strings.HasPrefix(frame.Function, "testing.") && |
| 32 |
strings.HasSuffix(frame.File, "src/testing/testing.go") { |
| 33 |
slog.Debug("WithCloudTraceContext: inside test", "function", frame.Function, "file", frame.File, "line", frame.Line) |
| 34 |
return true |
| 35 |
} |
| 36 |
} |
| 37 |
return false |
| 38 |
} |
| 39 |
|
| 40 |
var ( |
| 41 |
projectID string |
| 42 |
lookupOnce sync.Once |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
func WithCloudTraceContext(h http.Handler) http.Handler { |
| 49 |
|
| 50 |
fromEnv := os.Getenv("GOOGLE_CLOUD_PROJECT") |
| 51 |
if fromEnv != "" { |
| 52 |
projectID = fromEnv |
| 53 |
} else { |
| 54 |
lookupOnce.Do(func() { |
| 55 |
if insideTest() { |
| 56 |
slog.Debug("WithCloudTraceContext: inside test, not looking up project ID") |
| 57 |
return |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
host := "169.254.169.254" |
| 63 |
if h := os.Getenv("GCE_METADATA_HOST"); h != "" { |
| 64 |
host = h |
| 65 |
} |
| 66 |
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s/computeMetadata/v1/project/project-id", host), nil) |
| 67 |
if err != nil { |
| 68 |
slog.Debug("WithCloudTraceContext: could not get GCP project ID from metadata server", "err", err) |
| 69 |
return |
| 70 |
} |
| 71 |
req.Header.Set("Metadata-Flavor", "Google") |
| 72 |
resp, err := (&http.Client{ |
| 73 |
Transport: &http.Transport{ |
| 74 |
Dial: (&net.Dialer{Timeout: 2 * time.Second}).Dial, |
| 75 |
}, |
| 76 |
Timeout: 5 * time.Second, |
| 77 |
}).Do(req) |
| 78 |
if err != nil { |
| 79 |
slog.Debug("WithCloudTraceContext: could not get GCP project ID from metadata server", "err", err) |
| 80 |
return |
| 81 |
} |
| 82 |
if resp.StatusCode != http.StatusOK { |
| 83 |
slog.Debug("WithCloudTraceContext: could not get GCP project ID from metadata server", "code", resp.StatusCode, "status", resp.Status) |
| 84 |
return |
| 85 |
} |
| 86 |
defer resp.Body.Close() |
| 87 |
all, err := io.ReadAll(resp.Body) |
| 88 |
if err != nil { |
| 89 |
slog.Debug("WithCloudTraceContext: could not get GCP project ID from metadata server", "err", err) |
| 90 |
return |
| 91 |
} |
| 92 |
projectID = string(all) |
| 93 |
}) |
| 94 |
} |
| 95 |
|
| 96 |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 97 |
if projectID != "" { |
| 98 |
var trace string |
| 99 |
traceHeader := r.Header.Get("traceparent") |
| 100 |
traceID := parseTraceFromW3CHeader(traceHeader) |
| 101 |
if traceID != "" { |
| 102 |
trace = fmt.Sprintf("projects/%s/traces/%s", projectID, traceID) |
| 103 |
} |
| 104 |
r = r.WithContext(WithTrace(r.Context(), trace)) |
| 105 |
} |
| 106 |
h.ServeHTTP(w, r) |
| 107 |
}) |
| 108 |
} |
| 109 |
|
| 110 |
type traceKey struct{} |
| 111 |
|
| 112 |
|
| 113 |
func WithTrace(ctx context.Context, trace string) context.Context { |
| 114 |
if trace == "" { |
| 115 |
return ctx |
| 116 |
} |
| 117 |
return context.WithValue(ctx, traceKey{}, trace) |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func TraceFromContext(ctx context.Context) string { |
| 122 |
trace := ctx.Value(traceKey{}) |
| 123 |
if trace == nil { |
| 124 |
return "" |
| 125 |
} |
| 126 |
return trace.(string) |
| 127 |
} |
| 128 |
|
| 129 |
func parseTraceFromW3CHeader(traceparent string) string { |
| 130 |
traceParts := strings.Split(traceparent, "-") |
| 131 |
if len(traceParts) > 1 { |
| 132 |
return traceParts[1] |
| 133 |
} |
| 134 |
return "" |
| 135 |
} |