Skip to content

Commit 360f6b9

Browse files
authored
Merge pull request #123 from jjcard/add-context-tags
Make contextTags configurable
2 parents 8e9d230 + 6de8517 commit 360f6b9

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ appenders:
6363
| [`serverName`](https://docs.sentry.io/platforms/java/configuration/#server-name) | [empty] | Override the server name (rather than looking it up dynamically) | `10.0.0.1` |
6464
| [`inAppIncludes`](https://docs.sentry.io/platforms/java/configuration/#in-app-includes) | [empty] | List of package prefixes used by application code | `['com.example','com.foo']` |
6565
| [`inAppExcludes`](https://docs.sentry.io/platforms/java/configuration/#in-app-excludes) | [empty] | List of package prefixes not used by application code | `['com.thirdparty','com.anotherthirdparty']` |
66+
| `contextTags` | [empty] | context tags names applied as Sentry tags to each event | `['contextTag1','contextTag2']` |
6667

6768
If you need to set configuration properties not listed above, append them to the `dsn` as described [here](https://docs.sentry.io/clients/java/config/#configuration-via-the-dsn).
6869

src/main/java/org/dhatim/dropwizard/sentry/logging/SentryAppenderFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public class SentryAppenderFactory extends AbstractAppenderFactory<ILoggingEvent
5454
@JsonProperty
5555
public String configurator = null;
5656

57+
@JsonProperty
58+
public List<String> contextTags = null;
59+
5760
@Override
5861
public Appender<ILoggingEvent> build(LoggerContext context,
5962
String applicationName,
@@ -69,6 +72,7 @@ public Appender<ILoggingEvent> build(LoggerContext context,
6972
Optional.ofNullable(serverName).ifPresent(options::setServerName);
7073
Optional.ofNullable(inAppIncludes).ifPresent(inAppIncludes -> inAppIncludes.forEach(options::addInAppInclude));
7174
Optional.ofNullable(inAppExcludes).ifPresent(inAppExcludes -> inAppExcludes.forEach(options::addInAppExclude));
75+
Optional.ofNullable(contextTags).ifPresent(contextTags -> contextTags.forEach(options::addContextTag));
7276
Optional.ofNullable(configurator).ifPresent(configurator -> {
7377
try {
7478
Class<?> klass = Class.forName(configurator);

src/test/java/org/dhatim/dropwizard/sentry/logging/SentryAppenderFactoryTest.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77
import io.dropwizard.logging.common.async.AsyncLoggingEventAppenderFactory;
88
import io.dropwizard.logging.common.filter.ThresholdLevelFilterFactory;
99
import io.dropwizard.logging.common.layout.DropwizardLayoutFactory;
10+
import io.sentry.SentryOptions;
1011
import io.sentry.logback.SentryAppender;
12+
import org.dhatim.dropwizard.sentry.SentryConfigurator;
13+
import org.hamcrest.collection.IsIterableContainingInOrder;
14+
import org.hamcrest.collection.IsMapContaining;
1115
import org.junit.jupiter.api.Test;
1216

1317
import java.io.IOException;
18+
import java.util.List;
19+
import java.util.Map;
1420

1521
import static org.hamcrest.CoreMatchers.instanceOf;
1622
import static org.hamcrest.MatcherAssert.assertThat;
17-
import static org.junit.jupiter.api.Assertions.assertNull;
18-
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
import static org.junit.jupiter.api.Assertions.*;
1924

2025
public class SentryAppenderFactoryTest {
2126

@@ -50,4 +55,42 @@ public void buildSentryAppenderShouldWorkWithValidConfiguration() {
5055
assertThat(appender, instanceOf(SentryAppender.class));
5156
}
5257

58+
@Test
59+
void buildSentryAppenderFullConfiguration() {
60+
SentryAppenderFactory factory = new SentryAppenderFactory();
61+
factory.dsn = "https://user:[email protected]/id";
62+
factory.environment = "test";
63+
factory.tags = Map.of("tag1", "value1");
64+
factory.release = "1.0.0";
65+
factory.serverName = "10.0.0.1";
66+
factory.inAppIncludes = List.of("com.example");
67+
factory.inAppExcludes = List.of("com.thirdparty");
68+
factory.contextTags = List.of("contextTag1");
69+
factory.configurator = CaptureSentryConfigurator.class.getName();
70+
71+
Appender<ILoggingEvent> appender = factory.build(context, "", layoutFactory, levelFilterFactory, asyncAppenderFactory);
72+
assertThat(appender, instanceOf(SentryAppender.class));
73+
74+
SentryOptions capturedOptions = CaptureSentryConfigurator.capturedOptions;
75+
assertNotNull(capturedOptions);
76+
77+
assertEquals("https://user:[email protected]/id", capturedOptions.getDsn());
78+
assertEquals("test", capturedOptions.getEnvironment());
79+
assertEquals("1.0.0", capturedOptions.getRelease());
80+
assertThat(capturedOptions.getContextTags(), IsIterableContainingInOrder.contains("contextTag1"));
81+
assertEquals("10.0.0.1", capturedOptions.getServerName());
82+
assertThat(capturedOptions.getTags(), IsMapContaining.hasEntry("tag1", "value1"));
83+
assertThat(capturedOptions.getInAppIncludes(), IsIterableContainingInOrder.contains("com.example"));
84+
assertThat(capturedOptions.getInAppExcludes(), IsIterableContainingInOrder.contains("com.thirdparty"));
85+
86+
}
87+
88+
protected static class CaptureSentryConfigurator implements SentryConfigurator {
89+
static SentryOptions capturedOptions;
90+
@Override
91+
public void configure(SentryOptions options) {
92+
capturedOptions = options;
93+
}
94+
}
95+
5396
}

0 commit comments

Comments
 (0)