Created
May 16, 2019 14:00
-
-
Save efenderbosch/445381b00c8e2196e7ac94932a0f5f63 to your computer and use it in GitHub Desktop.
datadog appender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DatadogApiKeyPrefixEncoder extends EncoderBase<ILoggingEvent> { | |
private static final byte[] EMPTY = new byte[0]; | |
private final byte[] apiKey; | |
@SuppressFBWarnings({"EI_EXPOSE_REP2", "WEM_WEAK_EXCEPTION_MESSAGING"}) | |
public DatadogApiKeyPrefixEncoder(byte[] apiKey) { | |
if (apiKey == null || apiKey.length == 0) { | |
throw new IllegalArgumentException("DataDog API key must not be empty."); | |
} | |
this.apiKey = apiKey; | |
} | |
@Override | |
public byte[] headerBytes() { | |
return EMPTY; | |
} | |
@Override | |
@SuppressFBWarnings("EI_EXPOSE_REP") | |
public byte[] encode(ILoggingEvent event) { | |
return apiKey; | |
} | |
@Override | |
public byte[] footerBytes() { | |
return EMPTY; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DatadogAppender extends LogstashTcpSocketAppender { | |
private static final String DATADOG_DESTINATION_WTIH_SSL = "intake.logs.datadoghq.com:10516"; | |
private final ObjectNode customFields = JsonNodeFactory.instance.objectNode(); | |
@SuppressFBWarnings("IS2_INCONSISTENT_SYNC") | |
private byte[] apiKey; | |
public DatadogAppender() { | |
super(); | |
// Enable SSL by default | |
if (getSsl() == null) { | |
setSsl(new SSLConfiguration()); | |
} | |
} | |
public void setApiKey(@Nonnull String apiKey) { | |
String padded = apiKey.trim() + " "; | |
this.apiKey = padded.getBytes(StandardCharsets.UTF_8); | |
} | |
public void setHost(String host) { | |
customFields.put("host", host); | |
} | |
public void setSource(String source) { | |
customFields.put("source", source); | |
} | |
public void setService(String service) { | |
customFields.put("service", service); | |
} | |
public void setEnv(String env) { | |
customFields.put("env", env); | |
} | |
public void setAws(boolean aws) { | |
if (aws) { | |
setHost(EC2MetadataUtils.getInstanceId()); | |
} | |
} | |
public void setVersion(String version) { | |
customFields.put("version", version); | |
} | |
@Override | |
public synchronized void start() { | |
if (isStarted()) return; | |
if (getDestinations().isEmpty()) { | |
addDestination(DATADOG_DESTINATION_WTIH_SSL); | |
} | |
if (!customFields.hasNonNull("source")) { | |
customFields.put("source", "java"); | |
} | |
LogstashEncoder encoder = new LogstashEncoder(); | |
encoder.setCustomFields(customFields.toString()); | |
encoder.setPrefix(new DatadogApiKeyPrefixEncoder(apiKey)); | |
setEncoder(encoder); | |
super.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment