Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concurrent map write panic in monitoring middleware #14335

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions changelogs/head.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ https://github.com/elastic/apm-server/compare/8.15\...main[View commits]
==== Bug fixes

- Track all bulk request response status codes {pull}13574[13574]
- Fix a concurrent map write panic in monitoring middleware {pull}14335[14335]

[float]
==== Breaking Changes
Expand Down
30 changes: 27 additions & 3 deletions internal/beater/middleware/monitoring_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package middleware
import (
"context"
"net/http"
"sync"
"time"

"go.opentelemetry.io/otel"
Expand All @@ -36,9 +37,13 @@ const (
type monitoringMiddleware struct {
meter metric.Meter

ints map[request.ResultID]*monitoring.Int
counters map[string]metric.Int64Counter
histograms map[string]metric.Int64Histogram
ints map[request.ResultID]*monitoring.Int

counters map[string]metric.Int64Counter
countersRWMutex sync.RWMutex

histograms map[string]metric.Int64Histogram
histogramsRWMutex sync.RWMutex
}

func (m *monitoringMiddleware) Middleware() Middleware {
Expand Down Expand Up @@ -79,21 +84,40 @@ func (m *monitoringMiddleware) inc(id request.ResultID) {

func (m *monitoringMiddleware) getCounter(n string) metric.Int64Counter {
name := "http.server." + n

m.countersRWMutex.RLock()
if met, ok := m.counters[name]; ok {
m.countersRWMutex.RUnlock()
return met
}

m.countersRWMutex.RUnlock()
m.countersRWMutex.Lock()
defer m.countersRWMutex.Unlock()
if met, ok := m.counters[name]; ok {
return met
}
nm, _ := m.meter.Int64Counter(name)
m.counters[name] = nm
return nm
}

func (m *monitoringMiddleware) getHistogram(n string, opts ...metric.Int64HistogramOption) metric.Int64Histogram {
name := "http.server." + n

m.histogramsRWMutex.RLock()
if met, ok := m.histograms[name]; ok {
m.histogramsRWMutex.RUnlock()
return met
}

m.histogramsRWMutex.RUnlock()
m.histogramsRWMutex.Lock()
defer m.histogramsRWMutex.Unlock()

if met, ok := m.histograms[name]; ok {
return met
}
nm, _ := m.meter.Int64Histogram(name, opts...)
m.histograms[name] = nm
return nm
Expand Down
Loading