-
-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prometheus metrics for logs and basic jvm stats
- Loading branch information
1 parent
315804f
commit 2a81830
Showing
6 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
LavalinkServer/src/main/java/lavalink/server/config/MetricsPrometheusConfigProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package lavalink.server.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Created by napster on 20.05.18. | ||
*/ | ||
@Component | ||
@ConfigurationProperties("metrics.prometheus") | ||
public class MetricsPrometheusConfigProperties { | ||
|
||
private boolean enabled = false; | ||
private String endpoint = ""; | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
public String getEndpoint() { | ||
return endpoint; | ||
} | ||
|
||
public void setEndpoint(String endpoint) { | ||
this.endpoint = endpoint; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
LavalinkServer/src/main/java/lavalink/server/metrics/PrometheusMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package lavalink.server.metrics; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
import io.prometheus.client.hotspot.DefaultExports; | ||
import io.prometheus.client.logback.InstrumentedAppender; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Created by napster on 08.05.18. | ||
*/ | ||
@Component | ||
@ConditionalOnProperty("metrics.prometheus.enabled") | ||
public class PrometheusMetrics { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(PrometheusMetrics.class); | ||
|
||
public PrometheusMetrics() { | ||
|
||
InstrumentedAppender prometheusAppender = new InstrumentedAppender(); | ||
//log metrics | ||
final LoggerContext factory = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
final ch.qos.logback.classic.Logger root = factory.getLogger(Logger.ROOT_LOGGER_NAME); | ||
prometheusAppender.setContext(root.getLoggerContext()); | ||
prometheusAppender.start(); | ||
root.addAppender(prometheusAppender); | ||
|
||
//jvm (hotspot) metrics | ||
DefaultExports.initialize(); | ||
|
||
log.info("Prometheus metrics set up"); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
LavalinkServer/src/main/java/lavalink/server/metrics/PrometheusMetricsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package lavalink.server.metrics; | ||
|
||
import io.prometheus.client.CollectorRegistry; | ||
import io.prometheus.client.exporter.common.TextFormat; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.annotation.Nullable; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Created by napster on 18.10.17. - Copied from Quarterdeck on 20.05.2018 | ||
* <p> | ||
* Used to expose the prometheus metrics. Some code copied from prometheus' own MetricsServlet | ||
*/ | ||
@RestController | ||
@RequestMapping("${metrics.prometheus.endpoint:/metrics}") | ||
@ConditionalOnBean(PrometheusMetrics.class) | ||
public class PrometheusMetricsController { | ||
|
||
private final CollectorRegistry registry; | ||
|
||
public PrometheusMetricsController() { | ||
this.registry = CollectorRegistry.defaultRegistry; | ||
} | ||
|
||
@GetMapping(produces = TextFormat.CONTENT_TYPE_004) | ||
public ResponseEntity<String> getMetrics(@Nullable @RequestParam(name = "name[]", required = false) String[] includedParam) | ||
throws IOException { | ||
return buildAnswer(includedParam); | ||
} | ||
|
||
private ResponseEntity<String> buildAnswer(@Nullable String[] includedParam) throws IOException { | ||
Set<String> params; | ||
if (includedParam == null) { | ||
params = Collections.emptySet(); | ||
} else { | ||
params = new HashSet<>(Arrays.asList(includedParam)); | ||
} | ||
|
||
Writer writer = new StringWriter(); | ||
try (writer) { | ||
TextFormat.write004(writer, this.registry.filteredMetricFamilySamples(params)); | ||
writer.flush(); | ||
} | ||
|
||
return new ResponseEntity<>(writer.toString(), HttpStatus.OK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters