Skip to content

Commit

Permalink
Merge pull request #799 from kuisathaverat/fix/url_kibana
Browse files Browse the repository at this point in the history
fix: use Kibana Dashboard parameters
  • Loading branch information
kuisathaverat authored Jan 29, 2024
2 parents e1fd4d4 + db351fd commit c2733c0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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;

Check warning on line 175 in src/main/java/io/jenkins/plugins/opentelemetry/backend/ElasticBackend.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 163-175 are not covered by tests
}

public ElasticLogsBackend getElasticLogsBackend() {
Expand Down Expand Up @@ -298,4 +305,17 @@ public interface TemplateBindings extends ObservabilityBackend.TemplateBindings{
String KIBANA_DASHBOARD_TITLE = "kibanaDashboardTitle";
String KIBANA_SPACE_IDENTIFIER = "kibanaSpaceIdentifier";
}


public static Optional<ElasticBackend> get(){
Optional<ElasticBackend> ret = null;
final JenkinsOpenTelemetryPluginConfiguration configuration = GlobalConfiguration.all().get(JenkinsOpenTelemetryPluginConfiguration.class);
if (configuration != null) {

Check warning on line 313 in src/main/java/io/jenkins/plugins/opentelemetry/backend/ElasticBackend.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 313 is only partially covered, one branch is missing
Optional<ObservabilityBackend> backend = configuration.getObservabilityBackends().stream().filter(x -> x instanceof ElasticBackend).findFirst();
if (!backend.isEmpty()) {

Check warning on line 315 in src/main/java/io/jenkins/plugins/opentelemetry/backend/ElasticBackend.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 315 is only partially covered, one branch is missing
ret = Optional.of((ElasticBackend) backend.get());
}
}
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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)) {

Check warning on line 60 in src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackend.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 60 is only partially covered, one branch is missing
kibanaSpaceBaseUrl = "${kibanaBaseUrl}";
} else {
kibanaSpaceBaseUrl = "${kibanaBaseUrl}/s/" + URLEncoder.encode(this.getKibanaSpaceIdentifier(), StandardCharsets.UTF_8);
kibanaSpaceBaseUrl = "${kibanaBaseUrl}/s/" + URLEncoder.encode(spaceIdentifier, StandardCharsets.UTF_8);

Check warning on line 63 in src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackend.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 63 is not covered by tests
}

String urlTemplate = kibanaSpaceBaseUrl + "/app/logs/stream?" +
Expand All @@ -79,8 +82,13 @@ public Map<String, String> getOtelConfigurationProperties() {
}

private String getKibanaSpaceIdentifier() {
// FIXME implement getKibanaSpaceIdentifier
return "";
String ret = "";
Optional<ElasticBackend> backend = ElasticBackend.get();
if (!backend.isEmpty()) {

Check warning on line 87 in src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackend.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 87 is only partially covered, one branch is missing
ElasticBackend elasticLogsBackend = backend.get();
ret = elasticLogsBackend.getKibanaSpaceIdentifier();
}
return ret;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Check warning on line 61 in src/main/java/io/jenkins/plugins/opentelemetry/backend/elastic/ElasticLogsBackendWithJenkinsVisualization.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 60-61 are not covered by tests
} else {
Credentials credentials = new JenkinsCredentialsToApacheHttpCredentialsAdapter(
() -> elasticsearchCredentialsId);
Expand Down

0 comments on commit c2733c0

Please sign in to comment.