Skip to content
Open
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
22 changes: 16 additions & 6 deletions gcr_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type gcrAgent struct {
snapshot gcrSnapshot
lastProcessStats processStats

mu sync.Mutex
mu sync.RWMutex
spanQueue []Span

runtimeSnapshot *SnapshotCollector
Expand Down Expand Up @@ -133,20 +133,20 @@ func newGCRAgent(
logger: logger,
}

go func() {
go func(a *gcrAgent) {
for {
for i := 0; i < maximumRetries; i++ {
snapshot, ok := agent.collectSnapshot(context.Background())
snapshot, ok := a.collectSnapshot(context.Background())
if ok {
agent.snapshot = snapshot
a.snapshot = snapshot
break
}

time.Sleep(expDelay(i + 1))
}
time.Sleep(snapshotCollectionInterval)
}
}()
}(agent)
go agent.processStats.Run(context.Background(), time.Second)

return agent
Expand Down Expand Up @@ -208,7 +208,11 @@ func (a *gcrAgent) SendMetrics(data acceptor.Metrics) (err error) {
func (a *gcrAgent) SendEvent(event *EventData) error { return nil }

func (a *gcrAgent) SendSpans(spans []Span) error {
from := newServerlessAgentFromS(a.snapshot.Service.EntityID, "gcp")
a.mu.RLock()
entityID := a.snapshot.Service.EntityID
a.mu.RUnlock()

from := newServerlessAgentFromS(entityID, "gcp")
for i := range spans {
spans[i].From = from
}
Expand Down Expand Up @@ -286,6 +290,9 @@ func (a *gcrAgent) collectSnapshot(ctx context.Context) (gcrSnapshot, bool) {
return gcrSnapshot{}, false
}

a.mu.RLock()
defer a.mu.RUnlock()

snapshot := newGCRSnapshot(a.PID, gcrMetadata{
ComputeMetadata: md,
Service: os.Getenv(kService),
Expand All @@ -294,6 +301,9 @@ func (a *gcrAgent) collectSnapshot(ctx context.Context) (gcrSnapshot, bool) {
Port: os.Getenv("PORT"),
})
snapshot.Service.Zone = a.Zone
// Tags remains unchanged after initialization.
// An RLock alone is not enough if Tags ever becomes mutable.
// Update the locking strategy if this behavior changes.
snapshot.Service.Tags = a.Tags

a.logger.Debug("collected snapshot")
Expand Down