private boolean isDisabled(String name) { return WildcardMatcher.anyMatch(config.getDisableMetrics(), name) != null; }
HttpURLConnection connection = null; try { URL url = new URL(reporterConfiguration.getServerUrls().get(0).toString() + "/"); if (logger.isDebugEnabled()) { logger.debug("Starting healthcheck to {}", url); if (!reporterConfiguration.isVerifyServerCert()) { if (connection instanceof HttpsURLConnection) { trustAll((HttpsURLConnection) connection); if (reporterConfiguration.getSecretToken() != null) { connection.setRequestProperty("Authorization", "Bearer " + reporterConfiguration.getSecretToken()); connection.setConnectTimeout((int) reporterConfiguration.getServerTimeout().getMillis()); connection.setReadTimeout((int) reporterConfiguration.getServerTimeout().getMillis()); connection.connect();
private boolean shouldFlush() { final long written = deflater.getBytesWritten() + DslJsonSerializer.BUFFER_SIZE; final boolean flush = written >= reporterConfiguration.getApiRequestSize(); if (flush && logger.isDebugEnabled()) { logger.debug("Flushing, because request size limit exceeded {}/{}", written, reporterConfiguration.getApiRequestSize()); } return flush; }
if (!reporterConfiguration.isVerifyServerCert()) { if (connection instanceof HttpsURLConnection) { trustAll((HttpsURLConnection) connection); if (reporterConfiguration.getSecretToken() != null) { connection.setRequestProperty("Authorization", "Bearer " + reporterConfiguration.getSecretToken()); connection.setRequestProperty("Content-Type", "application/x-ndjson"); connection.setUseCaches(false); connection.setConnectTimeout((int) reporterConfiguration.getServerTimeout().getMillis()); connection.setReadTimeout((int) reporterConfiguration.getServerTimeout().getMillis()); connection.connect(); os = new DeflaterOutputStream(connection.getOutputStream(), deflater); timeoutTask = new FlushOnTimeoutTimerTask(reporter); if (logger.isDebugEnabled()) { logger.debug("Scheduling request timeout in {}", reporterConfiguration.getApiRequestTime()); timeoutTimer.schedule(timeoutTask, reporterConfiguration.getApiRequestTime().getMillis());
@BeforeEach void setUp() { final ConfigurationRegistry configurationRegistry = SpyConfiguration.createSpyConfig(); ReporterConfiguration reporterConfiguration = configurationRegistry.getConfig(ReporterConfiguration.class); when(reporterConfiguration.getFlushInterval()).thenReturn(TimeDuration.of("-1ms")); when(reporterConfiguration.getMaxQueueSize()).thenReturn(0); reportingEventHandler = mock(ReportingEventHandler.class); reporter = new ApmServerReporter(true, reporterConfiguration, reportingEventHandler); }
@BeforeEach void setUp() throws Exception { handler = exchange -> { if (!exchange.getRequestPath().equals("/healthcheck")) { receivedHttpRequests.incrementAndGet(); } exchange.setStatusCode(200).endExchange(); }; receivedHttpRequests.set(0); config = SpyConfiguration.createSpyConfig(); reporterConfiguration = config.getConfig(ReporterConfiguration.class); when(reporterConfiguration.getFlushInterval()).thenReturn(TimeDuration.of("-1s")); when(reporterConfiguration.getServerUrls()).thenReturn(Collections.singletonList(new URL("http://localhost:" + port))); SystemInfo system = new SystemInfo("x64", "localhost", "platform"); final Service service = new Service(); final ProcessInfo title = new ProcessInfo("title"); final ProcessorEventHandler processorEventHandler = ProcessorEventHandler.loadProcessors(config); final IntakeV2ReportingEventHandler v2handler = new IntakeV2ReportingEventHandler(service, title, system, reporterConfiguration, processorEventHandler, new DslJsonSerializer(mock(StacktraceConfiguration.class))); reporter = new ApmServerReporter(false, reporterConfiguration, v2handler); }
this.lifecycleListeners = lifecycleListeners; this.activationListeners = activationListeners; int maxPooledElements = configurationRegistry.getConfig(ReporterConfiguration.class).getMaxQueueSize() * 2; coreConfiguration = configurationRegistry.getConfig(CoreConfiguration.class); transactionPool = QueueBasedObjectPool.ofRecyclable(AtomicQueueFactory.<Transaction>newQueue(createBoundedMpmc(maxPooledElements)), false, activationListener.init(this); reporter.scheduleMetricReporting(metricRegistry, configurationRegistry.getConfig(ReporterConfiguration.class).getMetricsIntervalMs());
@Test void testValidatingSslCertificate() throws Exception { when(reporterConfiguration.isVerifyServerCert()).thenReturn(true); final Reporter reporter = reporterFactory.createReporter(configuration, null, null); reporter.report(new Transaction(mock(ElasticApmTracer.class))); reporter.flush().get(); assertThat(requestHandled).isFalse(); } }
private static List<URL> shuffleUrls(ReporterConfiguration reporterConfiguration) { List<URL> serverUrls = new ArrayList<>(reporterConfiguration.getServerUrls()); // shuffling the URL list helps to distribute the load across the apm servers // when there are multiple agents, they should not all start connecting to the same apm server Collections.shuffle(serverUrls); return serverUrls; }
@Test void testSecretToken() throws ExecutionException, InterruptedException { when(reporterConfiguration.getSecretToken()).thenReturn("token"); handler = exchange -> { assertThat(exchange.getRequestHeaders().get("Authorization").getFirst()).isEqualTo("Bearer token"); receivedHttpRequests.incrementAndGet(); exchange.setStatusCode(200).endExchange(); }; reporter.report(new Transaction(MockTracer.create())); reporter.flush().get(); assertThat(reporter.getDropped()).isEqualTo(0); assertThat(receivedHttpRequests.get()).isEqualTo(1); }
@Test void testNotValidatingSslCertificate() throws Exception { when(reporterConfiguration.isVerifyServerCert()).thenReturn(false); final Reporter reporter = reporterFactory.createReporter(configuration, null, null); reporter.report(new Transaction(mock(ElasticApmTracer.class))); reporter.flush().get(); assertThat(requestHandled).isTrue(); }
@BeforeEach void setUp() throws Exception { server = new Server(); final SslContextFactory sslContextFactory = new SslContextFactory(getClass().getResource("/keystore").getPath()); sslContextFactory.setKeyStorePassword("password"); sslContextFactory.getSslContext(); final HttpConfiguration httpConfiguration = new HttpConfiguration(); httpConfiguration.setSecureScheme("https"); httpConfiguration.setSecurePort(0); final HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration); httpsConfiguration.addCustomizer(new SecureRequestCustomizer()); final ServerConnector httpsConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfiguration)); httpsConnector.setPort(0); server.addConnector(httpsConnector); server.setHandler(new AbstractHandler() { @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) { baseRequest.setHandled(true); requestHandled.set(true); } }); server.start(); configuration = SpyConfiguration.createSpyConfig(); reporterConfiguration = configuration.getConfig(ReporterConfiguration.class); when(reporterConfiguration.getServerUrls()).thenReturn(Collections.singletonList(new URL("https://localhost:" + getPort()))); }
@Test void testDisabledMetrics() { when(config.getDisableMetrics()).thenReturn(List.of(WildcardMatcher.valueOf("jvm.gc.*"))); final DoubleSupplier problematicMetric = () -> { throw new RuntimeException("Huston, we have a problem"); }; metricRegistry.addUnlessNegative("jvm.gc.count", emptyMap(), problematicMetric); metricRegistry.addUnlessNan("jvm.gc.count", emptyMap(), problematicMetric); metricRegistry.add("jvm.gc.count", emptyMap(), problematicMetric); assertThat(metricRegistry.getMetricSets()).isEmpty(); } }