public URL getElasticsearchUrl() { return corePlugin.getElasticsearchUrl(); }
@Before public void setUp() throws Exception { final CorePlugin corePlugin = mock(CorePlugin.class); final URL url = new URL("http://localhost:41234"); when(corePlugin.getElasticsearchUrl()).thenReturn(url); when(corePlugin.getElasticsearchUrls()).thenReturn(Collections.singletonList(url)); when(corePlugin.getThreadPoolQueueCapacityLimit()).thenReturn(10000); when(corePlugin.isInitialized()).thenReturn(TRUE); elasticsearchClient = new ElasticsearchClient(corePlugin, new HttpClient(), -1, Collections.emptyList()); }
private JsonNode searchForElasticsearchIdsByTraceIds() { return httpClient.send("POST", corePlugin.getElasticsearchUrl() + "/stagemonitor-spans*/_msearch", ElasticsearchClient.CONTENT_TYPE_NDJSON, multiSearchOutputStreamHandler, responseHandler); }
@Before public void setUp() throws Exception { configuration = mock(ConfigurationRegistry.class); CorePlugin corePlugin = mock(CorePlugin.class); TracingPlugin tracingPlugin = mock(TracingPlugin.class); this.servletPlugin = mock(ServletPlugin.class); when(configuration.getConfig(CorePlugin.class)).thenReturn(corePlugin); when(configuration.getConfig(TracingPlugin.class)).thenReturn(tracingPlugin); when(configuration.getConfig(ServletPlugin.class)).thenReturn(servletPlugin); when(tracingPlugin.getDefaultRateLimitSpansPerMinute()).thenReturn(1000000d); when(tracingPlugin.getOnlyReportSpansWithName()).thenReturn(Collections.emptyList()); final URL url = new URL("http://localhost:9200"); when(corePlugin.getElasticsearchUrl()).thenReturn(url); when(corePlugin.getElasticsearchUrls()).thenReturn(Collections.singletonList(url)); ElasticsearchClient elasticsearchClient = mock(ElasticsearchClient.class); when(elasticsearchClient.isElasticsearchAvailable()).thenReturn(true); when(corePlugin.getElasticsearchClient()).thenReturn(elasticsearchClient); when(corePlugin.getMetricRegistry()).thenReturn(new Metric2Registry()); when(servletPlugin.isHonorDoNotTrackHeader()).thenReturn(true); tracer = new SpanWrappingTracer(new MockTracer(), Arrays.asList( new SpanContextInformation.SpanContextSpanEventListener(), new SpanContextInformation.SpanFinalizer() ) ); GlobalTracerTestHelper.override(tracer); when(tracingPlugin.getTracer()).thenReturn(tracer); assertThat(tracer.scopeManager().active()).isNull(); }
private void sendBulk(HttpClient.OutputStreamHandler outputStreamHandler, boolean logBulkErrors) { final HttpClient.ResponseHandler<Void> responseHandler = logBulkErrors ? BulkErrorReportingResponseHandler.INSTANCE : NoopResponseHandler.<Void>getInstance(); httpClient.send("POST", corePlugin.getElasticsearchUrl() + BULK, CONTENT_TYPE_NDJSON, outputStreamHandler, responseHandler); }
elasticsearchUrl = new URL("http://localhost:" + elasticsearchPort); AbstractElasticsearchTest.corePlugin = mock(CorePlugin.class); when(corePlugin.getElasticsearchUrl()).thenReturn(elasticsearchUrl); when(corePlugin.getElasticsearchUrls()).thenReturn(Collections.singletonList(elasticsearchUrl)); when(corePlugin.getThreadPoolQueueCapacityLimit()).thenReturn(1000);
private void execute(String method, String path, String logMessage) { if (!isElasticsearchAvailable()) { return; } final String url = corePlugin.getElasticsearchUrl() + "/" + path; logger.info(logMessage, path); try { httpClient.send(HttpRequestBuilder.forUrl(url).method(method).build()); } finally { logger.info(logMessage, "Done " + path); } }
public int sendAsJson(final String method, final String path, final Object requestBody) { return httpClient.send(HttpRequestBuilder.<Integer>jsonRequest(method, corePlugin.getElasticsearchUrl() + path, requestBody) .responseHandler(StatusCodeResponseHandler.WITH_ERROR_LOGGING) .build()); }
@Test public void testCycleElasticsearchUrls() throws Exception { CorePlugin corePlugin = ConfigurationRegistry.builder() .addOptionProvider(new CorePlugin()) .addConfigSource(new SimpleSource("test") .add("stagemonitor.reporting.elasticsearch.url", "http://bla:1/,http://bla:2,http://bla:3")) .build() .getConfig(CorePlugin.class); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://bla:1"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://bla:2"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://bla:3"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://bla:1"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://bla:2"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://bla:3"); }
@Override public void reportMetrics(final Map<MetricName, Gauge> gauges, final Map<MetricName, Counter> counters, final Map<MetricName, Histogram> histograms, final Map<MetricName, Meter> meters, final Map<MetricName, Timer> timers) { if (!corePlugin.isStagemonitorActive()) { return; } long timestamp = clock.getTime(); final Timer.Context time = registry.timer(reportingTimeMetricName).time(); final MetricsOutputStreamHandler metricsOutputStreamHandler = new MetricsOutputStreamHandler(gauges, counters, histograms, meters, timers, timestamp); if (!corePlugin.isOnlyLogElasticsearchMetricReports()) { if (!elasticsearchClient.isElasticsearchAvailable()) { return; } final String url = corePlugin.getElasticsearchUrl() + "/" + getTodaysIndexName() + "/" + METRICS_TYPE + "/_bulk"; httpClient.send("POST", url, CONTENT_TYPE_JSON, metricsOutputStreamHandler, NoopResponseHandler.INSTANCE); } else { try { final ByteArrayOutputStream os = new ByteArrayOutputStream(); metricsOutputStreamHandler.withHttpURLConnection(os); elasticsearchMetricsLogger.info(os.toString("UTF-8")); } catch (IOException e) { logger.warn(e.getMessage(), e); } } time.stop(); }
@Test public void doNotReportSpan() throws Exception { when(elasticsearchTracingPlugin.isOnlyLogElasticsearchSpanReports()).thenReturn(false); when(elasticsearchClient.isElasticsearchAvailable()).thenReturn(false); when(corePlugin.getElasticsearchUrl()).thenReturn(null); reportSpan(); Mockito.verify(httpClient, times(0)).send(any(), any(), any(), any(), any()); Mockito.verify(spanLogger, times(0)).info(anyString()); Assert.assertFalse(reporter.isActive(null)); verifyTimerCreated(1); }
public int delete(final String path) { return httpClient.send(HttpRequestBuilder.<Integer>forUrl(corePlugin.getElasticsearchUrl() + path) .method("DELETE") .responseHandler(StatusCodeResponseHandler.WITH_ERROR_LOGGING) .build()); }
@Test public void testCycleElasticsearchUrlsBasicAuth() throws Exception { CorePlugin corePlugin = ConfigurationRegistry.builder() .addOptionProvider(new CorePlugin()) .addConfigSource(new SimpleSource("test") .add("stagemonitor.reporting.elasticsearch.url", "http://bla:1/,http://other:p%21p@bla:2,https://bla:3,http://bla:4/search") .add("stagemonitor.reporting.elasticsearch.username", "user") .add("stagemonitor.reporting.elasticsearch.password", "p@ssword!")) .build() .getConfig(CorePlugin.class); URL cycle1 = corePlugin.getElasticsearchUrl(); String basicAuth = HttpClient.getBasicAuthFromUserInfo(cycle1); String testBasicAuth = "Basic " + DatatypeConverter.printBase64Binary("user:p@ssword!".getBytes()); assertThat(basicAuth).isEqualTo(testBasicAuth); assertThat(cycle1.toString()).isEqualTo("http://user:p%40ssword%21@bla:1"); URL cycle2 = corePlugin.getElasticsearchUrl(); String basicAuthCycle2 = HttpClient.getBasicAuthFromUserInfo(cycle2); String testBasicAuthCycle2 = "Basic " + DatatypeConverter.printBase64Binary("other:p!p".getBytes()); assertThat(basicAuth).isEqualTo(testBasicAuth); assertThat(cycle2.toString()).isEqualTo("http://other:p%21p@bla:2"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("https://user:p%40ssword%21@bla:3"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://user:p%40ssword%21@bla:4/search"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://user:p%40ssword%21@bla:1"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://other:p%21p@bla:2"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("https://user:p%40ssword%21@bla:3"); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://user:p%40ssword%21@bla:4/search"); }
public void updateIndexSettings(String indexPattern, final Map<String, ?> settings) { if (!isElasticsearchAvailable()) { return; } final String url = corePlugin.getElasticsearchUrl() + "/" + indexPattern + "/_settings?ignore_unavailable=true"; logger.info("Updating index settings {}\n{}", indexPattern, settings); httpClient.send(HttpRequestBuilder.<Integer>jsonRequest("PUT", url, settings) .responseHandler(new StatusCodeResponseHandler(new ErrorLoggingResponseHandler())) .build()); }
public JsonNode getJson(final String path, final boolean suppressWarning) { return httpClient.send(HttpRequestBuilder.<JsonNode>forUrl(corePlugin.getElasticsearchUrl() + path) .successHandler(new HttpClient.ResponseHandler<JsonNode>() { @Override public JsonNode handleResponse(HttpRequest<?> httpRequest, InputStream is, Integer statusCode, IOException e) throws IOException { return JsonUtils.getMapper().readTree(is); } }).errorHandler(new HttpClient.ResponseHandler<JsonNode>() { @Override public JsonNode handleResponse(HttpRequest<?> httpRequest, InputStream is, Integer statusCode, IOException e) throws IOException { if (! suppressWarning && Integer.valueOf(404).equals(statusCode)) { logger.warn(e.getMessage(), e); } return null; } }) .build()); }
@Override public void alert(AlertArguments alertArguments) { String target = alertArguments.getSubscription().getTarget(); if (StringUtils.isEmpty(target)) { target = "/stagemonitor/alerts"; } httpClient.send(HttpRequestBuilder.<Integer>jsonRequest("POST", corePlugin.getElasticsearchUrl() + target, alertArguments.getIncident()).build()); }
@Test public void testElasticsearchUrlsBasicAuthViaConfig() throws Exception { CorePlugin corePlugin = ConfigurationRegistry.builder() .addOptionProvider(new CorePlugin()) .addConfigSource(new SimpleSource("test") .add("stagemonitor.reporting.elasticsearch.url", "http://bla:1") .add("stagemonitor.reporting.elasticsearch.username", "user") .add("stagemonitor.reporting.elasticsearch.password", "password")) .build() .getConfig(CorePlugin.class); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http://user:password@bla:1"); }
final URL elasticsearchUrl = corePlugin.getElasticsearchUrl(); if (elasticsearchUrl == null) { return;
@Test public void testNoElasticsearchUrl() throws Exception { CorePlugin corePlugin = ConfigurationRegistry.builder() .addOptionProvider(new CorePlugin()) .addConfigSource(new SimpleSource("test")) .build() .getConfig(CorePlugin.class); assertNull(corePlugin.getElasticsearchUrl()); }
@Override public void initializePlugin(InitArguments initArguments) { this.metricRegistry = initArguments.getMetricRegistry(); this.healthCheckRegistry = initArguments.getHealthCheckRegistry(); final Integer reloadInterval = getReloadConfigurationInterval(); if (reloadInterval > 0) { initArguments.getConfiguration().scheduleReloadAtRate(reloadInterval, TimeUnit.SECONDS); } initArguments.getMetricRegistry().register(MetricName.name("online").build(), new Gauge<Integer>() { @Override public Integer getValue() { return 1; } }); // it's important to initialize the ElasticsearchClient via getElasticsearchClient() // otherwise the periodic availability check might not get started getElasticsearchClient(); if (isReportToElasticsearch()) { final GrafanaClient grafanaClient = getGrafanaClient(); grafanaClient.createElasticsearchDatasource(getElasticsearchUrl()); grafanaClient.sendGrafanaDashboardAsync("grafana/ElasticsearchCustomMetricsDashboard.json"); } registerReporters(initArguments.getMetricRegistry(), initArguments.getConfiguration(), initArguments.getMeasurementSession()); }