Skip to content

Commit

Permalink
Merge pull request #123 from jjcard/add-context-tags
Browse files Browse the repository at this point in the history
Make contextTags configurable
  • Loading branch information
ochedru authored Nov 16, 2023
2 parents 8e9d230 + 6de8517 commit 360f6b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ appenders:
| [`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` |
| [`inAppIncludes`](https://docs.sentry.io/platforms/java/configuration/#in-app-includes) | [empty] | List of package prefixes used by application code | `['com.example','com.foo']` |
| [`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']` |
| `contextTags` | [empty] | context tags names applied as Sentry tags to each event | `['contextTag1','contextTag2']` |

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).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public class SentryAppenderFactory extends AbstractAppenderFactory<ILoggingEvent
@JsonProperty
public String configurator = null;

@JsonProperty
public List<String> contextTags = null;

@Override
public Appender<ILoggingEvent> build(LoggerContext context,
String applicationName,
Expand All @@ -69,6 +72,7 @@ public Appender<ILoggingEvent> build(LoggerContext context,
Optional.ofNullable(serverName).ifPresent(options::setServerName);
Optional.ofNullable(inAppIncludes).ifPresent(inAppIncludes -> inAppIncludes.forEach(options::addInAppInclude));
Optional.ofNullable(inAppExcludes).ifPresent(inAppExcludes -> inAppExcludes.forEach(options::addInAppExclude));
Optional.ofNullable(contextTags).ifPresent(contextTags -> contextTags.forEach(options::addContextTag));
Optional.ofNullable(configurator).ifPresent(configurator -> {
try {
Class<?> klass = Class.forName(configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
import io.dropwizard.logging.common.async.AsyncLoggingEventAppenderFactory;
import io.dropwizard.logging.common.filter.ThresholdLevelFilterFactory;
import io.dropwizard.logging.common.layout.DropwizardLayoutFactory;
import io.sentry.SentryOptions;
import io.sentry.logback.SentryAppender;
import org.dhatim.dropwizard.sentry.SentryConfigurator;
import org.hamcrest.collection.IsIterableContainingInOrder;
import org.hamcrest.collection.IsMapContaining;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

public class SentryAppenderFactoryTest {

Expand Down Expand Up @@ -50,4 +55,42 @@ public void buildSentryAppenderShouldWorkWithValidConfiguration() {
assertThat(appender, instanceOf(SentryAppender.class));
}

@Test
void buildSentryAppenderFullConfiguration() {
SentryAppenderFactory factory = new SentryAppenderFactory();
factory.dsn = "https://user:[email protected]/id";
factory.environment = "test";
factory.tags = Map.of("tag1", "value1");
factory.release = "1.0.0";
factory.serverName = "10.0.0.1";
factory.inAppIncludes = List.of("com.example");
factory.inAppExcludes = List.of("com.thirdparty");
factory.contextTags = List.of("contextTag1");
factory.configurator = CaptureSentryConfigurator.class.getName();

Appender<ILoggingEvent> appender = factory.build(context, "", layoutFactory, levelFilterFactory, asyncAppenderFactory);
assertThat(appender, instanceOf(SentryAppender.class));

SentryOptions capturedOptions = CaptureSentryConfigurator.capturedOptions;
assertNotNull(capturedOptions);

assertEquals("https://user:[email protected]/id", capturedOptions.getDsn());
assertEquals("test", capturedOptions.getEnvironment());
assertEquals("1.0.0", capturedOptions.getRelease());
assertThat(capturedOptions.getContextTags(), IsIterableContainingInOrder.contains("contextTag1"));
assertEquals("10.0.0.1", capturedOptions.getServerName());
assertThat(capturedOptions.getTags(), IsMapContaining.hasEntry("tag1", "value1"));
assertThat(capturedOptions.getInAppIncludes(), IsIterableContainingInOrder.contains("com.example"));
assertThat(capturedOptions.getInAppExcludes(), IsIterableContainingInOrder.contains("com.thirdparty"));

}

protected static class CaptureSentryConfigurator implements SentryConfigurator {
static SentryOptions capturedOptions;
@Override
public void configure(SentryOptions options) {
capturedOptions = options;
}
}

}

0 comments on commit 360f6b9

Please sign in to comment.