Skip to content

Commit 490bd74

Browse files
mergify[bot]kruskallericywl
authored
fix: collect expvar metrics from local registry (#17594) (#17606)
monitoring.Do uses the default registry which is not used anymore propagate the local registries to correctly populate the exp var endpoint response (cherry picked from commit 0b9020f) Co-authored-by: kruskall <99559985+kruskall@users.noreply.github.com> Co-authored-by: Eric <22215921+ericywl@users.noreply.github.com>
1 parent eca95bc commit 490bd74

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
@@ -460,6 +460,7 @@ func (s *Runner) Run(ctx context.Context) error {
460460
NewElasticsearchClient: newElasticsearchClient,
461461
GRPCServer: grpcServer,
462462
Semaphore: semaphore.NewWeighted(int64(s.config.MaxConcurrentDecoders)),
463+
BeatMonitoring: s.beatMonitoring,
463464
}
464465
if s.wrapServer != nil {
465466
// 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

@@ -147,6 +148,9 @@ type ServerParams struct {
147148
// Semaphore holds a shared semaphore used to limit the number of
148149
// concurrently running requests
149150
Semaphore input.Semaphore
151+
152+
// BeatMonitoring holds the beat monitoring registries
153+
BeatMonitoring beat.Monitoring
150154
}
151155

152156
// newBaseRunServer returns the base RunServerFunc.
@@ -191,6 +195,7 @@ func newServer(args ServerParams, listener net.Listener) (server, error) {
191195
args.MeterProvider,
192196
args.TracerProvider,
193197
args.Logger,
198+
args.BeatMonitoring.StatsRegistry(),
194199
)
195200
if err != nil {
196201
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"
@@ -58,6 +59,7 @@ func newTracerServer(cfg *config.Config, listener net.Listener, logger *logp.Log
5859
noopmetric.NewMeterProvider(),
5960
nooptrace.NewTracerProvider(),
6061
logger,
62+
monitoring.NewRegistry(), // unused
6163
)
6264
if err != nil {
6365
return nil, err

0 commit comments

Comments
 (0)