forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentry.java
More file actions
295 lines (265 loc) · 10.2 KB
/
Copy pathSentry.java
File metadata and controls
295 lines (265 loc) · 10.2 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package io.sentry;
import io.sentry.config.Lookup;
import io.sentry.config.ResourceLoader;
import io.sentry.context.Context;
import io.sentry.dsn.Dsn;
import io.sentry.event.Breadcrumb;
import io.sentry.event.Event;
import io.sentry.event.EventBuilder;
import io.sentry.event.User;
import io.sentry.util.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Sentry provides easy access to a statically stored {@link SentryClient} instance.
*/
public final class Sentry {
private static final Logger logger = LoggerFactory.getLogger(Sentry.class);
/**
* A synchronization guard of the stored client. Not using the class as the guard because that could theoretically
* deadlock with 3rd party code if it also synced on the class.
*/
private static final Object STORED_CLIENT_ACCESS = new Object();
/**
* The most recently constructed {@link SentryClient} instance, used by static helper
* methods like {@link Sentry#capture(Event)}.
*/
private static SentryClient storedClient = null;
/**
* Optional override for the default resource loader used to look for properties.
*
* @deprecated This was a hack to be able to inject the resource loader into the static Lookup initialization.
* This is no longer required due to {@link Lookup} being configurable and passed throughout the classes as an
* instance.
*/
@Deprecated
private static ResourceLoader resourceLoader;
/**
* Hide constructor.
*/
private Sentry() {
}
/**
* Initialize and statically store a {@link SentryClient} by looking up
* a {@link Dsn} and automatically choosing a {@link SentryClientFactory}.
* <p>
* This uses a default lookup instance, use {@link #init(SentryOptions)} if you need to pass a specially
* configured lookup.
*
* @return SentryClient
* @see #init(SentryOptions)
*/
public static SentryClient init() {
return init((String) null);
}
/**
* Initialize and statically store a {@link SentryClient} by looking up
* a {@link Dsn} and using the provided {@link SentryClientFactory}.
* <p>
* This uses a default lookup instance, use {@link #init(SentryOptions)} if you need to pass a specially
* configured lookup.
*
* @param sentryClientFactory SentryClientFactory to use.
* @return SentryClient
* @see #init(SentryOptions)
*/
public static SentryClient init(@Nullable SentryClientFactory sentryClientFactory) {
return init(SentryOptions.from(Lookup.getDefault(), null, sentryClientFactory));
}
/**
* Initialize and statically store a {@link SentryClient} by using the provided
* {@link Dsn} and automatically choosing a {@link SentryClientFactory}.
* <p>
* This uses a default lookup instance, use {@link #init(SentryOptions)} if you need to pass a specially
* configured lookup.
*
* @param dsn Data Source Name of the Sentry server.
* @return SentryClient
* @see #init(SentryOptions)
*/
public static SentryClient init(@Nullable String dsn) {
return init(SentryOptions.defaults(dsn));
}
/**
* Initialize and statically store a {@link SentryClient} by using the provided
* {@link Dsn} and {@link SentryClientFactory}.
* <p>
* Note that the Dsn or SentryClientFactory may be null, at which a best effort attempt
* is made to look up or choose the best value(s).
* <p>
* This uses a default lookup instance, use {@link #init(SentryOptions)} if you need to pass a specially
* configured lookup.
*
* @param dsn Data Source Name of the Sentry server.
* @param sentryClientFactory SentryClientFactory to use.
* @return SentryClient
* @see #init(SentryOptions)
*/
public static SentryClient init(@Nullable String dsn, @Nullable SentryClientFactory sentryClientFactory) {
SentryOptions options = SentryOptions.defaults(dsn);
options.setSentryClientFactory(sentryClientFactory);
return init(options);
}
/**
* Initializes a new Sentry client from the provided context.
*
* The canonical way of using this method is:
* {@link Lookup} lookup = ... obtain or construct the instance of this class to be able to locate Sentry config
* String dsn = ... obtain the Sentry data source name or leave null for lookup in the configuration
* SentryClient client =
* Sentry.init({@link SentryOptions}.{@link SentryOptions#from(Lookup, String) from(lookup, dsn))};
* If you want to rely on the default mechanisms to obtain the configuration, you can also use the
* {@link SentryOptions#defaults()} method which will use the default way of obtaining the configuration and DSN
* obtained from the configuration.
*
* @param sentryOptions the context using with to create the client
* @return the Sentry client
*/
public static SentryClient init(SentryOptions sentryOptions) {
// Hack to allow Lookup.java access to a different resource locator before its static initializer runs.
// v2: Lookup won't be static and this hack will be removed.
// ResourceLocator will ba passed to Lookup upon instantiation
Sentry.resourceLoader = sentryOptions.getResourceLoader();
// make sure to use the DSN configured in the options instead of the one that the factory can find in its
// lookup
SentryClient client = sentryOptions.getSentryClientFactory().createClient(sentryOptions.getDsn());
setStoredClient(client);
return client;
}
/**
* Returns {@code true} if the Sentry object has been already initialized.
* <p>
* Note that this method will return true even when {@link SentryClient} was disabled passing an empty value
* as {@link Dsn}
*
* @return {@code true} if stored {@link SentryClient} is not null
*/
public static boolean isInitialized() {
return storedClient != null;
}
/**
* Returns the last statically stored {@link SentryClient} instance. If no instance
* is already stored, an attempt will be made to create a {@link SentryClient} from the configuration
* found in the environment.
*
* @return statically stored {@link SentryClient} instance, or null.
*/
public static SentryClient getStoredClient() {
synchronized (STORED_CLIENT_ACCESS) {
if (isInitialized()) {
return storedClient;
}
init(SentryOptions.defaults());
}
return storedClient;
}
/**
* The {@link ResourceLoader} used to lookup properties.
*
* @return {@link ResourceLoader}.
* @deprecated Using this field is discouraged in favour of using the configurable {@link Lookup} with
* {@link io.sentry.config.provider.ResourceLoaderConfigurationProvider}.
*/
@Deprecated
public static ResourceLoader getResourceLoader() {
return resourceLoader;
}
/**
* Returns the {@link Context} on the statically stored {@link SentryClient}.
*
* @return the {@link Context} on the statically stored {@link SentryClient}.
*/
public static Context getContext() {
return getStoredClient().getContext();
}
/**
* Clears the current context.
*/
public static void clearContext() {
getStoredClient().clearContext();
}
/**
* Set the statically stored {@link SentryClient} instance.
*
* @param client {@link SentryClient} instance to store.
*/
public static void setStoredClient(SentryClient client) {
synchronized (STORED_CLIENT_ACCESS) {
if (isInitialized()) {
logger.warn("Overwriting statically stored SentryClient instance {} with {}.",
storedClient, client);
}
storedClient = client;
}
}
/**
* Send an Event using the statically stored {@link SentryClient} instance.
*
* @param event Event to send to the Sentry server.
*/
public static void capture(Event event) {
getStoredClient().sendEvent(event);
}
/**
* Sends an exception (or throwable) to the Sentry server using the statically stored
* {@link SentryClient} instance.
* <p>
* The exception will be logged at the {@link Event.Level#ERROR} level.
*
* @param throwable exception to send to Sentry.
*/
public static void capture(Throwable throwable) {
getStoredClient().sendException(throwable);
}
/**
* Sends a message to the Sentry server using the statically stored {@link SentryClient} instance.
* <p>
* The message will be logged at the {@link Event.Level#INFO} level.
*
* @param message message to send to Sentry.
*/
public static void capture(String message) {
getStoredClient().sendMessage(message);
}
/**
* Builds and sends an {@link Event} to the Sentry server using the statically stored
* {@link SentryClient} instance.
*
* @param eventBuilder {@link EventBuilder} to send to Sentry.
*/
public static void capture(EventBuilder eventBuilder) {
getStoredClient().sendEvent(eventBuilder);
}
/**
* Record a {@link Breadcrumb}.
*
* @param breadcrumb Breadcrumb to record.
* @deprecated use {@link Sentry#getContext()} and then {@link Context#recordBreadcrumb(Breadcrumb)}.
*/
@Deprecated
public static void record(Breadcrumb breadcrumb) {
getStoredClient().getContext().recordBreadcrumb(breadcrumb);
}
/**
* Set the {@link User} in the current context.
*
* @param user User to store.
* @deprecated use {@link Sentry#getContext()} and then {@link Context#setUser(User)}.
*/
@Deprecated
public static void setUser(User user) {
getStoredClient().getContext().setUser(user);
}
/**
* Close the stored {@link SentryClient}'s connections and remove it from static storage.
*/
public static void close() {
synchronized (STORED_CLIENT_ACCESS) {
if (!isInitialized()) {
return;
}
storedClient.closeConnection();
storedClient = null;
}
}
}