diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/backend/ElasticBackend.java b/src/main/java/io/jenkins/plugins/opentelemetry/backend/ElasticBackend.java index 2f3b4539c..fc4a1290d 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/backend/ElasticBackend.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/backend/ElasticBackend.java @@ -15,6 +15,7 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; +import java.util.Optional; import org.apache.commons.lang.StringUtils; import org.jenkins.ui.icon.Icon; @@ -31,9 +32,11 @@ import edu.umd.cs.findbugs.annotations.Nullable; import hudson.Extension; import hudson.util.FormValidation; +import io.jenkins.plugins.opentelemetry.JenkinsOpenTelemetryPluginConfiguration; import io.jenkins.plugins.opentelemetry.TemplateBindingsProvider; import io.jenkins.plugins.opentelemetry.backend.elastic.ElasticLogsBackend; import io.jenkins.plugins.opentelemetry.job.log.LogStorageRetriever; +import jenkins.model.GlobalConfiguration; public class ElasticBackend extends ObservabilityBackend { @@ -157,15 +160,19 @@ public String getMetricsVisualizationUrlTemplate() { return null; } // see https://www.elastic.co/guide/en/kibana/6.8/sharing-dashboards.html - String kibanaSpaceBaseUrl; + String kibanaSpaceBaseUrl = "${kibanaBaseUrl}"; if (StringUtils.isBlank(this.getKibanaSpaceIdentifier())) { - kibanaSpaceBaseUrl = "${kibanaBaseUrl}"; + kibanaSpaceBaseUrl += "/app/kibana#/dashboards?"; } else { - kibanaSpaceBaseUrl = "${kibanaBaseUrl}/s/" + URLEncoder.encode(this.getKibanaSpaceIdentifier(), StandardCharsets.UTF_8); + kibanaSpaceBaseUrl += "/s/" + URLEncoder.encode(this.getKibanaSpaceIdentifier(), StandardCharsets.UTF_8) + "/app/kibana#/dashboards?"; } - return kibanaSpaceBaseUrl + "/app/kibana#/dashboards?" + - "title=" + URLEncoder.encode(getKibanaDashboardTitle(), StandardCharsets.UTF_8) + "&" + - "_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-24h%2Fh,to:now))"; + if (StringUtils.isNotBlank(this.getKibanaDashboardUrlParameters())){ + kibanaSpaceBaseUrl += this.getKibanaDashboardUrlParameters(); + } else { + kibanaSpaceBaseUrl += "title=" + URLEncoder.encode(getKibanaDashboardTitle(), StandardCharsets.UTF_8) + "&" + + "_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-24h%2Fh,to:now))"; + } + return kibanaSpaceBaseUrl; } public ElasticLogsBackend getElasticLogsBackend() { @@ -298,4 +305,17 @@ public interface TemplateBindings extends ObservabilityBackend.TemplateBindings{ String KIBANA_DASHBOARD_TITLE = "kibanaDashboardTitle"; String KIBANA_SPACE_IDENTIFIER = "kibanaSpaceIdentifier"; } + + + public static Optional get(){ + Optional ret = null; + final JenkinsOpenTelemetryPluginConfiguration configuration = GlobalConfiguration.all().get(JenkinsOpenTelemetryPluginConfiguration.class); + if (configuration != null) { + Optional backend = configuration.getObservabilityBackends().stream().filter(x -> x instanceof ElasticBackend).findFirst(); + if (!backend.isEmpty()) { + ret = Optional.of((ElasticBackend) backend.get()); + } + } + return ret; + } } diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackend.java b/src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackend.java index 97f74a3ab..96363d5ba 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackend.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackend.java @@ -10,6 +10,7 @@ import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Map; +import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; @@ -25,6 +26,7 @@ import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; import io.jenkins.plugins.opentelemetry.TemplateBindingsProvider; +import io.jenkins.plugins.opentelemetry.backend.ElasticBackend; import io.jenkins.plugins.opentelemetry.backend.ObservabilityBackend; import io.jenkins.plugins.opentelemetry.job.log.LogStorageRetriever; import jenkins.model.Jenkins; @@ -54,10 +56,11 @@ public Template getBuildLogsVisualizationUrlTemplate() { if (this.buildLogsVisualizationUrlGTemplate == null) { String kibanaSpaceBaseUrl; - if (StringUtils.isBlank(this.getKibanaSpaceIdentifier())) { + String spaceIdentifier = this.getKibanaSpaceIdentifier(); + if (StringUtils.isBlank(spaceIdentifier)) { kibanaSpaceBaseUrl = "${kibanaBaseUrl}"; } else { - kibanaSpaceBaseUrl = "${kibanaBaseUrl}/s/" + URLEncoder.encode(this.getKibanaSpaceIdentifier(), StandardCharsets.UTF_8); + kibanaSpaceBaseUrl = "${kibanaBaseUrl}/s/" + URLEncoder.encode(spaceIdentifier, StandardCharsets.UTF_8); } String urlTemplate = kibanaSpaceBaseUrl + "/app/logs/stream?" + @@ -79,8 +82,13 @@ public Map getOtelConfigurationProperties() { } private String getKibanaSpaceIdentifier() { - // FIXME implement getKibanaSpaceIdentifier - return ""; + String ret = ""; + Optional backend = ElasticBackend.get(); + if (!backend.isEmpty()) { + ElasticBackend elasticLogsBackend = backend.get(); + ret = elasticLogsBackend.getKibanaSpaceIdentifier(); + } + return ret; } @Override diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackendWithJenkinsVisualization.java b/src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackendWithJenkinsVisualization.java index 4c297689a..e0bab4ec7 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackendWithJenkinsVisualization.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackendWithJenkinsVisualization.java @@ -10,6 +10,7 @@ import java.net.URISyntaxException; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.logging.Logger; import org.apache.commons.lang.StringUtils; import org.apache.http.auth.Credentials; @@ -38,6 +39,10 @@ import jenkins.model.Jenkins; public class ElasticLogsBackendWithJenkinsVisualization extends ElasticLogsBackend { + private static final String MSG_ELASTICSEARCH_URL_IS_BLANK = "Elasticsearch URL is blank, logs will not be stored in Elasticsearch"; + + private final static Logger logger = Logger.getLogger(ElasticLogsBackendWithJenkinsVisualization.class.getName()); + private String elasticsearchUrl; private boolean disableSslVerifications; private String elasticsearchCredentialsId; @@ -52,9 +57,8 @@ public ElasticLogsBackendWithJenkinsVisualization() { public LogStorageRetriever newLogStorageRetriever(TemplateBindingsProvider templateBindingsProvider) { Template buildLogsVisualizationUrlTemplate = getBuildLogsVisualizationUrlTemplate(); if (StringUtils.isBlank(elasticsearchUrl)) { - return null; // FIXME handle case where this logs retriever is miss configured lacking of an - // Elasticsearch URL. We should use the rendering - // ElasticLogsBackendWithVisualizationOnlyThroughKibana + logger.warning(MSG_ELASTICSEARCH_URL_IS_BLANK); + throw new IllegalStateException(MSG_ELASTICSEARCH_URL_IS_BLANK); } else { Credentials credentials = new JenkinsCredentialsToApacheHttpCredentialsAdapter( () -> elasticsearchCredentialsId);