-
Notifications
You must be signed in to change notification settings - Fork 38
/
AbstractSenderBuilder.java
112 lines (97 loc) · 3.26 KB
/
AbstractSenderBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.newrelic.telemetry;
import com.newrelic.telemetry.http.HttpPoster;
import com.newrelic.telemetry.util.Utils;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
public abstract class AbstractSenderBuilder<B extends AbstractSenderBuilder<B>> {
protected String apiKey;
protected HttpPoster httpPoster;
protected URL sendUrl;
protected boolean auditLoggingEnabled = false;
protected String secondaryUserAgent;
protected URL getOrDefaultSendUrl() {
if (sendUrl != null) {
return sendUrl;
}
try {
return constructUrlWithHost(URI.create(getDefaultUrl()));
} catch (MalformedURLException e) {
throw new UncheckedIOException("Bad hardcoded URL", e);
}
}
protected abstract String getDefaultUrl();
protected abstract String getBasePath();
protected URL constructUrlWithHost(URI hostUri) throws MalformedURLException {
return hostUri.resolve(getBasePath()).toURL();
}
/**
* Set a URI to override the default ingest endpoint.
*
* @param uriOverride The scheme, host, and port that should be used for the Spans API endpoint.
* The path component of this parameter is unused.
* @return the Builder
* @throws MalformedURLException This is thrown when the provided URI is malformed.
*/
public B uriOverride(URI uriOverride) throws MalformedURLException {
sendUrl = constructUrlWithHost(uriOverride);
return self();
}
public abstract Object build();
@SuppressWarnings("unchecked")
protected final B self() {
return (B) this;
}
/**
* Turns on audit logging. Payloads sent will be logged at the DEBUG level. Please note that if
* your payloads contain sensitive information, that information will be logged wherever your logs
* are configured.
*/
public B enableAuditLogging() {
this.auditLoggingEnabled = true;
return self();
}
/**
* Provide your New Relic Insights Insert API key
*
* @see <a
* href="https://docs.newrelic.com/docs/apis/getting-started/intro-apis/understand-new-relic-api-keys#user-api-key">New
* Relic API Keys</a>
*/
public B apiKey(String apiKey) {
this.apiKey = apiKey;
return self();
}
/**
* Provide an implementation for HTTP POST. {@link #build()} will throw if an implementation is
* not provided or this method is not called.
*/
public B httpPoster(HttpPoster httpPoster) {
this.httpPoster = httpPoster;
return self();
}
/**
* By default, the DEFAULT_URL defined in specific subclasses is used. Otherwise uses the provided
* {@code traceUrl}
*
* @deprecated Use the {@link #uriOverride(URI)} method instead.
*/
public B traceUrl(URL traceUrl) {
sendUrl = traceUrl;
return self();
}
/**
* Provide additional user agent information. The product is required to be non-null and
* non-empty. The version is optional, although highly recommended.
*/
public B secondaryUserAgent(String product, String version) {
Utils.verifyNonBlank(product, "Product cannot be null or empty in the secondary user-agent.");
if (version == null || version.isEmpty()) {
secondaryUserAgent = product;
} else {
secondaryUserAgent = product + "/" + version;
}
return self();
}
}