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

feat: dimensional metrics for elasticsearch.events.processed metric #54

Merged
merged 8 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
41 changes: 31 additions & 10 deletions appender.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func New(client *elasticsearch.Client, cfg Config) (*Appender, error) {
available: available,
closed: make(chan struct{}),
bulkItems: make(chan bulkIndexerItem, cfg.DocumentBufferSize),
metrics: ms,
metrics: *ms,
telemetryAttrs: cfg.MetricAttributes,
}
indexer.addCount(int64(len(available)), &indexer.availableBulkRequests, ms.availableBulkRequests)
Expand Down Expand Up @@ -240,12 +240,13 @@ func (a *Appender) Add(ctx context.Context, index string, document io.Reader) er
return nil
}

func (a *Appender) addCount(delta int64, lm *int64, m metric.Int64Counter) {
func (a *Appender) addCount(delta int64, lm *int64, m metric.Int64Counter, opts ...metric.AddOption) {
// legacy metric
atomic.AddInt64(lm, delta)

attrs := metric.WithAttributeSet(a.config.MetricAttributes)
m.Add(context.Background(), delta, attrs)
opts = append(opts, attrs)
m.Add(context.Background(), delta, opts...)
}

func (a *Appender) flush(ctx context.Context, bulkIndexer *bulkIndexer) error {
Expand Down Expand Up @@ -275,7 +276,7 @@ func (a *Appender) flush(ctx context.Context, bulkIndexer *bulkIndexer) error {
a.addCount(int64(flushed), &a.bytesTotal, a.metrics.bytesTotal)
}
if err != nil {
a.addCount(int64(n), &a.docsFailed, a.metrics.docsFailed)
atomic.AddInt64(&a.docsFailed, int64(n))
logger.Error("bulk indexing request failed", zap.Error(err))
if a.tracingEnabled() {
apm.CaptureError(ctx, err).Send()
Expand All @@ -284,7 +285,11 @@ func (a *Appender) flush(ctx context.Context, bulkIndexer *bulkIndexer) error {
var errTooMany errorTooManyRequests
// 429 may be returned as errors from the bulk indexer.
if errors.As(err, &errTooMany) {
a.addCount(int64(n), &a.tooManyRequests, a.metrics.tooManyRequests)
a.addCount(int64(n),
&a.tooManyRequests,
a.metrics.docsIndexed,
metric.WithAttributes(attribute.String("status", "TooMany")),
)
}
return err
}
Expand Down Expand Up @@ -319,19 +324,35 @@ func (a *Appender) flush(ctx context.Context, bulkIndexer *bulkIndexer) error {
}
}
if docsFailed > 0 {
a.addCount(docsFailed, &a.docsFailed, a.metrics.docsFailed)
atomic.AddInt64(&a.docsFailed, docsFailed)
}
if docsIndexed > 0 {
a.addCount(docsIndexed, &a.docsIndexed, a.metrics.docsIndexed)
a.addCount(docsIndexed,
&a.docsIndexed,
a.metrics.docsIndexed,
metric.WithAttributes(attribute.String("status", "Success")),
)
}
if tooManyRequests > 0 {
a.addCount(tooManyRequests, &a.tooManyRequests, a.metrics.tooManyRequests)
a.addCount(tooManyRequests,
&a.tooManyRequests,
a.metrics.docsIndexed,
metric.WithAttributes(attribute.String("status", "TooMany")),
)
}
if clientFailed > 0 {
a.addCount(clientFailed, &a.docsFailedClient, a.metrics.docsFailedClient)
a.addCount(clientFailed,
&a.docsFailedClient,
a.metrics.docsIndexed,
metric.WithAttributes(attribute.String("status", "FailedClient")),
)
}
if serverFailed > 0 {
a.addCount(serverFailed, &a.docsFailedServer, a.metrics.docsFailedServer)
a.addCount(serverFailed,
&a.docsFailedServer,
a.metrics.docsIndexed,
metric.WithAttributes(attribute.String("status", "FailedServer")),
)
}
logger.Debug(
"bulk request completed",
Expand Down
44 changes: 32 additions & 12 deletions appender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"go.opentelemetry.io/otel/attribute"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
Expand Down Expand Up @@ -145,6 +146,33 @@ loop:
assert.Equal(t, attrs, dp.Attributes)
}
}

var processedAsserted int
assertProcessedCounter := func(metric metricdata.Metrics, count int64, attrs attribute.Set) {
asserted++
counter := metric.Data.(metricdata.Sum[int64])
for _, dp := range counter.DataPoints {
metricdatatest.AssertHasAttributes[metricdata.DataPoint[int64]](t, dp, attrs.ToSlice()...)
status, exist := dp.Attributes.Value(attribute.Key("status"))
assert.True(t, exist)
switch status.AsString() {
case "Success":
processedAsserted++
assert.Equal(t, stats.Indexed, dp.Value)
case "FailedClient":
processedAsserted++
assert.Equal(t, stats.FailedClient, dp.Value)
case "FailedServer":
processedAsserted++
assert.Equal(t, stats.FailedServer, dp.Value)
case "TooMany":
processedAsserted++
assert.Equal(t, stats.TooManyRequests, dp.Value)
default:
assert.FailNow(t, "Unexpected metric with status: "+status.AsString())
}
}
}
// check the set of names and then check the counter or histogram
unexpectedMetrics := []string{}
for _, metric := range rm.ScopeMetrics[0].Metrics {
Expand All @@ -155,16 +183,8 @@ loop:
assertCounter(metric, stats.Active, indexerAttrs)
case "elasticsearch.bulk_requests.count":
assertCounter(metric, stats.BulkRequests, indexerAttrs)
case "elasticsearch.failed.count":
assertCounter(metric, stats.Failed, indexerAttrs)
case "elasticsearch.failed.client.count":
assertCounter(metric, stats.FailedClient, indexerAttrs)
case "elasticsearch.failed.server.count":
assertCounter(metric, stats.FailedServer, indexerAttrs)
case "elasticsearch.events.processed":
assertCounter(metric, stats.Indexed, indexerAttrs)
case "elasticsearch.failed.too_many_reqs":
assertCounter(metric, stats.TooManyRequests, indexerAttrs)
assertProcessedCounter(metric, stats.Indexed, indexerAttrs)
case "elasticsearch.bulk_requests.available":
assertCounter(metric, stats.AvailableBulkRequests, indexerAttrs)
case "elasticsearch.flushed.bytes":
Expand All @@ -180,7 +200,8 @@ loop:
}
}
assert.Empty(t, unexpectedMetrics)
assert.Equal(t, 10, asserted)
assert.Equal(t, 6, asserted)
assert.Equal(t, 4, processedAsserted)
}

func TestAppenderAvailableAppenders(t *testing.T) {
Expand Down Expand Up @@ -390,7 +411,7 @@ func TestAppenderFlushMetric(t *testing.T) {
if !ignoreCount {
assert.Equal(t, count, int(dp.Count))
} else {
assert.Greater(t, int(dp.Count), count)
assert.GreaterOrEqual(t, int(dp.Count), count)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I debugged it locally with the main branch, and it also quite often emits 2 counts here, it should be ok to expect greater or equal than 2 if I'm not mistaken.

}
assert.Positive(t, dp.Sum)
assert.Equal(t, attrs, dp.Attributes)
Expand Down Expand Up @@ -607,7 +628,6 @@ func TestAppenderCloseInterruptAdd(t *testing.T) {
defer cancel()
go func() {
added <- indexer.Add(addContext, "logs-foo-testing", readerFunc(func(p []byte) (int, error) {
fmt.Println("hello?")
close(readInvoked)
return copy(p, "{}"), nil
}))
Expand Down
36 changes: 6 additions & 30 deletions metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ type metrics struct {
bulkRequests metric.Int64Counter
docsAdded metric.Int64Counter
docsActive metric.Int64Counter
docsFailed metric.Int64Counter
docsFailedClient metric.Int64Counter
docsFailedServer metric.Int64Counter
docsIndexed metric.Int64Counter
tooManyRequests metric.Int64Counter
bytesTotal metric.Int64Counter
availableBulkRequests metric.Int64Counter
activeCreated metric.Int64Counter
Expand All @@ -56,7 +52,7 @@ type counterMetric struct {
p *metric.Int64Counter
}

func newMetrics(cfg Config) (metrics, error) {
func newMetrics(cfg Config) (*metrics, error) {
kyungeunni marked this conversation as resolved.
Show resolved Hide resolved
if cfg.MeterProvider == nil {
cfg.MeterProvider = otel.GetMeterProvider()
}
Expand All @@ -80,7 +76,7 @@ func newMetrics(cfg Config) (metrics, error) {
for _, m := range histograms {
err := newFloat64Histogram(meter, m)
if err != nil {
return ms, err
return &ms, err
}
}

Expand All @@ -92,39 +88,19 @@ func newMetrics(cfg Config) (metrics, error) {
},
{
name: "elasticsearch.events.count",
description: "the total number of items added to the indexer.",
description: "Number of APM Events received for indexing",
p: &ms.docsAdded,
},
{
name: "elasticsearch.events.queued",
description: "the number of active items waiting in the indexer's queue.",
p: &ms.docsActive,
},
{
name: "elasticsearch.failed.count",
description: "The amount of time a document was buffered for, in seconds.",
p: &ms.docsFailed,
},
{
name: "elasticsearch.failed.client.count",
description: "The number of docs failed to get indexed with client error(status_code >= 400 < 500, but not 429).",
p: &ms.docsFailedClient,
},
{
name: "elasticsearch.failed.server.count",
description: "The number of docs failed to get indexed with server error(status_code >= 500).",
p: &ms.docsFailedServer,
},
{
name: "elasticsearch.events.processed",
description: "The number of docs indexed successfully.",
description: "Number of APM Events flushed to Elasticsearch. Dimensions are used to report the project ID, success or failures",
kyungeunni marked this conversation as resolved.
Show resolved Hide resolved
p: &ms.docsIndexed,
},
{
name: "elasticsearch.failed.too_many_reqs",
description: "The number of 429 errors returned from the bulk indexer due to too many requests.",
p: &ms.tooManyRequests,
},
{
name: "elasticsearch.flushed.bytes",
description: "The total number of bytes written to the request body",
Expand All @@ -150,11 +126,11 @@ func newMetrics(cfg Config) (metrics, error) {
for _, m := range counters {
err := newInt64Counter(meter, m)
if err != nil {
return ms, err
return &ms, err
}
}

return ms, nil
return &ms, nil
}

func newInt64Counter(meter metric.Meter, c counterMetric) error {
Expand Down