-
Notifications
You must be signed in to change notification settings - Fork 524
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
Expose HTTP/gRPC metrics to OpenTelemetry as well #11564
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f471f91
add otel metrics to HTTP middleware
dmathieu 7ba7d48
add otel metrics to gRPC interceptor
dmathieu c20602b
add server to metrics names
dmathieu 5c20dd8
remove unnecessary assignment
dmathieu 0e36ccc
remove metrics that doesn't appear to be used
dmathieu b4f8db1
Merge branch 'main' into middleware-otel-metrics
dmathieu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,36 +18,79 @@ | |
package middleware | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/elastic/elastic-agent-libs/monitoring" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/metric" | ||
|
||
"github.com/elastic/apm-server/internal/beater/request" | ||
"github.com/elastic/elastic-agent-libs/monitoring" | ||
) | ||
|
||
// MonitoringMiddleware returns a middleware that increases monitoring counters for collecting metrics | ||
// about request processing. As input parameter it takes a map capable of mapping a request.ResultID to a counter. | ||
func MonitoringMiddleware(m map[request.ResultID]*monitoring.Int) Middleware { | ||
type monitoringMiddleware struct { | ||
meter metric.Meter | ||
|
||
ints map[request.ResultID]*monitoring.Int | ||
counters map[request.ResultID]metric.Int64Counter | ||
} | ||
|
||
func (m *monitoringMiddleware) Middleware() Middleware { | ||
return func(h request.Handler) (request.Handler, error) { | ||
inc := func(id request.ResultID) { | ||
if counter, ok := m[id]; ok { | ||
counter.Inc() | ||
} | ||
} | ||
return func(c *request.Context) { | ||
inc(request.IDRequestCount) | ||
ctx := context.Background() | ||
|
||
m.getMetric(request.IDRequestCount).Add(ctx, 1) | ||
m.inc(request.IDRequestCount) | ||
|
||
h(c) | ||
|
||
inc(request.IDResponseCount) | ||
m.getMetric(request.IDResponseCount).Add(ctx, 1) | ||
m.inc(request.IDResponseCount) | ||
if c.Result.StatusCode >= http.StatusBadRequest { | ||
inc(request.IDResponseErrorsCount) | ||
m.getMetric(request.IDResponseErrorsCount).Add(ctx, 1) | ||
m.inc(request.IDResponseErrorsCount) | ||
} else { | ||
inc(request.IDResponseValidCount) | ||
m.getMetric(request.IDResponseValidCount).Add(ctx, 1) | ||
m.inc(request.IDResponseValidCount) | ||
} | ||
|
||
inc(c.Result.ID) | ||
m.getMetric(c.Result.ID).Add(ctx, 1) | ||
m.inc(c.Result.ID) | ||
}, nil | ||
|
||
} | ||
} | ||
|
||
func (m *monitoringMiddleware) inc(id request.ResultID) { | ||
if counter, ok := m.ints[id]; ok { | ||
counter.Inc() | ||
} | ||
} | ||
|
||
func (m *monitoringMiddleware) getMetric(n request.ResultID) metric.Int64Counter { | ||
name := "http.server." + n | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I named those metrics We can rename those metrics in the local exporter. |
||
if met, ok := m.counters[name]; ok { | ||
return met | ||
} | ||
|
||
nm, _ := m.meter.Int64Counter(string(name)) | ||
m.counters[name] = nm | ||
return nm | ||
} | ||
|
||
// MonitoringMiddleware returns a middleware that increases monitoring counters for collecting metrics | ||
// about request processing. As input parameter it takes a map capable of mapping a request.ResultID to a counter. | ||
func MonitoringMiddleware(m map[request.ResultID]*monitoring.Int, mp metric.MeterProvider) Middleware { | ||
if mp == nil { | ||
mp = otel.GetMeterProvider() | ||
} | ||
|
||
mid := &monitoringMiddleware{ | ||
meter: mp.Meter("internal/beater/middleware"), | ||
ints: m, | ||
counters: map[request.ResultID]metric.Int64Counter{}, | ||
} | ||
|
||
return mid.Middleware() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: why do we have an additional
IDResponseErrorsInternal
in monitoringInt ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't really figured out why, but nothing can set it from this interceptor, so I suspect it's because something elsewhere sets it, only for monitoring.Int.
The only location that sets it though is jaeger, and it's not executed here. So it may be state from a previous test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed the metric from monitoring.Int, and the test doesn't fail either. So it does seem it's not being incremented in that interceptor (and the monitoring.Int test doesn't check for metrics that aren't emitted).