Skip to content

Commit

Permalink
systemtest: check _doc_count in txmetrics docs
Browse files Browse the repository at this point in the history
  • Loading branch information
axw committed Jan 21, 2021
1 parent 45e997b commit a846590
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelogs/head.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ https://github.com/elastic/apm-server/compare/7.11\...master[View commits]
[float]
==== Added
* Jaeger gRPC is now served over the same port as the Elastic APM agent protocol {pull}4618[4618]
* Add a `_doc_count` field to transaction histogram docs {pull}4647[4647]

[float]
==== Deprecated
Expand Down
53 changes: 47 additions & 6 deletions systemtest/aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package systemtest_test

import (
"context"
"encoding/json"
"net/http"
"strings"
"testing"
"time"

Expand All @@ -30,6 +33,7 @@ import (
"github.com/elastic/apm-server/systemtest"
"github.com/elastic/apm-server/systemtest/apmservertest"
"github.com/elastic/apm-server/systemtest/estest"
"github.com/elastic/go-elasticsearch/v7/esapi"
)

func TestTransactionAggregation(t *testing.T) {
Expand All @@ -56,21 +60,58 @@ func TestTransactionAggregation(t *testing.T) {

// Send some transactions to the server to be aggregated.
tracer := srv.Tracer()
tx := tracer.StartTransaction("name", "backend")
req, _ := http.NewRequest("GET", "/", nil)
tx.Context.SetHTTPRequest(req)
tx.Duration = time.Second
tx.End()
for i, name := range []string{"abc", "def"} {
for j := 0; j < (i+1)*5; j++ {
tx := tracer.StartTransaction(name, "backend")
req, _ := http.NewRequest("GET", "/", nil)
tx.Context.SetHTTPRequest(req)
tx.Duration = time.Second
tx.End()
}
}
tracer.Flush(nil)

result := systemtest.Elasticsearch.ExpectDocs(t, "apm-*",
result := systemtest.Elasticsearch.ExpectMinDocs(t, 2, "apm-*",
estest.ExistsQuery{Field: "transaction.duration.histogram"},
)
systemtest.ApproveEvents(t, t.Name(), result.Hits.Hits, "@timestamp")

// Make sure apm-server.aggregation.txmetrics metrics are published. Metric values are unit tested.
doc := getBeatsMonitoringStats(t, srv, nil)
assert.True(t, gjson.GetBytes(doc.RawSource, "beats_stats.metrics.apm-server.aggregation.txmetrics").Exists())

// Make sure the _doc_count field is added such that aggregations return
// the appropriate per-bucket doc_count values.
result = estest.SearchResult{}
_, err = systemtest.Elasticsearch.Do(context.Background(), &esapi.SearchRequest{
Index: []string{"apm-*"},
Body: strings.NewReader(`{
"size": 0,
"query": {"exists":{"field":"transaction.duration.histogram"}},
"aggs": {
"transaction_names": {
"terms": {"field": "transaction.name"}
}
}
}
`),
}, &result)
require.NoError(t, err)
require.Contains(t, result.Aggregations, "transaction_names")

type aggregationBucket struct {
Key string `json:"key"`
DocCount int `json:"doc_count"`
}
var aggregationResult struct {
Buckets []aggregationBucket `json:"buckets"`
}
err = json.Unmarshal(result.Aggregations["transaction_names"], &aggregationResult)
require.NoError(t, err)
assert.Equal(t, []aggregationBucket{
{Key: "def", DocCount: 10},
{Key: "abc", DocCount: 5},
}, aggregationResult.Buckets)
}

func TestTransactionAggregationShutdown(t *testing.T) {
Expand Down
61 changes: 58 additions & 3 deletions systemtest/approvals/TestTransactionAggregation.approved.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"events": [
{
"@timestamp": "dynamic",
"_doc_count": 5,
"agent": {
"name": "go"
},
Expand Down Expand Up @@ -35,20 +36,74 @@
}
},
"timeseries": {
"instance": "systemtest:name:865d6816622184cd"
"instance": "systemtest:abc:29d4be3fbd7f200f"
},
"transaction": {
"duration": {
"histogram": {
"counts": [
1
5
],
"values": [
1003519
]
}
},
"name": "name",
"name": "abc",
"root": true,
"type": "backend"
}
},
{
"@timestamp": "dynamic",
"_doc_count": 10,
"agent": {
"name": "go"
},
"ecs": {
"version": "dynamic"
},
"event": {
"ingested": "dynamic",
"outcome": "unknown"
},
"host": {
"hostname": "beowulf",
"name": "beowulf"
},
"observer": {
"ephemeral_id": "dynamic",
"hostname": "dynamic",
"id": "dynamic",
"type": "apm-server",
"version": "dynamic",
"version_major": "dynamic"
},
"processor": {
"event": "metric",
"name": "metric"
},
"service": {
"name": "systemtest",
"node": {
"name": "beowulf"
}
},
"timeseries": {
"instance": "systemtest:def:41b636ee77bc8c1c"
},
"transaction": {
"duration": {
"histogram": {
"counts": [
10
],
"values": [
1003519
]
}
},
"name": "def",
"root": true,
"type": "backend"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"events": [
{
"@timestamp": "dynamic",
"_doc_count": 1,
"agent": {
"name": "go"
},
Expand Down
3 changes: 2 additions & 1 deletion systemtest/estest/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func (r *SearchRequest) Do(ctx context.Context, out *SearchResult, opts ...Reque
}

type SearchResult struct {
Hits SearchHits `json:"hits"`
Hits SearchHits `json:"hits"`
Aggregations map[string]json.RawMessage `json:"aggregations"`
}

type SearchHits struct {
Expand Down

0 comments on commit a846590

Please sign in to comment.