@@ -29,11 +29,11 @@ import (
2929
3030 "github.com/pkg/errors"
3131 "go.elastic.co/apm/v2"
32+ "go.opentelemetry.io/otel/metric"
3233
3334 "github.com/elastic/apm-server/internal/elasticsearch"
3435 "github.com/elastic/apm-server/internal/logs"
3536 "github.com/elastic/elastic-agent-libs/logp"
36- "github.com/elastic/elastic-agent-libs/monitoring"
3737 "github.com/elastic/go-elasticsearch/v8/esapi"
3838)
3939
@@ -71,22 +71,34 @@ type ElasticsearchFetcher struct {
7171
7272 logger , rateLimitedLogger * logp.Logger
7373
74- tracer * apm.Tracer
75- metrics fetcherMetrics
76- }
74+ tracer * apm.Tracer
7775
78- type fetcherMetrics struct {
79- fetchES , fetchFallback , fetchFallbackUnavailable , fetchInvalid ,
80- cacheRefreshSuccesses , cacheRefreshFailures ,
81- cacheEntriesCount atomic.Int64
76+ esCacheEntriesCount metric.Int64Gauge
77+ esFetchCount metric.Int64Counter
78+ esFetchFallbackCount metric.Int64Counter
79+ esFetchUnavailableCount metric.Int64Counter
80+ esFetchInvalidCount metric.Int64Counter
81+ esCacheRefreshSuccesses metric.Int64Counter
82+ esCacheRefreshFailures metric.Int64Counter
8283}
8384
8485func NewElasticsearchFetcher (
8586 client * elasticsearch.Client ,
8687 cacheDuration time.Duration ,
8788 fetcher Fetcher ,
8889 tracer * apm.Tracer ,
90+ mp metric.MeterProvider ,
8991) * ElasticsearchFetcher {
92+ meter := mp .Meter ("github.com/elastic/apm-server/internal/agentcfg" )
93+
94+ esCacheEntriesCount , _ := meter .Int64Gauge ("apm-server.agentcfg.elasticsearch.cache.entries.count" )
95+ esFetchCount , _ := meter .Int64Counter ("apm-server.agentcfg.elasticsearch.fetch.es" )
96+ esFetchFallbackCount , _ := meter .Int64Counter ("apm-server.agentcfg.elasticsearch.fetch.fallback" )
97+ esFetchUnavailableCount , _ := meter .Int64Counter ("apm-server.agentcfg.elasticsearch.fetch.unavailable" )
98+ esFetchInvalidCount , _ := meter .Int64Counter ("apm-server.agentcfg.elasticsearch.fetch.invalid" )
99+ esCacheRefreshSuccesses , _ := meter .Int64Counter ("apm-server.agentcfg.elasticsearch.cache.refresh.successes" )
100+ esCacheRefreshFailures , _ := meter .Int64Counter ("apm-server.agentcfg.elasticsearch.cache.refresh.failures" )
101+
90102 logger := logp .NewLogger ("agentcfg" )
91103 return & ElasticsearchFetcher {
92104 client : client ,
@@ -96,6 +108,14 @@ func NewElasticsearchFetcher(
96108 logger : logger ,
97109 rateLimitedLogger : logger .WithOptions (logs .WithRateLimit (loggerRateLimit )),
98110 tracer : tracer ,
111+
112+ esCacheEntriesCount : esCacheEntriesCount ,
113+ esFetchCount : esFetchCount ,
114+ esFetchFallbackCount : esFetchFallbackCount ,
115+ esFetchUnavailableCount : esFetchUnavailableCount ,
116+ esFetchInvalidCount : esFetchInvalidCount ,
117+ esCacheRefreshSuccesses : esCacheRefreshSuccesses ,
118+ esCacheRefreshFailures : esCacheRefreshFailures ,
99119 }
100120}
101121
@@ -105,22 +125,22 @@ func (f *ElasticsearchFetcher) Fetch(ctx context.Context, query Query) (Result,
105125 // Happy path: serve fetch requests using an initialized cache.
106126 f .mu .RLock ()
107127 defer f .mu .RUnlock ()
108- f .metrics . fetchES . Add (1 )
128+ f .esFetchCount . Add (ctx , 1 )
109129 return matchAgentConfig (query , f .cache ), nil
110130 }
111131
112132 if f .fallbackFetcher != nil {
113- f .metrics . fetchFallback . Add (1 )
133+ f .esFetchFallbackCount . Add (ctx , 1 )
114134 return f .fallbackFetcher .Fetch (ctx , query )
115135 }
116136
117137 if f .invalidESCfg .Load () {
118- f .metrics . fetchInvalid . Add (1 )
138+ f .esFetchInvalidCount . Add (ctx , 1 )
119139 f .rateLimitedLogger .Errorf ("rejecting fetch request: no valid elasticsearch config" )
120140 return Result {}, errors .New (ErrNoValidElasticsearchConfig )
121141 }
122142
123- f .metrics . fetchFallbackUnavailable . Add (1 )
143+ f .esFetchUnavailableCount . Add (ctx , 1 )
124144 f .rateLimitedLogger .Warnf ("rejecting fetch request: infrastructure is not ready" )
125145 return Result {}, errors .New (ErrInfrastructureNotReady )
126146}
@@ -213,9 +233,9 @@ func (f *ElasticsearchFetcher) refreshCache(ctx context.Context) (err error) {
213233
214234 defer func () {
215235 if err != nil {
216- f .metrics . cacheRefreshFailures . Add (1 )
236+ f .esCacheRefreshFailures . Add (ctx , 1 )
217237 } else {
218- f .metrics . cacheRefreshSuccesses . Add (1 )
238+ f .esCacheRefreshSuccesses . Add (ctx , 1 )
219239 }
220240 }()
221241
@@ -247,7 +267,7 @@ func (f *ElasticsearchFetcher) refreshCache(ctx context.Context) (err error) {
247267 f .cache = buffer
248268 f .mu .Unlock ()
249269 f .cacheInitialized .Store (true )
250- f .metrics . cacheEntriesCount . Store ( int64 (len (f .cache )))
270+ f .esCacheEntriesCount . Record ( ctx , int64 (len (f .cache )))
251271 f .last = time .Now ()
252272 return nil
253273}
@@ -304,20 +324,3 @@ func (f *ElasticsearchFetcher) singlePageRefresh(ctx context.Context, scrollID s
304324 }
305325 return result , json .NewDecoder (resp .Body ).Decode (& result )
306326}
307-
308- // CollectMonitoring may be called to collect monitoring metrics from the
309- // fetcher. It is intended to be used with libbeat/monitoring.NewFunc.
310- //
311- // The metrics should be added to the "apm-server.agentcfg.elasticsearch" registry.
312- func (f * ElasticsearchFetcher ) CollectMonitoring (_ monitoring.Mode , V monitoring.Visitor ) {
313- V .OnRegistryStart ()
314- defer V .OnRegistryFinished ()
315-
316- monitoring .ReportInt (V , "cache.entries.count" , f .metrics .cacheEntriesCount .Load ())
317- monitoring .ReportInt (V , "fetch.es" , f .metrics .fetchES .Load ())
318- monitoring .ReportInt (V , "fetch.fallback" , f .metrics .fetchFallback .Load ())
319- monitoring .ReportInt (V , "fetch.unavailable" , f .metrics .fetchFallbackUnavailable .Load ())
320- monitoring .ReportInt (V , "fetch.invalid" , f .metrics .fetchInvalid .Load ())
321- monitoring .ReportInt (V , "cache.refresh.successes" , f .metrics .cacheRefreshSuccesses .Load ())
322- monitoring .ReportInt (V , "cache.refresh.failures" , f .metrics .cacheRefreshFailures .Load ())
323- }
0 commit comments