Skip to content

Commit

Permalink
Extract more elasticsearch code (#310)
Browse files Browse the repository at this point in the history
* Move Elasticsearch code under backend package

* Move Elasticsearch code under backend package
  • Loading branch information
cyrille-leclerc authored Feb 6, 2022
1 parent 4a35569 commit 6978007
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import io.jenkins.plugins.opentelemetry.job.log.es.ElasticsearchRetriever;
import io.jenkins.plugins.opentelemetry.backend.elastic.ElasticsearchRetriever;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright The Original Author or Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.jenkins.plugins.opentelemetry.job.log.es;
package io.jenkins.plugins.opentelemetry.backend.elastic;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -72,15 +72,15 @@ public ElasticsearchRetriever(@Nonnull String url, @Nonnull String username, @No
/**
* @return the current timestamp on a valid format to Elasticsearch.
*/
public static final String now() {
private static final String now() {
ZonedDateTime date = ZonedDateTime.now(TimeZone.getTimeZone("UTC").toZoneId());
return DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(date);
}

/**
* @return the RestClientBuilder to create the Elasticsearch REST client.
*/
public RestClientBuilder getBuilder() {
private RestClientBuilder getBuilder() {
RestClientBuilder builder = RestClient.builder(HttpHost.create(url));
builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
Expand All @@ -98,7 +98,7 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli
* @return A page with log lines the results of the search. The object contains the scrollID to use in {@link #next(String)} requests.
* @throws IOException
*/
public SearchResponse search(@Nonnull String traceId) throws IOException {
SearchResponse search(@Nonnull String traceId) throws IOException {
return search(traceId, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import hudson.console.AnnotatedLargeText;
import io.jenkins.plugins.opentelemetry.JenkinsOpenTelemetryPluginConfiguration;
import io.jenkins.plugins.opentelemetry.backend.ElasticBackend;
import io.jenkins.plugins.opentelemetry.job.log.es.ElasticsearchRetriever;
import io.jenkins.plugins.opentelemetry.backend.elastic.ElasticsearchRetriever;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchResponse;
Expand All @@ -30,7 +30,9 @@
import static io.jenkins.plugins.opentelemetry.semconv.OpenTelemetryTracesSemanticConventions.LABELS;

/**
* Retrieve the logs from Elasticsearch.
* Retrieve the logs from the logs backend.
*
* TODO extract Elasticsearch specific code and add abstraction layer for a backend agnostic Log Retriever
*/
public class OtelLogRetriever {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright The Original Author or Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.jenkins.plugins.opentelemetry.job.log;
package io.jenkins.plugins.opentelemetry.backend.elastic;

import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
Expand All @@ -17,9 +17,6 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.testcontainers.DockerClientFactory;

import java.io.IOException;

import static io.jenkins.plugins.opentelemetry.job.log.ElasticsearchContainer.INDEX_PATTERN;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assume.assumeTrue;
Expand Down Expand Up @@ -57,17 +54,17 @@ public void setUp() throws Exception {

@Test
public void testCredentialsDoValidate() {
assertEquals(descriptor.doValidate(CRED_ID, esContainer.getUrl(), INDEX_PATTERN).kind, FormValidation.Kind.OK);
assertEquals(descriptor.doValidate(CRED_ID, esContainer.getUrl(), ElasticsearchContainer.INDEX_PATTERN).kind, FormValidation.Kind.OK);

assertEquals(descriptor.doValidate(WRONG_CREDS, esContainer.getUrl(), INDEX_PATTERN).kind,
assertEquals(descriptor.doValidate(WRONG_CREDS, esContainer.getUrl(), ElasticsearchContainer.INDEX_PATTERN).kind,
FormValidation.Kind.ERROR
);
assertEquals(descriptor.doValidate(CRED_ID, "nowhere", INDEX_PATTERN).kind, FormValidation.Kind.ERROR);
assertEquals(descriptor.doValidate(CRED_ID, "nowhere", ElasticsearchContainer.INDEX_PATTERN).kind, FormValidation.Kind.ERROR);
}

@Test
public void testIndexPatternDoValidate() throws IOException {
assertEquals(FormValidation.Kind.OK, descriptor.doValidate(CRED_ID, esContainer.getUrl(), INDEX_PATTERN).kind);
public void testIndexPatternDoValidate() {
assertEquals(FormValidation.Kind.OK, descriptor.doValidate(CRED_ID, esContainer.getUrl(), ElasticsearchContainer.INDEX_PATTERN).kind);
assertEquals(FormValidation.Kind.ERROR, descriptor.doValidate(CRED_ID, esContainer.getUrl(), "pattern").kind);
assertEquals(FormValidation.Kind.ERROR, descriptor.doValidate(CRED_ID, esContainer.getUrl(), "").kind);
assertEquals(FormValidation.Kind.ERROR, descriptor.doValidate(CRED_ID, "", "pattern").kind);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright The Original Author or Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.jenkins.plugins.opentelemetry.job.log;
package io.jenkins.plugins.opentelemetry.backend.elastic;

import io.jenkins.plugins.opentelemetry.semconv.OpenTelemetryTracesSemanticConventions;
import org.apache.http.HttpHost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
* Copyright The Original Author or Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.jenkins.plugins.opentelemetry.job.log;
package io.jenkins.plugins.opentelemetry.backend.elastic;

import io.jenkins.plugins.opentelemetry.job.log.es.ElasticsearchRetriever;
import org.elasticsearch.action.search.ClearScrollResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
Expand All @@ -23,7 +22,7 @@
/**
* Test the class to retrieve the logs from Elasticsearch.
*/
public class RetrieverTest {
public class ElasticsearchRetrieverTest {

@Rule
public ElasticsearchContainer esContainer = new ElasticsearchContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright The Original Author or Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.jenkins.plugins.opentelemetry.job.log;
package io.jenkins.plugins.opentelemetry.backend.elastic;

import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
Expand All @@ -13,7 +13,6 @@
import io.jenkins.plugins.opentelemetry.backend.ElasticBackend;
import io.jenkins.plugins.opentelemetry.backend.ObservabilityBackend;
import io.jenkins.plugins.opentelemetry.job.MonitoringAction;
import io.jenkins.plugins.opentelemetry.job.log.es.ElasticsearchRetriever;
import org.elasticsearch.action.search.SearchResponse;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand All @@ -29,10 +28,9 @@
import java.util.ArrayList;
import java.util.List;

import static io.jenkins.plugins.opentelemetry.job.log.ElasticsearchContainer.*;
import static org.junit.Assume.assumeTrue;

public class PipelineTest {
public class PipelineElasticsearchBackendTest {

public static final String CRED_ID = "credID";
public static final int OTEL_PORT = 8200;
Expand Down Expand Up @@ -61,7 +59,7 @@ public void setup() throws Exception {
String kibanaEndpoint = "http://localhost:" + environment.getServicePort("kibana_1", KIBANA_PORT);

SystemCredentialsProvider.getInstance().getCredentials().add(
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, CRED_ID, "", USER_NAME, PASSWORD)
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, CRED_ID, "", ElasticsearchContainer.USER_NAME, ElasticsearchContainer.PASSWORD)
);

JenkinsOpenTelemetryPluginConfiguration config = JenkinsOpenTelemetryPluginConfiguration.get();
Expand All @@ -78,14 +76,14 @@ public void setup() throws Exception {
List<ObservabilityBackend> observabilityBackends = new ArrayList<>();
ElasticBackend esBackend = new ElasticBackend();
esBackend.setElasticsearchUrl(esEndpoint);
esBackend.setIndexPattern(INDEX_PATTERN);
esBackend.setIndexPattern(ElasticsearchContainer.INDEX_PATTERN);
esBackend.setKibanaBaseUrl(kibanaEndpoint);
esBackend.setElasticsearcCredentialsId(CRED_ID);
observabilityBackends.add(esBackend);
config.setObservabilityBackends(observabilityBackends);
config.initializeOpenTelemetry();

elasticsearchRetriever = new ElasticsearchRetriever(esEndpoint, USER_NAME, PASSWORD, INDEX_PATTERN);
elasticsearchRetriever = new ElasticsearchRetriever(esEndpoint, ElasticsearchContainer.USER_NAME, ElasticsearchContainer.PASSWORD, ElasticsearchContainer.INDEX_PATTERN);
}

@Test
Expand Down

0 comments on commit 6978007

Please sign in to comment.