From a34e5cd6c94f4ae2920ff9ac55c94ef39c7371ac Mon Sep 17 00:00:00 2001 From: Napster Date: Mon, 21 May 2018 00:18:20 +0200 Subject: [PATCH] Add custom sentry tags (#103) --- LavalinkServer/application.yml.example | 7 +++- .../server/config/SentryConfigProperties.java | 34 +++++++++++++++++++ .../server/config/SentryConfiguration.java | 27 ++++++++++++--- .../lavalink/server/config/ServerConfig.java | 4 +++ 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 LavalinkServer/src/main/java/lavalink/server/config/SentryConfigProperties.java diff --git a/LavalinkServer/application.yml.example b/LavalinkServer/application.yml.example index b1ba3d02d..2fb41c8d0 100644 --- a/LavalinkServer/application.yml.example +++ b/LavalinkServer/application.yml.example @@ -16,10 +16,15 @@ lavalink: mixer: true http: true local: false - sentryDsn: "" bufferDurationMs: 400 youtubePlaylistLoadLimit: 600 +sentry: + dsn: "" +# tags: +# some_key: some_value +# another_key: another_value + logging: file: max-history: 30 diff --git a/LavalinkServer/src/main/java/lavalink/server/config/SentryConfigProperties.java b/LavalinkServer/src/main/java/lavalink/server/config/SentryConfigProperties.java new file mode 100644 index 000000000..eca63fa28 --- /dev/null +++ b/LavalinkServer/src/main/java/lavalink/server/config/SentryConfigProperties.java @@ -0,0 +1,34 @@ +package lavalink.server.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by napster on 20.05.18. + */ +@Component +@ConfigurationProperties(prefix = "sentry") +public class SentryConfigProperties { + + private String dsn = ""; + private Map tags = new HashMap<>(); + + public String getDsn() { + return dsn; + } + + public void setDsn(String dsn) { + this.dsn = dsn; + } + + public Map getTags() { + return tags; + } + + public void setTags(Map tags) { + this.tags = tags; + } +} diff --git a/LavalinkServer/src/main/java/lavalink/server/config/SentryConfiguration.java b/LavalinkServer/src/main/java/lavalink/server/config/SentryConfiguration.java index 194f2ea9e..e4f1ad48a 100644 --- a/LavalinkServer/src/main/java/lavalink/server/config/SentryConfiguration.java +++ b/LavalinkServer/src/main/java/lavalink/server/config/SentryConfiguration.java @@ -12,6 +12,7 @@ import org.springframework.context.annotation.Configuration; import java.io.IOException; +import java.util.Map; import java.util.Properties; /** @@ -23,20 +24,36 @@ public class SentryConfiguration { private static final Logger log = LoggerFactory.getLogger(SentryConfiguration.class); private static final String SENTRY_APPENDER_NAME = "SENTRY"; - public SentryConfiguration(ServerConfig serverConfig) { - String sentryDsn = serverConfig.getSentryDsn(); - if (sentryDsn != null && !sentryDsn.isEmpty()) { - turnOn(sentryDsn); + public SentryConfiguration(ServerConfig serverConfig, SentryConfigProperties sentryConfig) { + + String dsn = sentryConfig.getDsn(); + boolean warnDeprecatedDsnConfig = false; + if (dsn == null || dsn.isEmpty()) { + //try deprecated config location + dsn = serverConfig.getSentryDsn(); + warnDeprecatedDsnConfig = true; + } + + if (dsn != null && !dsn.isEmpty()) { + turnOn(dsn, sentryConfig.getTags()); + if (warnDeprecatedDsnConfig) { + log.warn("Please update the location of the sentry dsn in lavalinks config file / your environment " + + "vars, it is now located under 'sentry.dsn' instead of 'lavalink.server.sentryDsn'."); + } } else { turnOff(); } } - public void turnOn(String dsn) { + public void turnOn(String dsn, Map tags) { log.info("Turning on sentry"); SentryClient sentryClient = Sentry.init(dsn); + if (tags != null) { + tags.forEach(sentryClient::addTag); + } + // set the git commit hash this was build on as the release Properties gitProps = new Properties(); try { diff --git a/LavalinkServer/src/main/java/lavalink/server/config/ServerConfig.java b/LavalinkServer/src/main/java/lavalink/server/config/ServerConfig.java index a3d16a2a5..fb05a9d8a 100644 --- a/LavalinkServer/src/main/java/lavalink/server/config/ServerConfig.java +++ b/LavalinkServer/src/main/java/lavalink/server/config/ServerConfig.java @@ -43,6 +43,10 @@ public void setPassword(String password) { private String sentryDsn = ""; + /** + * @deprecated use {@link SentryConfigProperties} instead. + */ + @Deprecated(since = "3") public String getSentryDsn() { return sentryDsn; }