Skip to content

Commit f0a0d35

Browse files
DavidS-ovmactions-user
authored andcommitted
Merge pull request #791 from overmindtech/tracing
Fix the EC2 detector avoidance in the monorepo GitOrigin-RevId: 64d6d758071d9c7343c1d2557c780ed171cff7a6
1 parent 3995f7d commit f0a0d35

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

tracing/main.go

+33-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"path/filepath"
78
"slices"
89
"time"
910

@@ -56,14 +57,44 @@ func Tracer() trace.Tracer {
5657
return tracer
5758
}
5859

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+
5991
func tracingResource(component string) *resource.Resource {
6092
// Identify your application using resource detection
6193
resources := []*resource.Resource{}
6294

6395
// the EC2 detector takes ~10s to time out outside EC2
6496
// disable it if we're running from a git checkout
65-
_, err := os.Stat(".git")
66-
if os.IsNotExist(err) {
97+
if !hasGitDir() {
6798
ec2Res, err := resource.New(context.Background(), resource.WithDetectors(ec2.NewResourceDetector()))
6899
if err != nil {
69100
log.WithError(err).Error("error initialising EC2 resource detector")
@@ -180,7 +211,6 @@ func InitTracerWithUpstreams(component, honeycombApiKey, sentryDSN string, opts
180211
}
181212

182213
func InitTracer(component string, opts ...otlptracehttp.Option) error {
183-
184214
client := otlptracehttp.NewClient(opts...)
185215
otlpExp, err := otlptrace.New(context.Background(), client)
186216
if err != nil {

0 commit comments

Comments
 (0)