Skip to content

Commit ad9cd7c

Browse files
feat/java docs for sdk entry points
1 parent 09a4117 commit ad9cd7c

8 files changed

Lines changed: 137 additions & 62 deletions

File tree

.idea/workspace.xml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

traceo-sdk-core/src/main/java/com/traceo/sdk/builder/CoreBuilder.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.traceo.sdk.builder;
22

3+
/**
4+
* Base class for every client builders.
5+
*
6+
* @param <Subclass> Client builder.
7+
* @param <ConfigurationType> Client configuration class extended by ClientCoreConfiguration.
8+
*/
39
public abstract class CoreBuilder<Subclass extends CoreBuilder, ConfigurationType extends ClientCoreConfiguration> {
410

511
protected ConfigurationType configuration;
@@ -16,21 +22,45 @@ public final ConfigurationType build() {
1622
return configuration;
1723
}
1824

25+
/**
26+
* Set api key generated for this project.
27+
*
28+
* @param apiKey
29+
* @return This builder for method calling.
30+
*/
1931
public final Subclass withApiKey(String apiKey) {
2032
configuration.setApiKey(apiKey);
2133
return getSubclass();
2234
}
2335

36+
/**
37+
* Set host on which Traceo Platform is currently running.
38+
*
39+
* @param host in format [protocol]://[domain]:[port]
40+
* @return This builder for method calling.
41+
*/
2442
public final Subclass withHost(String host) {
2543
configuration.setHost(host);
2644
return getSubclass();
2745
}
2846

47+
/**
48+
* If false then client is not initialized. None of the captured exception are send to Traceo Platform.
49+
*
50+
* @param enabled
51+
* @return This builder for method calling.
52+
*/
2953
public final Subclass withEnabled(boolean enabled) {
3054
configuration.setEnabled(enabled);
3155
return getSubclass();
3256
}
3357

58+
/**
59+
* Set to true if you want to check internal logs for SDK.
60+
*
61+
* @param enabled
62+
* @return This builder for method calling.
63+
*/
3464
public final Subclass withDebug(boolean enabled) {
3565
configuration.setDebug(enabled);
3666
return getSubclass();

traceo-sdk-core/src/main/java/com/traceo/sdk/client/CoreClient.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ public static synchronized <T extends ClientCoreConfiguration> void init(T clien
3434
LOGGER.log("You run Traceo SDK with enabled debug option. All internal messages for this SDK will be visible to you.");
3535
}
3636

37-
// if (!initSDK()) {
38-
// return;
39-
// }
40-
4137
LOGGER.log("Traceo SDK has been initialized.");
4238

4339
asyncTransport = new HttpAsyncClient();
44-
incidentHandler = new IncidentHandler();
40+
incidentHandler = new IncidentHandler(asyncTransport);
4541

4642
shutdownHook();
4743
} else {

traceo-sdk-core/src/main/java/com/traceo/sdk/incident/IncidentHandler.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
11
package com.traceo.sdk.incident;
22

3+
import com.traceo.sdk.DefaultRequest;
4+
import com.traceo.sdk.EventCallback;
35
import com.traceo.sdk.TraceoIncident;
46
import com.traceo.sdk.TraceoRuntimePlatform;
7+
import com.traceo.sdk.http.HttpAsyncClient;
8+
import com.traceo.sdk.logging.ClientLogger;
59
import com.traceo.sdk.utils.ThrowableUtils;
610

11+
import static com.traceo.sdk.client.CoreClient.getConfigs;
12+
713
public class IncidentHandler {
14+
private final static ClientLogger LOGGER = new ClientLogger(IncidentHandler.class);
15+
16+
private static HttpAsyncClient httpAsyncClient;
17+
18+
public IncidentHandler(HttpAsyncClient httpClient) {
19+
this.httpAsyncClient = httpClient;
20+
}
821

9-
public IncidentHandler() {}
22+
public void catchException(Throwable throwable, String message, EventCallback<TraceoIncident> callback) {
23+
if (!getConfigs().isEnabled()) {
24+
LOGGER.log("Traceo client is disabled. Use withEnabled(true) to enabled exceptions scrapping.");
25+
return;
26+
}
27+
28+
if (throwable == null) {
29+
LOGGER.log("Exception has been passed as null.");
30+
return;
31+
}
32+
33+
TraceoIncident traceoIncident = processIncident(throwable, message);
34+
if (callback != null) {
35+
callback.run(traceoIncident);
36+
}
37+
38+
DefaultRequest<TraceoIncident> request = new DefaultRequest<>();
39+
request.setContent(traceoIncident);
40+
request.setEndpoint("/incident");
41+
42+
try {
43+
httpAsyncClient.executeAsync(request);
44+
} catch (Throwable e) {
45+
LOGGER.error("Failed to send exception.", e);
46+
}
47+
}
1048

11-
public TraceoIncident processIncident(Throwable throwable, String customMessage) {
49+
private static TraceoIncident processIncident(Throwable throwable, String customMessage) {
1250
TraceoIncident traceoIncident = new TraceoIncident();
1351

1452
String stacktrace = ThrowableUtils.stacktraceToString(throwable);
Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
11
package com.traceo.sdk.client;
22

3-
import com.traceo.sdk.DefaultRequest;
43
import com.traceo.sdk.EventCallback;
54
import com.traceo.sdk.TraceoIncident;
6-
import com.traceo.sdk.logging.ClientLogger;
75

8-
public class TraceoClient extends CoreClient<TraceoClientConfiguration> {
9-
private final static ClientLogger LOGGER = new ClientLogger(TraceoClient.class);
6+
/**
7+
* Entry point for accessing Traceo SDK.
8+
*/
9+
public final class TraceoClient extends CoreClient<TraceoClientConfiguration> {
1010

11-
protected TraceoClient() {}
11+
private TraceoClient() {}
1212

13-
public static void catchException(Throwable e) {
14-
catchException(e, null);
13+
/**
14+
* Catch the exception.
15+
* @param throwable Exception.
16+
*/
17+
public static void catchException(Throwable throwable) {
18+
incidentHandler.catchException(throwable, null, null);
1519
}
1620

17-
public static void catchException(String message, Throwable e) {
18-
catchException(e, message, null);
21+
/**
22+
* Catch the exception.
23+
* @param message Custom message instead of eq. java.lang.NullPointerException.
24+
* @param throwable Exception.
25+
*/
26+
public static void catchException(String message, Throwable throwable) {
27+
incidentHandler.catchException(throwable, message, null);
1928
}
2029

21-
public static void catchException(Throwable e, EventCallback<TraceoIncident> callback) {
22-
catchException(e, null, callback);
30+
/**
31+
* Catch the exception.
32+
* @param throwable Exception.
33+
* @param callback The callback to provide operations on already prepared TraceoIncident.
34+
*/
35+
public static void catchException(Throwable throwable, EventCallback<TraceoIncident> callback) {
36+
incidentHandler.catchException(throwable, null, callback);
2337
}
2438

25-
public static void catchException(Throwable e, String message, EventCallback<TraceoIncident> callback) {
26-
if (!getConfigs().isEnabled()) {
27-
LOGGER.log("Traceo client is disabled. Use withEnabled(true) to enabled exceptions scrapping.");
28-
return;
29-
}
30-
31-
TraceoIncident traceoIncident = incidentHandler.processIncident(e, message);
32-
if (callback != null) {
33-
callback.run(traceoIncident);
34-
}
35-
36-
DefaultRequest<TraceoIncident> request = new DefaultRequest<>();
37-
request.setContent(traceoIncident);
38-
request.setEndpoint("/incident");
39-
40-
asyncTransport.executeAsync(request);
39+
/**
40+
* Catch the exception.
41+
* @param throwable Exception.
42+
* @param message Custom message instead of eq. java.lang.NullPointerException.
43+
* @param callback The callback to provide operations on already prepared TraceoIncident.
44+
*/
45+
public static void catchException(Throwable throwable, String message, EventCallback<TraceoIncident> callback) {
46+
incidentHandler.catchException(throwable, message, callback);
4147
}
4248
}

traceo-sdk/src/main/java/com/traceo/sdk/client/TraceoClientBuilder.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
import com.traceo.sdk.builder.CoreBuilder;
44

5+
/**
6+
* Builder for creating configuration for {@link com.traceo.sdk.client.TraceoClient}.
7+
*/
58
public class TraceoClientBuilder extends CoreBuilder<TraceoClientBuilder, TraceoClientConfiguration> {
69
protected TraceoClientBuilder() {
710
super(new TraceoClientConfiguration());
811
}
912

13+
/**
14+
* Create new instance of this builder with default configuration for each option.
15+
* @return Builder instance
16+
*/
1017
public static TraceoClientBuilder standard() {
1118
return new TraceoClientBuilder();
1219
}
@@ -15,16 +22,23 @@ public static TraceoClientConfiguration defaultClient() {
1522
return standard().build();
1623
}
1724

18-
public final TraceoClientBuilder withMetricsCollection(boolean collect) {
19-
configuration.setCollectMetrics(collect);
20-
return this;
21-
}
25+
/**
26+
* Set to false if you SDK shouldn't collect any default metrics. It does not affect for OpenTelemetry.
27+
* @param collect
28+
* @return
29+
*/
30+
// public final TraceoClientBuilder withMetricsCollection(boolean collect) {
31+
// configuration.setCollectMetrics(collect);
32+
// return this;
33+
// }
2234

23-
public final TraceoClientBuilder withOfflineMode(boolean isOffline) {
24-
configuration.setOffline(isOffline);
25-
return this;
26-
}
2735

36+
/**
37+
* Set custom value for export default metrics interval.
38+
*
39+
* @param ms value provided in milliseconds
40+
* @return
41+
*/
2842
public final TraceoClientBuilder withExportIntervalMs(int ms) {
2943
configuration.setExportIntervalMillis(ms);
3044
return this;

traceo-sdk/src/main/java/com/traceo/sdk/client/TraceoClientConfiguration.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ public class TraceoClientConfiguration extends ClientCoreConfiguration {
66

77
private boolean collectMetrics = false;
88

9-
private boolean offline = false;
10-
119
private int exportIntervalMillis = 15000;
1210

1311
public TraceoClientConfiguration() {}
@@ -20,13 +18,6 @@ public void setCollectMetrics(boolean collectMetrics) {
2018
this.collectMetrics = collectMetrics;
2119
}
2220

23-
public boolean isOffline() {
24-
return offline;
25-
}
26-
27-
public void setOffline(boolean offline) {
28-
this.offline = offline;
29-
}
3021

3122
public int getExportIntervalMillis() {
3223
return exportIntervalMillis;

traceo-sdk/src/test/java/com/traceo/sdk/TraceoIncidentHandlerTest.java renamed to traceo-sdk/src/test/java/com/traceo/sdk/IncidentHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import org.junit.Before;
77
import org.junit.Test;
88

9-
public class TraceoIncidentHandlerTest {
9+
public class IncidentHandlerTest {
1010

1111
@Before
1212
public void setUp() {

0 commit comments

Comments
 (0)