|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package co.elastic.apm.agent.impl; |
| 20 | + |
| 21 | +import co.elastic.apm.agent.AbstractInstrumentationTest; |
| 22 | +import co.elastic.apm.agent.MockReporter; |
| 23 | +import co.elastic.apm.agent.configuration.CoreConfiguration; |
| 24 | +import co.elastic.apm.agent.configuration.SpyConfiguration; |
| 25 | +import co.elastic.apm.agent.impl.metadata.MetaData; |
| 26 | +import co.elastic.apm.agent.objectpool.TestObjectPoolFactory; |
| 27 | +import co.elastic.apm.agent.report.ApmServerClient; |
| 28 | +import org.junit.jupiter.api.AfterAll; |
| 29 | +import org.junit.jupiter.api.AfterEach; |
| 30 | +import org.junit.jupiter.api.BeforeAll; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.stagemonitor.configuration.ConfigurationRegistry; |
| 33 | + |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | +import static org.mockito.Mockito.when; |
| 40 | + |
| 41 | +class DropUnsampledTransactionsTest extends AbstractInstrumentationTest { |
| 42 | + |
| 43 | + private static ApmServerClient apmServerClient = mock(ApmServerClient.class); |
| 44 | + |
| 45 | + private static MockReporter reporter = new MockReporter(); |
| 46 | + |
| 47 | + private static ElasticApmTracer tracer; |
| 48 | + |
| 49 | + @BeforeAll |
| 50 | + static void startTracer() { |
| 51 | + ConfigurationRegistry configurationRegistry = SpyConfiguration.createSpyConfig(); |
| 52 | + tracer = new ElasticApmTracer(configurationRegistry, reporter, new TestObjectPoolFactory(), apmServerClient, "ephemeralId", MetaData.create(configurationRegistry, "ephemeralId")); |
| 53 | + tracer.start(false); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterAll |
| 57 | + static void stopTracer() { |
| 58 | + tracer.stop(); |
| 59 | + } |
| 60 | + |
| 61 | + @AfterEach |
| 62 | + void resetReporter() { |
| 63 | + reporter.reset(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void whenTheAPMServerSupportsKeepingUnsampledTransactionsUnsampledTransactionsShouldBeReported() throws IOException { |
| 68 | + when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(true); |
| 69 | + tracer.getConfig(CoreConfiguration.class).getSampleRate().update(0.0, SpyConfiguration.CONFIG_SOURCE_NAME); |
| 70 | + |
| 71 | + tracer.startRootTransaction(null).end(); |
| 72 | + |
| 73 | + assertThat(reporter.getTransactions().size()).isEqualTo(1); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void whenTheAPMServerSupportsKeepingUnsampledTransactionsSampledTransactionsShouldBeReported() throws IOException { |
| 78 | + when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(true); |
| 79 | + tracer.getConfig(CoreConfiguration.class).getSampleRate().update(1.0, SpyConfiguration.CONFIG_SOURCE_NAME); |
| 80 | + |
| 81 | + tracer.startRootTransaction(null).end(); |
| 82 | + |
| 83 | + assertThat(reporter.getTransactions().size()).isEqualTo(1); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void whenTheAPMServerDoesNotSupportsKeepingUnsampledTransactionsUnsampledTransactionsShouldNotBeReported() throws IOException { |
| 88 | + when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(false); |
| 89 | + tracer.getConfig(CoreConfiguration.class).getSampleRate().update(0.0, SpyConfiguration.CONFIG_SOURCE_NAME); |
| 90 | + |
| 91 | + tracer.startRootTransaction(null).end(); |
| 92 | + |
| 93 | + assertThat(reporter.getTransactions().size()).isEqualTo(0); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void whenTheAPMServerDoesNotSupportsKeepingUnsampledTransactionsSampledTransactionsShouldBeReported() throws IOException { |
| 98 | + when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(false); |
| 99 | + tracer.getConfig(CoreConfiguration.class).getSampleRate().update(1.0, SpyConfiguration.CONFIG_SOURCE_NAME); |
| 100 | + |
| 101 | + tracer.startRootTransaction(null).end(); |
| 102 | + |
| 103 | + assertThat(reporter.getTransactions().size()).isEqualTo(1); |
| 104 | + } |
| 105 | +} |
0 commit comments