|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "os"
|
| 7 | + "path/filepath" |
7 | 8 | "slices"
|
8 | 9 | "time"
|
9 | 10 |
|
@@ -56,14 +57,44 @@ func Tracer() trace.Tracer {
|
56 | 57 | return tracer
|
57 | 58 | }
|
58 | 59 |
|
| 60 | +// hasGitDir returns true if the current directory or any parent directory contains a .git directory |
| 61 | +func hasGitDir() bool { |
| 62 | + // Start with the current working directory |
| 63 | + dir, err := os.Getwd() |
| 64 | + if err != nil { |
| 65 | + return false |
| 66 | + } |
| 67 | + |
| 68 | + // Check the current directory and all parent directories |
| 69 | + for { |
| 70 | + // Check if .git exists in this directory |
| 71 | + _, err := os.Stat(filepath.Join(dir, ".git")) |
| 72 | + if err == nil { |
| 73 | + return true // Found a .git directory |
| 74 | + } |
| 75 | + |
| 76 | + // Get the parent directory |
| 77 | + parentDir := filepath.Dir(dir) |
| 78 | + |
| 79 | + // If we've reached the root directory, stop searching |
| 80 | + if parentDir == dir { |
| 81 | + break |
| 82 | + } |
| 83 | + |
| 84 | + // Move up to the parent directory |
| 85 | + dir = parentDir |
| 86 | + } |
| 87 | + |
| 88 | + return false // No .git directory found |
| 89 | +} |
| 90 | + |
59 | 91 | func tracingResource(component string) *resource.Resource {
|
60 | 92 | // Identify your application using resource detection
|
61 | 93 | resources := []*resource.Resource{}
|
62 | 94 |
|
63 | 95 | // the EC2 detector takes ~10s to time out outside EC2
|
64 | 96 | // disable it if we're running from a git checkout
|
65 |
| - _, err := os.Stat(".git") |
66 |
| - if os.IsNotExist(err) { |
| 97 | + if !hasGitDir() { |
67 | 98 | ec2Res, err := resource.New(context.Background(), resource.WithDetectors(ec2.NewResourceDetector()))
|
68 | 99 | if err != nil {
|
69 | 100 | log.WithError(err).Error("error initialising EC2 resource detector")
|
@@ -180,7 +211,6 @@ func InitTracerWithUpstreams(component, honeycombApiKey, sentryDSN string, opts
|
180 | 211 | }
|
181 | 212 |
|
182 | 213 | func InitTracer(component string, opts ...otlptracehttp.Option) error {
|
183 |
| - |
184 | 214 | client := otlptracehttp.NewClient(opts...)
|
185 | 215 | otlpExp, err := otlptrace.New(context.Background(), client)
|
186 | 216 | if err != nil {
|
|
0 commit comments