|
| 1 | +package com.prashantchaubey.exceptionlessclient; |
| 2 | + |
| 3 | +import com.prashantchaubey.exceptionlessclient.configuration.ConfigurationManager; |
| 4 | +import com.prashantchaubey.exceptionlessclient.models.Event; |
| 5 | +import com.prashantchaubey.exceptionlessclient.models.EventPluginContext; |
| 6 | +import com.prashantchaubey.exceptionlessclient.models.PluginContext; |
| 7 | +import com.prashantchaubey.exceptionlessclient.models.UserDescription; |
| 8 | +import com.prashantchaubey.exceptionlessclient.models.enums.EventPropertyKey; |
| 9 | +import com.prashantchaubey.exceptionlessclient.models.enums.EventType; |
| 10 | +import com.prashantchaubey.exceptionlessclient.models.submission.SubmissionResponse; |
| 11 | +import com.prashantchaubey.exceptionlessclient.plugins.EventPluginManager; |
| 12 | +import lombok.Builder; |
| 13 | +import lombok.Getter; |
| 14 | + |
| 15 | +import java.time.LocalDate; |
| 16 | +import java.util.Timer; |
| 17 | +import java.util.TimerTask; |
| 18 | +import java.util.function.Consumer; |
| 19 | + |
| 20 | +@Builder(builderClassName = "ExceptionlessClientInternalBuilder") |
| 21 | +@Getter |
| 22 | +public class ExceptionlessClient { |
| 23 | + private static final int UPDATE_SETTINGS_TIMER_INITIAL_DELAY = 5000; |
| 24 | + |
| 25 | + private ConfigurationManager configurationManager; |
| 26 | + |
| 27 | + // lombok ignored fields |
| 28 | + private EventPluginManager $eventPluginManager; |
| 29 | + private Timer $updateSettingsTimer = new Timer(); |
| 30 | + |
| 31 | + public static ExceptionlessClient from(String apiKey, String serverUrl) { |
| 32 | + return ExceptionlessClient.builder() |
| 33 | + .configurationManager(ConfigurationManager.from(apiKey, serverUrl)) |
| 34 | + .build(); |
| 35 | + } |
| 36 | + |
| 37 | + public void submitException(Exception exception, Consumer<EventPluginContext> handler) { |
| 38 | + Event event = createException().build(); |
| 39 | + PluginContext pluginContext = PluginContext.builder().exception(exception).build(); |
| 40 | + submitEvent(EventPluginContext.builder().event(event).context(pluginContext).build(), handler); |
| 41 | + } |
| 42 | + |
| 43 | + private Event.EventBuilderImpl createException() { |
| 44 | + return createEvent().type(EventType.ERROR.value()); |
| 45 | + } |
| 46 | + |
| 47 | + public void submitUnhandledException( |
| 48 | + Exception exception, String submissionMethod, Consumer<EventPluginContext> handler) { |
| 49 | + Event event = createException().build(); |
| 50 | + PluginContext pluginContext = |
| 51 | + PluginContext.builder() |
| 52 | + .exception(exception) |
| 53 | + .markAsUnhandledError() |
| 54 | + .submissionMethod(submissionMethod) |
| 55 | + .build(); |
| 56 | + submitEvent(EventPluginContext.builder().event(event).context(pluginContext).build(), handler); |
| 57 | + } |
| 58 | + |
| 59 | + public void submitFeatureUsage(String feature, Consumer<EventPluginContext> handler) { |
| 60 | + Event event = createFeatureUsage(feature).build(); |
| 61 | + submitEvent(EventPluginContext.from(event), handler); |
| 62 | + } |
| 63 | + |
| 64 | + private Event.EventBuilderImpl createFeatureUsage(String feature) { |
| 65 | + return createEvent().type(EventType.USAGE.value()).source(feature); |
| 66 | + } |
| 67 | + |
| 68 | + public void submitLog(String message, Consumer<EventPluginContext> handler) { |
| 69 | + submitLog(message, null, null, handler); |
| 70 | + } |
| 71 | + |
| 72 | + public void submitLog(String message, String source, Consumer<EventPluginContext> handler) { |
| 73 | + submitLog(message, source, null, handler); |
| 74 | + } |
| 75 | + |
| 76 | + public void submitLog( |
| 77 | + String message, String source, String level, Consumer<EventPluginContext> handler) { |
| 78 | + Event event = createLog(message, source, level).build(); |
| 79 | + submitEvent(EventPluginContext.from(event), handler); |
| 80 | + } |
| 81 | + |
| 82 | + private Event.EventBuilderImpl createLog(String message, String source, String level) { |
| 83 | + if (source == null) { |
| 84 | + // Calling method |
| 85 | + source = Thread.currentThread().getStackTrace()[2].getMethodName(); |
| 86 | + } |
| 87 | + |
| 88 | + Event.EventBuilderImpl builder = |
| 89 | + createEvent().type(EventType.LOG.value()).source(source).message(message); |
| 90 | + if (level == null) { |
| 91 | + return builder; |
| 92 | + } |
| 93 | + |
| 94 | + return builder.property(EventPropertyKey.LOG_LEVEL.value(), level); |
| 95 | + } |
| 96 | + |
| 97 | + public void submitNotFound(String resource, Consumer<EventPluginContext> handler) { |
| 98 | + Event event = createNotFound(resource).build(); |
| 99 | + submitEvent(EventPluginContext.from(event), handler); |
| 100 | + } |
| 101 | + |
| 102 | + private Event.EventBuilderImpl createNotFound(String resource) { |
| 103 | + return createEvent().type(EventType.NOT_FOUND.value()).source(resource); |
| 104 | + } |
| 105 | + |
| 106 | + public void submitSessionStart(Consumer<EventPluginContext> handler) { |
| 107 | + Event event = createSessionStart().build(); |
| 108 | + submitEvent(EventPluginContext.from(event), handler); |
| 109 | + } |
| 110 | + |
| 111 | + private Event.EventBuilderImpl createSessionStart() { |
| 112 | + return createEvent().type(EventType.SESSION.value()); |
| 113 | + } |
| 114 | + |
| 115 | + private Event.EventBuilderImpl createEvent() { |
| 116 | + return Event.builder(configurationManager.getDataExclusions()).date(LocalDate.now()); |
| 117 | + } |
| 118 | + |
| 119 | + private void submitEvent( |
| 120 | + EventPluginContext eventPluginContext, Consumer<EventPluginContext> handler) { |
| 121 | + $eventPluginManager.run( |
| 122 | + eventPluginContext, |
| 123 | + evc -> { |
| 124 | + if (evc.getContext().isEventCancelled()) { |
| 125 | + return; |
| 126 | + } |
| 127 | + configurationManager.getQueue().enqueue(evc.getEvent()); |
| 128 | + if (evc.getEvent().getReferenceId() != null) { |
| 129 | + configurationManager |
| 130 | + .getLastReferenceIdManager() |
| 131 | + .setLast(evc.getEvent().getReferenceId()); |
| 132 | + } |
| 133 | + handler.accept(evc); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + public void submitSessionEnd(String sessionOrUserId) { |
| 138 | + configurationManager |
| 139 | + .getLog() |
| 140 | + .info(String.format("Submitting session end: %s", sessionOrUserId)); |
| 141 | + configurationManager.getSubmissionClient().sendHeartBeat(sessionOrUserId, true); |
| 142 | + } |
| 143 | + |
| 144 | + public void submitSessionHeartbeat(String sessionOrUserId) { |
| 145 | + configurationManager.submitSessionHeartbeat(sessionOrUserId); |
| 146 | + } |
| 147 | + |
| 148 | + public void updateEmailAndDescription( |
| 149 | + String referenceId, String email, String description, Consumer<SubmissionResponse> handler) { |
| 150 | + SubmissionResponse response = |
| 151 | + configurationManager |
| 152 | + .getSubmissionClient() |
| 153 | + .postUserDescription( |
| 154 | + referenceId, |
| 155 | + UserDescription.builder().description(description).emailAddress(email).build()); |
| 156 | + if (!response.isSuccess()) { |
| 157 | + configurationManager |
| 158 | + .getLog() |
| 159 | + .error( |
| 160 | + String.format( |
| 161 | + "Failed to submit user email and description for event: %s", referenceId)); |
| 162 | + } |
| 163 | + handler.accept(response); |
| 164 | + } |
| 165 | + |
| 166 | + public String getLastReferenceId() { |
| 167 | + return configurationManager.getLastReferenceIdManager().getLast(); |
| 168 | + } |
| 169 | + |
| 170 | + public static ExceptionlessClientBuilder builder() { |
| 171 | + return new ExceptionlessClientBuilder(); |
| 172 | + } |
| 173 | + |
| 174 | + public static class ExceptionlessClientBuilder extends ExceptionlessClientInternalBuilder { |
| 175 | + @Override |
| 176 | + public ExceptionlessClient build() { |
| 177 | + ExceptionlessClient client = super.build(); |
| 178 | + client.init(); |
| 179 | + |
| 180 | + return client; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private void init() { |
| 185 | + $updateSettingsTimer.schedule( |
| 186 | + new TimerTask() { |
| 187 | + @Override |
| 188 | + public void run() { |
| 189 | + configurationManager.getSettingsManager().updateSettingsThreadSafe(); |
| 190 | + } |
| 191 | + }, |
| 192 | + UPDATE_SETTINGS_TIMER_INITIAL_DELAY, |
| 193 | + configurationManager.getConfiguration().getUpdateSettingsWhenIdleInterval()); |
| 194 | + $eventPluginManager = |
| 195 | + EventPluginManager.builder().configurationManager(configurationManager).build(); |
| 196 | + } |
| 197 | +} |
0 commit comments