You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SentryErrorReporter.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.logger;
  18. import com.dmdirc.interfaces.Connection;
  19. import com.dmdirc.util.ClientInfo;
  20. import java.time.LocalDateTime;
  21. import java.time.ZoneOffset;
  22. import java.util.Date;
  23. import java.util.Optional;
  24. import javax.inject.Inject;
  25. import javax.inject.Singleton;
  26. import com.getsentry.raven.Raven;
  27. import com.getsentry.raven.RavenFactory;
  28. import com.getsentry.raven.dsn.Dsn;
  29. import com.getsentry.raven.event.Event;
  30. import com.getsentry.raven.event.EventBuilder;
  31. import com.getsentry.raven.event.interfaces.ExceptionInterface;
  32. import com.getsentry.raven.event.interfaces.MessageInterface;
  33. /**
  34. * Facilitates reporting errors to the DMDirc developers.
  35. */
  36. @Singleton
  37. public class SentryErrorReporter {
  38. /** DSN used to connect to Sentry. */
  39. private static final String SENTRY_DSN = "https://d53a31a3c53c4a4f91c5ff503e612677:"
  40. + "e0a8aa1ecca14568a9f52d052ecf6a30@sentry.dmdirc.com/2?raven.async=false";
  41. /** Template to use when sending mode alias reports. */
  42. private static final String MODE_ALIAS_TEMPLATE = "%s\n\nConnection headers:\n%s";
  43. private final ClientInfo clientInfo;
  44. @Inject
  45. public SentryErrorReporter(final ClientInfo clientInfo) {
  46. this.clientInfo = clientInfo;
  47. }
  48. /**
  49. * Sends an error report caused by some kind of exception.
  50. *
  51. * @param message The message generated when the exception occurred.
  52. * @param level The severity level of the error.
  53. * @param timestamp The timestamp that the error first occurred at.
  54. * @param exception The actual exception, if available.
  55. */
  56. public void sendException(
  57. final String message,
  58. final ErrorLevel level,
  59. final LocalDateTime timestamp,
  60. final Optional<Throwable> exception) {
  61. final EventBuilder eventBuilder = newEventBuilder()
  62. .withMessage(message)
  63. .withLevel(getSentryLevel(level))
  64. .withTimestamp(Date.from(timestamp.toInstant(ZoneOffset.UTC)));
  65. exception.ifPresent(e -> eventBuilder.withSentryInterface(new ExceptionInterface(e)));
  66. send(eventBuilder.build());
  67. }
  68. /**
  69. * Sends a report of missing mode aliases.
  70. *
  71. * @param connection The connection that encountered the missing aliases.
  72. * @param channelModes The missing channel modes (may be empty).
  73. * @param userModes The missing user modes (may be empty).
  74. * @param modeAliasVersion The version of the mode aliases used.
  75. */
  76. public void sendModeAliasesReport(
  77. final Connection connection,
  78. final String channelModes,
  79. final String userModes,
  80. final String modeAliasVersion) {
  81. final String title = getModeAliasReportTitle(channelModes, userModes, connection.getIrcd());
  82. final EventBuilder eventBuilder = newEventBuilder()
  83. .withMessage(title)
  84. .withCulprit(title)
  85. .withLevel(Event.Level.INFO)
  86. .withTag("ircd.type", connection.getIrcd())
  87. .withTag("ircd.version", connection.getParser().get().getServerSoftware())
  88. .withTag("network", connection.getNetwork())
  89. .withTag("server", connection.getAddress())
  90. .withTag("modealiases", modeAliasVersion)
  91. .withSentryInterface(new MessageInterface(MODE_ALIAS_TEMPLATE, title,
  92. connection.getParser().get().getServerInformationLines().toString()));
  93. send(eventBuilder.build());
  94. }
  95. /**
  96. * Constructs a title for a mode alias report.
  97. *
  98. * @param channelModes The missing channel modes (may be empty).
  99. * @param userModes The missing user modes (may be empty).
  100. * @param ircd The name of the IRCd that has the modes.
  101. *
  102. * @return A title string to use for mode alias reports.
  103. */
  104. private String getModeAliasReportTitle(final String channelModes, final String userModes,
  105. final String ircd) {
  106. final StringBuilder title = new StringBuilder("Missing mode aliases:");
  107. if (!channelModes.isEmpty()) {
  108. title.append(" channel: +").append(channelModes);
  109. }
  110. if (!userModes.isEmpty()) {
  111. title.append(" user: ").append(userModes);
  112. }
  113. title.append(" [").append(ircd).append(']');
  114. return title.toString();
  115. }
  116. /**
  117. * Creates a new event builder with DMDirc and platform information pre-populated.
  118. *
  119. * @return A new event builder.
  120. */
  121. private EventBuilder newEventBuilder() {
  122. return new EventBuilder()
  123. .withServerName("")
  124. .withRelease(clientInfo.getVersion())
  125. .withTag("version", clientInfo.getVersion())
  126. .withTag("version.major", clientInfo.getMajorVersion())
  127. .withTag("os.name", clientInfo.getOperatingSystemName())
  128. .withTag("os.version", clientInfo.getOperatingSystemVersion())
  129. .withTag("os.arch", clientInfo.getOperatingSystemArchitecture())
  130. .withTag("encoding", clientInfo.getSystemFileEncoding())
  131. .withTag("locale", clientInfo.getSystemDefaultLocale())
  132. .withTag("jvm.name", clientInfo.getJavaName())
  133. .withTag("jvm.vendor", clientInfo.getJavaVendor())
  134. .withTag("jvm.version", clientInfo.getJavaVersion())
  135. .withTag("jvm.version.major", clientInfo.getJavaMajorVersion());
  136. }
  137. /**
  138. * Connects to Sentry and sends the specified event.
  139. *
  140. * @param event The event to be sent.
  141. */
  142. private void send(final Event event) {
  143. final Raven raven = RavenFactory.ravenInstance(new Dsn(SENTRY_DSN));
  144. raven.sendEvent(event);
  145. }
  146. /**
  147. * Converts a DMDirc error level into a Sentry event level.
  148. *
  149. * @param level The DMDirc error level to convert.
  150. *
  151. * @return The corresponding sentry error level.
  152. */
  153. private static Event.Level getSentryLevel(final ErrorLevel level) {
  154. switch (level) {
  155. case FATAL:
  156. return Event.Level.FATAL;
  157. case HIGH:
  158. return Event.Level.ERROR;
  159. case MEDIUM:
  160. return Event.Level.WARNING;
  161. case LOW:
  162. return Event.Level.INFO;
  163. default:
  164. return Event.Level.INFO;
  165. }
  166. }
  167. }