Skip to content

Commit

Permalink
add major/minor version-aware HelpUrl builder
Browse files Browse the repository at this point in the history
  • Loading branch information
yaauie committed Oct 7, 2024
1 parent aa6ede4 commit dba08e0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
18 changes: 18 additions & 0 deletions logstash-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def versionMap = (Map) (new Yaml()).load(new File("$projectDir/../versions.yml")

description = """Logstash Core Java"""

String logstashCoreVersion = versionMap['logstash-core']
String jacksonVersion = versionMap['jackson']
String jacksonDatabindVersion = versionMap['jackson-databind']
String jrubyVersion = versionMap['jruby']['version']
Expand Down Expand Up @@ -183,6 +184,23 @@ artifacts {
}
}

task generateVersionInfoResources(type: DefaultTask) {
ext.outDir = layout.buildDirectory.dir("generated-resources/version-info").get()

inputs.property("version-info:logstash-core", logstashCoreVersion)
outputs.dir(ext.outDir)

doLast {
mkdir outDir;
def resourceFile = outDir.file('version-info.properties').asFile
resourceFile.text = "logstash-core: ${logstashCoreVersion}"
}
}
sourceSets {
main { output.dir(generateVersionInfoResources.outputs.files) }
}
processResources.dependsOn generateVersionInfoResources

configurations {
provided
}
Expand Down
20 changes: 20 additions & 0 deletions logstash-core/src/main/java/org/logstash/Logstash.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -45,6 +46,25 @@
*/
public final class Logstash implements Runnable, AutoCloseable {

public static final String VERSION_FULL;
public static final String VERSION_MAJOR;
public static final String VERSION_MINOR;
public static final String VERSION_PATCH;

static {
final Properties properties = new Properties();
try {
properties.load(Logstash.class.getResourceAsStream("/version-info.properties"));
VERSION_FULL = properties.getProperty("logstash-core");
final String[] versions = VERSION_FULL.split("\\.");
VERSION_MAJOR = versions[0];
VERSION_MINOR = versions[1];
VERSION_PATCH = versions[2];
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static final Logger LOGGER = LogManager.getLogger(Logstash.class);

/**
Expand Down
50 changes: 50 additions & 0 deletions logstash-core/src/main/java/org/logstash/health/HelpUrl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.logstash.health;

import org.logstash.Logstash;

import java.util.Objects;

public class HelpUrl {
static final String BASE_URL;
static {
final String versionAnchor;
if (Integer.parseInt(Logstash.VERSION_MAJOR) >= 9) {
versionAnchor = "master";
} else {
versionAnchor = String.format("%s.%s", Logstash.VERSION_MAJOR, Logstash.VERSION_MINOR);
}
BASE_URL = String.format("https://www.elastic.co/guide/en/logstash/%s/", versionAnchor);
}

public HelpUrl(final String page) {
this(page, null);
}

public HelpUrl withAnchor(final String anchor) {
return new HelpUrl(this.page, anchor);
}

private HelpUrl(final String page, final String anchor) {
Objects.requireNonNull(page, "page cannot be null");
this.page = page;
this.anchor = anchor;
}

private final String page;
private final String anchor;

private transient String resolved;

@Override
public String toString() {
if (resolved == null) {
final StringBuilder sb = new StringBuilder(BASE_URL);
sb.append(page).append(".html");
if (anchor != null) {
sb.append("#").append(anchor);
}
resolved = sb.toString();
}
return resolved;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ static class StatusProbe implements Probe<Details> {
.withDescription("the pipeline is not currently processing")
.withAdditionalImpactArea(ImpactArea.PIPELINE_EXECUTION);

static final HelpUrl HELP_URL = new HelpUrl("health-report-pipeline-status");

@Override
public Analysis analyze(final Details details) {
switch (details.getStatus().getState()) {
Expand All @@ -120,7 +122,8 @@ public Analysis analyze(final Details details) {
.withDiagnosis(db -> db
.withId(diagnosisId("loading"))
.withCause("pipeline is loading")
.withAction("if pipeline does not come up quickly, you may need to check the logs to see if it is stalled"))
.withAction("if pipeline does not come up quickly, you may need to check the logs to see if it is stalled")
.withHelpUrl(HELP_URL.withAnchor("loading").toString()))
.withImpact(NOT_PROCESSING.withSeverity(1).withDescription("pipeline is loading").build())
.build();
case RUNNING:
Expand All @@ -133,7 +136,8 @@ public Analysis analyze(final Details details) {
.withDiagnosis(db -> db
.withId(diagnosisId("finished"))
.withCause("pipeline has finished running because its inputs have been closed and events have been processed")
.withAction("if you expect this pipeline to run indefinitely, you will need to configure its inputs to continue receiving or fetching events"))
.withAction("if you expect this pipeline to run indefinitely, you will need to configure its inputs to continue receiving or fetching events")
.withHelpUrl(HELP_URL.withAnchor("finished").toString()))
.withImpact(NOT_PROCESSING.withSeverity(10).withDescription("pipeline has finished running").build())
.build();
case TERMINATED:
Expand All @@ -142,7 +146,8 @@ public Analysis analyze(final Details details) {
.withDiagnosis(db -> db
.withId(diagnosisId("terminated"))
.withCause("pipeline is not running, likely because it has encountered an error")
.withAction("view logs to determine the cause of abnormal pipeline shutdown"))
.withAction("view logs to determine the cause of abnormal pipeline shutdown")
.withHelpUrl(HELP_URL.withAnchor("terminated").toString()))
.withImpact(NOT_PROCESSING.withSeverity(1).build())
.build();
case UNKNOWN:
Expand All @@ -151,8 +156,9 @@ public Analysis analyze(final Details details) {
.withStatus(YELLOW)
.withDiagnosis(db -> db
.withId(diagnosisId("unknown"))
.withCause("pipeline is not known; it may have been recently deleted")
.withAction("check logs"))
.withCause("pipeline is not known; it may have been recently deleted or failed to start")
.withAction("view logs to determine if the pipeline failed to start")
.withHelpUrl(HELP_URL.withAnchor("unknown").toString()))
.withImpact(NOT_PROCESSING.withSeverity(2).build())
.build();
}
Expand Down

0 comments on commit dba08e0

Please sign in to comment.