Skip to content

Commit 6a27580

Browse files
committed
Configuration for dropping non-sampled transactions (elastic#3702)
* beater/config: add Config.Sampling * Handle "apm-server.sampling.keep_non_sampled" Add a publisher.Reporter middleware, which drops non-sampled transactions. * tests/system: test sampling.keep_non_sampled * Add changelog entry * Non-sampled => unsampled * Rename approvals
1 parent 8824195 commit 6a27580

File tree

13 files changed

+2175
-6
lines changed

13 files changed

+2175
-6
lines changed

beater/beater.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/elastic/apm-server/ingest/pipeline"
3636
logs "github.com/elastic/apm-server/log"
3737
"github.com/elastic/apm-server/publish"
38+
"github.com/elastic/apm-server/sampling"
3839
)
3940

4041
var (
@@ -172,6 +173,14 @@ func (bt *beater) Run(b *beat.Beat) error {
172173
}
173174
defer publisher.Stop()
174175

176+
reporter := publisher.Send
177+
if !bt.config.Sampling.KeepUnsampled {
178+
// The server has been configured to discard unsampled
179+
// transactions. Make sure this is done just before calling
180+
// the publisher to avoid affecting aggregations.
181+
reporter = sampling.NewDiscardUnsampledReporter(reporter)
182+
}
183+
175184
stopped := make(chan struct{})
176185
defer close(stopped)
177186
ctx, cancelContext := context.WithCancel(context.Background())
@@ -196,7 +205,7 @@ func (bt *beater) Run(b *beat.Beat) error {
196205
Config: bt.config,
197206
Logger: bt.logger,
198207
Tracer: tracer,
199-
Reporter: publisher.Send,
208+
Reporter: reporter,
200209
})
201210
}
202211

beater/config/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type Config struct {
8989
APIKeyConfig *APIKeyConfig `config:"api_key"`
9090
JaegerConfig JaegerConfig `config:"jaeger"`
9191
Aggregation AggregationConfig `config:"aggregation"`
92+
Sampling SamplingConfig `config:"sampling"`
9293

9394
Pipeline string
9495
}
@@ -141,6 +142,18 @@ func NewConfig(version string, ucfg *common.Config, outputESCfg *common.Config)
141142
return nil, err
142143
}
143144

145+
if !c.Sampling.KeepUnsampled && !c.Aggregation.Enabled {
146+
// Unsampled transactions should only be dropped
147+
// when transaction aggregation is enabled in the
148+
// server. This means the aggregations performed
149+
// by the APM UI will not have access to a complete
150+
// representation of the latency distribution.
151+
logger.Warn("" +
152+
"apm-server.sampling.keep_unsampled and " +
153+
"apm-server.aggregation.enabled are both false, " +
154+
"which will lead to incorrect metrics being reported in the APM UI",
155+
)
156+
}
144157
return c, nil
145158
}
146159

@@ -174,5 +187,6 @@ func DefaultConfig(beatVersion string) *Config {
174187
APIKeyConfig: defaultAPIKeyConfig(),
175188
JaegerConfig: defaultJaeger(),
176189
Aggregation: defaultAggregationConfig(),
190+
Sampling: defaultSamplingConfig(),
177191
}
178192
}

beater/config/config_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ func Test_UnpackConfig(t *testing.T) {
210210
MaxTransactionGroups: 123,
211211
HDRHistogramSignificantFigures: 1,
212212
},
213+
Sampling: SamplingConfig{
214+
KeepUnsampled: true,
215+
},
213216
},
214217
},
215218
"merge config with default": {
@@ -239,9 +242,10 @@ func Test_UnpackConfig(t *testing.T) {
239242
},
240243
},
241244
},
242-
"jaeger.grpc.enabled": true,
243-
"api_key.enabled": true,
244-
"aggregation.enabled": true,
245+
"jaeger.grpc.enabled": true,
246+
"api_key.enabled": true,
247+
"aggregation.enabled": true,
248+
"sampling.keep_unsampled": false,
245249
},
246250
outCfg: &Config{
247251
Host: "localhost:3000",
@@ -316,6 +320,9 @@ func Test_UnpackConfig(t *testing.T) {
316320
MaxTransactionGroups: 1000,
317321
HDRHistogramSignificantFigures: 2,
318322
},
323+
Sampling: SamplingConfig{
324+
KeepUnsampled: false,
325+
},
319326
},
320327
},
321328
"kibana trailing slash": {

beater/config/sampling.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package config
19+
20+
// SamplingConfig holds configuration related to sampling.
21+
type SamplingConfig struct {
22+
// KeepUnsampled controls whether unsampled
23+
// transactions should be recorded.
24+
KeepUnsampled bool `config:"keep_unsampled"`
25+
}
26+
27+
func defaultSamplingConfig() SamplingConfig {
28+
return SamplingConfig{
29+
// In a future major release we will set this to
30+
// false, and then later remove the option.
31+
KeepUnsampled: true,
32+
}
33+
}

0 commit comments

Comments
 (0)