Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Chédru committed Jan 31, 2018
0 parents commit 8de3b44
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
@@ -0,0 +1,11 @@
.idea
target
atlassian-ide-plugin.xml
logs
*.iml
*.ipr
#Eclipse-specific
.settings/
.classpath
.project
tmp/*
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
language: java
jdk: oraclejdk8
install: true
script: . .travis/script.sh
15 changes: 15 additions & 0 deletions .travis/script.sh
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

if [ "${TRAVIS_EVENT_TYPE}" == push ] &&
echo "${TRAVIS_TAG}" | egrep '^[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?$'
then
# the build is triggered by a tag push, and the tag looks like
# a version number: proceed with release
echo ${GPG_SECRET_KEY} | base64 --decode | gpg --import
echo ${GPG_OWNERTRUST} | base64 --decode | gpg --import-ownertrust
mvn versions:set -DnewVersion=${TRAVIS_TAG}
mvn -s .travis/settings.xml -Prelease deploy
else
# this is a regular build
mvn install
fi
20 changes: 20 additions & 0 deletions .travis/settings.xml
@@ -0,0 +1,20 @@
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>ossrh</id>
<username>${env.SONATYPE_USERNAME}</username>
<password>${env.SONATYPE_PASSWORD}</password>
</server>
</servers>
<profiles>
<profile>
<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.passphrase>${env.GPG_PASSPHRASE}</gpg.passphrase>
</properties>
</profile>
</profiles>
</settings>
81 changes: 81 additions & 0 deletions README.md
@@ -0,0 +1,81 @@
# Dropwizard Raven

[![Build Status](https://travis-ci.org/dhatim/dropwizard-raven.png?branch=master)](https://travis-ci.org/dhatim/dropwizard-raven)
[![Coverage Status](https://coveralls.io/repos/github/dhatim/dropwizard-raven/badge.svg?branch=master)](https://coveralls.io/github/dhatim/dropwizard-raven?branch=master)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.dhatim/dropwizard-raven/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.dhatim/dropwizard-raven)
[![Javadocs](http://www.javadoc.io/badge/org.dhatim/dropwizard-raven.svg)](http://www.javadoc.io/doc/org.dhatim/dropwizard-raven)

Dropwizard integration for error logging to [Sentry](https://sentry.io).

## Usage

The Dropwizard Raven provides an `AppenderFactory` which is automatically registered in Dropwizard and will send errors to Sentry.

### Logging startup errors

In order to log startup errors (i.e. before the `RavenAppenderFactory` has been properly initialized), the Dropwizard application has to run the `RavenBootstrap.bootstrap()` in its `main` method and set a custom `UncaughtExceptionHandler` for the main thread.

```java
public static void main(String[] args) throws Exception {
RavenBootstrap.bootstrap(DSN);
Thread.currentThread().setUncaughtExceptionHandler(UncaughtExceptionHandlers.systemExit());

new MyDropwizardApplication().run(args);
}
```

### Configuration

The Logback `SentryAppender` can be configured using the provided `RavenConfiguration` class which basically mirrors the appender configuration outlined in [raven-logback](https://github.com/getsentry/raven-java/tree/master/raven-logback).

Include the `raven` appender in your `config.yml`:

```yaml
appenders:
- type: raven
threshold: ERROR
dsn: https://user:pass@sentry.io/appid
environment: production
extraTags: foo,bar,baz
ravenFactory: com.example.RavenFactory
release: 1.0.0
serverName: 10.0.0.1
tags: tag:value,tag2:value
```

### Configuration Settings

| Setting | Default | Description | Example Value |
|---|---|---|---|
| `threshold` | ALL | The log level to configure to send to Sentry | `ERROR` |
| `dsn` | | Data Source Name - `'https://{PUBLIC_KEY}:{SECRET_KEY}@sentry.io/{PROJECT_ID}'` | `https://foo:bar@sentry.io/12345` |
| `environment` | [empty] | The environment your application is running in | `production` |
| `extraTags` | [empty] | `Additional tag names to be extracted from MDC when using SLF4J` | `foo,bar,baz` |
| `ravenFactory` | [empty] | Specify a custom [`RavenFactory`](https://github.com/getsentry/raven-java/blob/master/raven/src/main/java/com/getsentry/raven/RavenFactory.java) class | `com.example.RavenFactory` |
| `release` | [empty] | The release version of your application | `1.0.0` |
| `serverName` | [empty] | Override the server name (rather than looking it up dynamically) | `10.0.0.1` |
| `tags` | [empty] | Additional tags to be sent with errors | `tag1:value1,tag2:value2` |

## Maven Artifacts

This project is available in the [Central Repository](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.dhatim%22%20AND%20a%3A%22dropwizard-raven%22). To add it to your project simply add the following dependency to your POM:

```xml
<dependency>
<groupId>org.dhatim</groupId>
<artifactId>dropwizard-raven</artifactId>
<version>1.2.0-1</version>
</dependency>
```

## Support

Please file bug reports and feature requests in [GitHub issues](https://github.com/dhatim/dropwizard-raven/issues).

## Acknowledgements

Thanks to [gini](https://github.com/gini) for [dropwizard-gelf](https://github.com/gini/dropwizard-gelf) from which much of the original implementation was derived.

## Copyright

Copyright (c) 2014-2016 Tradier Inc. See [LICENSE](LICENSE.md) for detail.
96 changes: 96 additions & 0 deletions pom.xml
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.dhatim</groupId>
<artifactId>root-oss</artifactId>
<version>9.1.0</version>
</parent>
<groupId>org.dhatim</groupId>
<artifactId>dropwizard-correlation-id</artifactId>
<version>0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>Dropwizard bundle for request correlation ids</description>
<url>https://github.com/dhatim/dropwizard-correlation-id</url>
<licenses>
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<email>dev-oss@dhatim.com</email>
<organization>Dhatim</organization>
<organizationUrl>http://dhatim.com/</organizationUrl>
</developer>
</developers>
<scm>
<connection>scm:git:git@github.com:dhatim/dropwizard-correlation-id.git</connection>
<developerConnection>scm:git:git@github.com:dhatim/dropwizard-correlation-id.git</developerConnection>
<url>git@github.com:dhatim/dropwizard-correlation-id.git</url>
</scm>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,40 @@
package org.dhatim.dropwizard.correlationid;

import io.dropwizard.Configuration;
import io.dropwizard.ConfiguredBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import java.util.EnumSet;
import java.util.function.Function;
import javax.servlet.DispatcherType;

public class CorrelationIdBundle<C extends Configuration> implements ConfiguredBundle<C> {

private final Function<C, CorrelationIdConfiguration> configurationSupplier;

public CorrelationIdBundle(Function<C, CorrelationIdConfiguration> configurationSupplier) {
this.configurationSupplier = configurationSupplier;
}

public static <C extends Configuration> CorrelationIdBundle<C> withConfigurationSupplier(Function<C, CorrelationIdConfiguration> configurationSupplier) {
return new CorrelationIdBundle(configurationSupplier);
}

public static <C extends Configuration> CorrelationIdBundle<C> getDefault() {
return withConfigurationSupplier(c -> new CorrelationIdConfiguration());
}

@Override
public void initialize(Bootstrap<?> bootstrap) {

}

@Override
public void run(C configuration, Environment environment) throws Exception {
CorrelationIdConfiguration correlationIdConfiguration = configurationSupplier.apply(configuration);
environment.servlets()
.addFilter("correlation-id-servlet-filter", new CorrelationIdServletFilter(correlationIdConfiguration))
.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*");
}

}
@@ -0,0 +1,28 @@
package org.dhatim.dropwizard.correlationid;

import java.io.IOException;
import java.util.Optional;
import java.util.UUID;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import org.slf4j.MDC;

public class CorrelationIdClientFilter implements ClientRequestFilter {

private final CorrelationIdConfiguration configuration;

public CorrelationIdClientFilter() {
this(new CorrelationIdConfiguration());
}

public CorrelationIdClientFilter(CorrelationIdConfiguration configuration) {
this.configuration = configuration;
}

@Override
public void filter(ClientRequestContext requestContext) throws IOException {
String correlationId = Optional.ofNullable(MDC.get(configuration.mdcKey)).orElseGet(() -> UUID.randomUUID().toString());
requestContext.getHeaders().add(configuration.headerName, correlationId);
}

}
@@ -0,0 +1,21 @@
package org.dhatim.dropwizard.correlationid;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Optional;

public class CorrelationIdConfiguration {

public final String headerName;
public final String mdcKey;

public CorrelationIdConfiguration() {
this(null, null);
}

public CorrelationIdConfiguration(@JsonProperty("headerName") String headerName,
@JsonProperty("mdcKey") String mdcKey) {
this.headerName = Optional.ofNullable(headerName).orElse("X-Correlation-Id");
this.mdcKey = Optional.ofNullable(mdcKey).orElse("CorrelationId");
}

}

0 comments on commit 8de3b44

Please sign in to comment.