Skip to content

Commit 0b9020f

Browse files
kruskallericywl
andauthored
fix: collect expvar metrics from local registry (elastic#17594)
monitoring.Do uses the default registry which is not used anymore propagate the local registries to correctly populate the exp var endpoint response Co-authored-by: Eric <[email protected]>
1 parent c5af4d6 commit 0b9020f

File tree

7 files changed

+36
-20
lines changed

7 files changed

+36
-20
lines changed

internal/beater/api/expvar.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ import (
3030
// TODO(axw) this is copied from libbeat/service. We should move the
3131
// handler to libbeat/monitoring, and export it for libbeat/service and
3232
// apm-server to use.
33-
func debugVarsHandler(w http.ResponseWriter, r *http.Request) {
34-
w.Header().Set("Content-Type", "application/json; charset=utf-8")
33+
func debugVarsHandler(statsRegistry *monitoring.Registry) http.HandlerFunc {
34+
return func(w http.ResponseWriter, r *http.Request) {
35+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
3536

36-
first := true
37-
report := func(key string, value interface{}) {
38-
if !first {
39-
fmt.Fprintf(w, ",\n")
37+
first := true
38+
report := func(key string, value interface{}) {
39+
if !first {
40+
fmt.Fprintf(w, ",\n")
41+
}
42+
first = false
43+
if str, ok := value.(string); ok {
44+
fmt.Fprintf(w, "%q: %q", key, str)
45+
} else {
46+
fmt.Fprintf(w, "%q: %v", key, value)
47+
}
4048
}
41-
first = false
42-
if str, ok := value.(string); ok {
43-
fmt.Fprintf(w, "%q: %q", key, str)
44-
} else {
45-
fmt.Fprintf(w, "%q: %v", key, value)
46-
}
47-
}
4849

49-
fmt.Fprintf(w, "{\n")
50-
monitoring.Do(monitoring.Full, report)
51-
expvar.Do(func(kv expvar.KeyValue) {
52-
report(kv.Key, kv.Value)
53-
})
54-
fmt.Fprintf(w, "\n}\n")
50+
fmt.Fprintf(w, "{\n")
51+
statsRegistry.Do(monitoring.Full, report)
52+
expvar.Do(func(kv expvar.KeyValue) {
53+
report(kv.Key, kv.Value)
54+
})
55+
fmt.Fprintf(w, "\n}\n")
56+
}
5557
}

internal/beater/api/mux.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"go.uber.org/zap"
3131

3232
"github.com/elastic/elastic-agent-libs/logp"
33+
"github.com/elastic/elastic-agent-libs/monitoring"
3334

3435
"github.com/elastic/apm-data/input"
3536
"github.com/elastic/apm-data/input/elasticapm"
@@ -93,6 +94,7 @@ func NewMux(
9394
meterProvider metric.MeterProvider,
9495
traceProvider trace.TracerProvider,
9596
logger *logp.Logger,
97+
statsRegistry *monitoring.Registry,
9698
) (*mux.Router, error) {
9799
pool := request.NewContextPool()
98100
logger = logger.Named(logs.Handler)
@@ -147,7 +149,7 @@ func NewMux(
147149
if beaterConfig.Expvar.Enabled {
148150
path := beaterConfig.Expvar.URL
149151
logger.Infof("Path %s added to request handler", path)
150-
router.Handle(path, http.HandlerFunc(debugVarsHandler))
152+
router.Handle(path, debugVarsHandler(statsRegistry))
151153
}
152154
if beaterConfig.Pprof.Enabled {
153155
const path = "/debug/pprof"

internal/beater/api/mux_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"github.com/elastic/apm-server/internal/sourcemap"
4545
"github.com/elastic/elastic-agent-libs/logp"
4646
"github.com/elastic/elastic-agent-libs/logp/logptest"
47+
"github.com/elastic/elastic-agent-libs/monitoring"
4748
)
4849

4950
func TestBackendRequestMetadata(t *testing.T) {
@@ -185,6 +186,7 @@ func (m muxBuilder) build(cfg *config.Config) (sdkmetric.Reader, http.Handler, e
185186
mp,
186187
noop.NewTracerProvider(),
187188
m.Logger,
189+
monitoring.NewRegistry(),
188190
)
189191
return reader, r, err
190192
}

internal/beater/beater.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ func (s *Runner) Run(ctx context.Context) error {
469469
NewElasticsearchClient: newElasticsearchClient,
470470
GRPCServer: grpcServer,
471471
Semaphore: semaphore.NewWeighted(int64(s.config.MaxConcurrentDecoders)),
472+
BeatMonitoring: s.beatMonitoring,
472473
}
473474
if s.wrapServer != nil {
474475
// Wrap the serverParams and runServer function, enabling

internal/beater/otlp/http_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"github.com/elastic/apm-server/internal/beater/monitoringtest"
4747
"github.com/elastic/apm-server/internal/beater/ratelimit"
4848
"github.com/elastic/elastic-agent-libs/logp/logptest"
49+
"github.com/elastic/elastic-agent-libs/monitoring"
4950
)
5051

5152
func TestConsumeTracesHTTP(t *testing.T) {
@@ -183,6 +184,7 @@ func newHTTPServer(t *testing.T, batchProcessor modelpb.BatchProcessor) (string,
183184
mp,
184185
noop.NewTracerProvider(),
185186
logptest.NewTestingLogger(t, ""),
187+
monitoring.NewRegistry(),
186188
)
187189
require.NoError(t, err)
188190
srv := http.Server{Handler: router}

internal/beater/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"golang.org/x/sync/errgroup"
3131
"google.golang.org/grpc"
3232

33+
"github.com/elastic/beats/v7/libbeat/beat"
3334
"github.com/elastic/beats/v7/libbeat/version"
3435
"github.com/elastic/elastic-agent-libs/logp"
3536

@@ -142,6 +143,9 @@ type ServerParams struct {
142143
// Semaphore holds a shared semaphore used to limit the number of
143144
// concurrently running requests
144145
Semaphore input.Semaphore
146+
147+
// BeatMonitoring holds the beat monitoring registries
148+
BeatMonitoring beat.Monitoring
145149
}
146150

147151
// newBaseRunServer returns the base RunServerFunc.
@@ -186,6 +190,7 @@ func newServer(args ServerParams, listener net.Listener) (server, error) {
186190
args.MeterProvider,
187191
args.TracerProvider,
188192
args.Logger,
193+
args.BeatMonitoring.StatsRegistry(),
189194
)
190195
if err != nil {
191196
return server{}, err

internal/beater/tracing.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
nooptrace "go.opentelemetry.io/otel/trace/noop"
2727

2828
"github.com/elastic/elastic-agent-libs/logp"
29+
"github.com/elastic/elastic-agent-libs/monitoring"
2930

3031
"github.com/elastic/apm-data/input"
3132
"github.com/elastic/apm-data/model/modelpb"
@@ -57,6 +58,7 @@ func newTracerServer(cfg *config.Config, listener net.Listener, logger *logp.Log
5758
noopmetric.NewMeterProvider(),
5859
nooptrace.NewTracerProvider(),
5960
logger,
61+
monitoring.NewRegistry(), // unused
6062
)
6163
if err != nil {
6264
return nil, err

0 commit comments

Comments
 (0)