Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ func sendEvent(event *EventData) {

// we do fire & forget here, because the whole pid dance isn't necessary to send events
go func() {
_ = sensor.Agent().SendEvent(event)
_ = safeSensor().Agent().SendEvent(event)
}()
}
16 changes: 13 additions & 3 deletions lambda_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type lambdaAgent struct {

snapshot serverlessSnapshot

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

client *http.Client
Expand Down Expand Up @@ -144,7 +144,11 @@ func (a *lambdaAgent) enqueueSpans(spans []Span) {
}

func (a *lambdaAgent) sendRequest(req *http.Request) error {
req.Header.Set("X-Instana-Host", a.snapshot.Host)
a.mu.RLock()
host := a.snapshot.Host
a.mu.RUnlock()

req.Header.Set("X-Instana-Host", host)
req.Header.Set("X-Instana-Key", a.Key)
req.Header.Set("X-Instana-Time", strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10))

Expand Down Expand Up @@ -172,7 +176,11 @@ func (a *lambdaAgent) sendRequest(req *http.Request) error {
}

func (a *lambdaAgent) collectSnapshot(spans []Span) serverlessSnapshot {
if a.snapshot.EntityID != "" {
a.mu.RLock()
entityID := a.snapshot.EntityID
a.mu.RUnlock()

if entityID != "" {
return a.snapshot
}

Expand All @@ -184,10 +192,12 @@ func (a *lambdaAgent) collectSnapshot(spans []Span) serverlessSnapshot {
continue
}

a.mu.Lock()
a.snapshot = serverlessSnapshot{
EntityID: sp.Snapshot.ARN,
Host: sp.Snapshot.ARN,
}
a.mu.Unlock()
a.logger.Debug("collected snapshot")

break
Expand Down
18 changes: 10 additions & 8 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"strings"
"sync"
"sync/atomic"
)

// Valid log levels to be used with (*logger.Logger).SetLevel()
Expand Down Expand Up @@ -58,7 +59,7 @@ type Logger struct {
p Printer

mu sync.Mutex
lvl Level
lvl atomic.Uint32
prefix string
}

Expand Down Expand Up @@ -124,10 +125,11 @@ func (l *Logger) SetLevel(level Level) {
level = DebugLevel
}

l.mu.Lock()
defer l.mu.Unlock()
l.lvl.Store(uint32(level))
}

l.lvl = level
func (l *Logger) GetLevel() Level {
return Level(l.lvl.Load())
}

// SetPrefix sets the label that will be used as a prefix for each log line
Expand All @@ -140,7 +142,7 @@ func (l *Logger) SetPrefix(prefix string) {

// Debug appends a debug message to the log
func (l *Logger) Debug(v ...interface{}) {
if l.lvl < DebugLevel {
if l.GetLevel() < DebugLevel {
return
}

Expand All @@ -149,7 +151,7 @@ func (l *Logger) Debug(v ...interface{}) {

// Info appends an info message to the log
func (l *Logger) Info(v ...interface{}) {
if l.lvl < InfoLevel {
if l.GetLevel() < InfoLevel {
return
}

Expand All @@ -158,7 +160,7 @@ func (l *Logger) Info(v ...interface{}) {

// Warn appends a warning message to the log
func (l *Logger) Warn(v ...interface{}) {
if l.lvl < WarnLevel {
if l.GetLevel() < WarnLevel {
return
}

Expand All @@ -167,7 +169,7 @@ func (l *Logger) Warn(v ...interface{}) {

// Error appends an error message to the log
func (l *Logger) Error(v ...interface{}) {
if l.lvl < ErrorLevel {
if l.GetLevel() < ErrorLevel {
return
}

Expand Down
4 changes: 2 additions & 2 deletions meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func (m *meterS) Run(collectInterval time.Duration) {
case <-m.done:
return
case <-ticker.C:
if sensor.Agent().Ready() {
if isAgentReady() {
go func() {
_ = sensor.Agent().SendMetrics(m.collectMetrics())
_ = safeSensor().Agent().SendMetrics(m.collectMetrics())
}()
}
}
Expand Down
3 changes: 2 additions & 1 deletion recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func NewRecorder() *Recorder {
ticker := time.NewTicker(1 * time.Second)
go func() {
for range ticker.C {
if sensor.Agent().Ready() {

if isAgentReady() {
go func() {
if err := r.Flush(context.Background()); err != nil {
sensor.logger.Error("failed to flush the spans: ", err.Error())
Expand Down
18 changes: 17 additions & 1 deletion sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type sensorS struct {

var (
sensor *sensorS
muSensor sync.Mutex
muSensor sync.RWMutex
binaryName = filepath.Base(os.Args[0])
processStartedAt = time.Now()
c TracerLogger
Expand Down Expand Up @@ -157,6 +157,15 @@ func newSensor(options *Options) *sensorS {
return s
}

// safeSensor safely returns the global sensor instance for concurrent access.
// It acquires a read lock and should only be used for read operations.
// Since the sensor is immutable after initialization, this provides sufficient protection against data races.
func safeSensor() *sensorS {
muSensor.RLock()
defer muSensor.RUnlock()
return sensor
}

func (r *sensorS) setLogger(l LeveledLogger) {
r.logger = l

Expand Down Expand Up @@ -286,6 +295,13 @@ func ShutdownSensor() {
}
}

func isAgentReady() bool {
muSensor.RLock()
defer muSensor.RUnlock()

return sensor.Agent().Ready()
}

// ShutdownCollector cleans up the collector and sensor reference.
// It will also reset the singleton as the next time that instana.InitCollector API is called,
// collector and sensor will be reinitialized.
Expand Down
Loading