CorePlugin extends StagemonitorPlugin { public URL getElasticsearchUrl() { final List<URL> urls = elasticsearchUrls.getValue(); if (urls.isEmpty()) { return null; } final int index = accessesToElasticsearchUrl.getAndIncrement() % urls.size(); URL elasticsearchURL = urls.get(index); final String defaultUsernameValue = elasticsearchDefaultUsername.getValue(); final String defaultPasswordValue = elasticsearchDefaultPassword.getValue(); if (elasticsearchURL.getUserInfo() == null && ! defaultUsernameValue.isEmpty() && ! defaultPasswordValue.isEmpty()) { try { String username = URLEncoder.encode(defaultUsernameValue, "UTF-8"); String password = URLEncoder.encode(defaultPasswordValue, "UTF-8"); StringBuilder stringBuilder = new StringBuilder(); stringBuilder .append(elasticsearchURL.getProtocol()) .append(": .append(username) .append(":") .append(password) .append("@") .append(elasticsearchURL.getHost()) .append(":") .append(elasticsearchURL.getPort()) .append(elasticsearchURL.getPath()); return new URL(stringBuilder.toString()); } catch (Exception e) { logger.error(e.getMessage(), e); } } return elasticsearchURL; } CorePlugin();  CorePlugin(ElasticsearchClient elasticsearchClient); @Override void initializePlugin(InitArguments initArguments); @Override List<Class<? extends StagemonitorPlugin>> dependsOn(); @Override void onShutDown(); MeasurementSession getMeasurementSession(); Metric2Registry getMetricRegistry(); ElasticsearchClient getElasticsearchClient(); GrafanaClient getGrafanaClient(); void setElasticsearchClient(ElasticsearchClient elasticsearchClient); static String getNameOfLocalHost(); boolean isStagemonitorActive(); boolean isInternalMonitoringActive(); long getConsoleReportingInterval(); boolean isReportToJMX(); int getGraphiteReportingInterval(); String getGraphiteHostName(); int getGraphitePort(); String getApplicationName(); String getInstanceName(); String getHostName(); URL getElasticsearchUrl(); Collection<URL> getElasticsearchUrls(); String getElasticsearchUrlsWithoutAuthenticationInformation(); Collection<String> getElasticsearchConfigurationSourceProfiles(); boolean isDeactivateStagemonitorIfEsConfigSourceIsDown(); Collection<MetricName> getExcludedMetricsPatterns(); Collection<String> getDisabledPlugins(); Integer getReloadConfigurationInterval(); Collection<String> getExcludeContaining(); Collection<String> getIncludePackages(); Collection<String> getExcludePackages(); boolean isAttachAgentAtRuntime(); Collection<String> getExcludedInstrumenters(); URL getInfluxDbUrl(); String getInfluxDbDb(); boolean isReportToElasticsearch(); boolean isReportToGraphite(); String getGrafanaUrl(); String getGrafanaApiKey(); int getThreadPoolQueueCapacityLimit(); IndexSelector getIndexSelector(); int getElasticsearchReportingInterval(); Integer getMoveToColdNodesAfterDays(); boolean isOnlyLogElasticsearchMetricReports(); boolean isDebugInstrumentation(); Collection<String> getExportClassesWithName(); Integer getNumberOfReplicas(); Integer getNumberOfShards(); void closeOnShutdown(Closeable closeable); List<URL> getRemotePropertiesConfigUrls(); boolean isDeactivateStagemonitorIfRemotePropertyServerIsDown(); int getDeleteElasticsearchMetricsAfterDays(); String getMetricsIndexTemplate(); boolean isMetricsIndexTemplateDefaultValue(); boolean isInitializeElasticsearch(); HealthCheckRegistry getHealthCheckRegistry(); static final String POOLS_QUEUE_CAPACITY_LIMIT_KEY; }
@Test public void testCycleElasticsearchUrls() throws Exception { CorePlugin corePlugin = ConfigurationRegistry.builder() .addOptionProvider(new CorePlugin()) .addConfigSource(new SimpleSource("test") .add("stagemonitor.reporting.elasticsearch.url", "http: .build() .getConfig(CorePlugin.class); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: }
@Test public void testCycleElasticsearchUrlsBasicAuth() throws Exception { CorePlugin corePlugin = ConfigurationRegistry.builder() .addOptionProvider(new CorePlugin()) .addConfigSource(new SimpleSource("test") .add("stagemonitor.reporting.elasticsearch.url", "http: .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: 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: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("https: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("https: assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: }
@Test public void testElasticsearchUrlsBasicAuthViaConfig() throws Exception { CorePlugin corePlugin = ConfigurationRegistry.builder() .addOptionProvider(new CorePlugin()) .addConfigSource(new SimpleSource("test") .add("stagemonitor.reporting.elasticsearch.url", "http: .add("stagemonitor.reporting.elasticsearch.username", "user") .add("stagemonitor.reporting.elasticsearch.password", "password")) .build() .getConfig(CorePlugin.class); assertThat(corePlugin.getElasticsearchUrl().toString()).isEqualTo("http: }
@Test public void testNoElasticsearchUrl() throws Exception { CorePlugin corePlugin = ConfigurationRegistry.builder() .addOptionProvider(new CorePlugin()) .addConfigSource(new SimpleSource("test")) .build() .getConfig(CorePlugin.class); assertNull(corePlugin.getElasticsearchUrl()); }