|
| 1 | +package io.sentry.samples.console; |
| 2 | + |
| 3 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 4 | +import io.opentelemetry.api.trace.Span; |
| 5 | +import io.opentelemetry.api.trace.StatusCode; |
| 6 | +import io.opentelemetry.context.Scope; |
| 7 | +import io.sentry.Breadcrumb; |
| 8 | +import io.sentry.EventProcessor; |
| 9 | +import io.sentry.Hint; |
| 10 | +import io.sentry.ISentryLifecycleToken; |
| 11 | +import io.sentry.ISpan; |
| 12 | +import io.sentry.ITransaction; |
| 13 | +import io.sentry.Sentry; |
| 14 | +import io.sentry.SentryEvent; |
| 15 | +import io.sentry.SentryLevel; |
| 16 | +import io.sentry.SpanStatus; |
| 17 | +import io.sentry.protocol.Message; |
| 18 | +import io.sentry.protocol.User; |
| 19 | +import java.util.Collections; |
| 20 | + |
| 21 | +public class Main { |
| 22 | + |
| 23 | + public static void main(String[] args) throws InterruptedException { |
| 24 | + Sentry.init( |
| 25 | + options -> { |
| 26 | + // NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in |
| 27 | + // your Sentry project/dashboard |
| 28 | + options.setDsn( |
| 29 | + "https://[email protected]/5428563"); |
| 30 | + |
| 31 | + // All events get assigned to the release. See more at |
| 32 | + // https://docs.sentry.io/workflow/releases/ |
| 33 | + options. setRelease( "[email protected]+1"); |
| 34 | + |
| 35 | + // Modifications to event before it goes out. Could replace the event altogether |
| 36 | + options.setBeforeSend( |
| 37 | + (event, hint) -> { |
| 38 | + // Drop an event altogether: |
| 39 | + if (event.getTag("SomeTag") != null) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + return event; |
| 43 | + }); |
| 44 | + |
| 45 | + options.setBeforeSendTransaction( |
| 46 | + (transaction, hint) -> { |
| 47 | + // Drop a transaction: |
| 48 | + if (transaction.getTag("SomeTransactionTag") != null) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + return transaction; |
| 53 | + }); |
| 54 | + |
| 55 | + // Allows inspecting and modifying, returning a new or simply rejecting (returning null) |
| 56 | + options.setBeforeBreadcrumb( |
| 57 | + (breadcrumb, hint) -> { |
| 58 | + // Don't add breadcrumbs with message containing: |
| 59 | + if (breadcrumb.getMessage() != null |
| 60 | + && breadcrumb.getMessage().contains("bad breadcrumb")) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + return breadcrumb; |
| 64 | + }); |
| 65 | + |
| 66 | + // Configure the background worker which sends events to sentry: |
| 67 | + // Wait up to 5 seconds before shutdown while there are events to send. |
| 68 | + options.setShutdownTimeoutMillis(5000); |
| 69 | + |
| 70 | + // Enable SDK logging with Debug level |
| 71 | + options.setDebug(true); |
| 72 | + // To change the verbosity, use: |
| 73 | + // By default it's DEBUG. |
| 74 | + // options.setDiagnosticLevel( |
| 75 | + // SentryLevel |
| 76 | + // .ERROR); // A good option to have SDK debug log in prod is to use |
| 77 | + // only level |
| 78 | + // ERROR here. |
| 79 | + options.setEnablePrettySerializationOutput(false); |
| 80 | + |
| 81 | + // Exclude frames from some packages from being "inApp" so are hidden by default in Sentry |
| 82 | + // UI: |
| 83 | + options.addInAppExclude("org.jboss"); |
| 84 | + |
| 85 | + // Include frames from our package |
| 86 | + options.addInAppInclude("io.sentry.samples"); |
| 87 | + |
| 88 | + // Performance configuration options |
| 89 | + // Set what percentage of traces should be collected |
| 90 | + options.setTracesSampleRate(1.0); // set 0.5 to send 50% of traces |
| 91 | + |
| 92 | + // Determine traces sample rate based on the sampling context |
| 93 | + // options.setTracesSampler( |
| 94 | + // context -> { |
| 95 | + // // only 10% of transactions with "/product" prefix will be collected |
| 96 | + // if (!context.getTransactionContext().getName().startsWith("/products")) |
| 97 | + // { |
| 98 | + // return 0.1; |
| 99 | + // } else { |
| 100 | + // return 0.5; |
| 101 | + // } |
| 102 | + // }); |
| 103 | + }); |
| 104 | + |
| 105 | + Sentry.addBreadcrumb( |
| 106 | + "A 'bad breadcrumb' that will be rejected because of 'BeforeBreadcrumb callback above.'"); |
| 107 | + |
| 108 | + // Data added to the root scope (no PushScope called up to this point) |
| 109 | + // The modifications done here will affect all events sent and will propagate to child scopes. |
| 110 | + Sentry.configureScope( |
| 111 | + scope -> { |
| 112 | + scope.addEventProcessor(new SomeEventProcessor()); |
| 113 | + |
| 114 | + scope.setExtra("SomeExtraInfo", "Some value for extra info"); |
| 115 | + }); |
| 116 | + |
| 117 | + // Configures a scope which is only valid within the callback |
| 118 | + Sentry.withScope( |
| 119 | + scope -> { |
| 120 | + scope.setLevel(SentryLevel.FATAL); |
| 121 | + scope.setTransaction("main"); |
| 122 | + |
| 123 | + // This message includes the data set to the scope in this block: |
| 124 | + Sentry.captureMessage("Fatal message!"); |
| 125 | + }); |
| 126 | + |
| 127 | + // Only data added to the scope on `configureScope` above is included. |
| 128 | + Sentry.captureMessage("Some warning!", SentryLevel.WARNING); |
| 129 | + |
| 130 | + // Sending exception: |
| 131 | + Exception exception = new RuntimeException("Some error!"); |
| 132 | + Sentry.captureException(exception); |
| 133 | + |
| 134 | + // An event with breadcrumb and user data |
| 135 | + SentryEvent evt = new SentryEvent(); |
| 136 | + Message msg = new Message(); |
| 137 | + msg.setMessage("Detailed event"); |
| 138 | + evt.setMessage(msg); |
| 139 | + evt.addBreadcrumb("Breadcrumb directly to the event"); |
| 140 | + User user = new User(); |
| 141 | + user.setUsername("some@user"); |
| 142 | + evt.setUser(user); |
| 143 | + // Group all events with the following fingerprint: |
| 144 | + evt.setFingerprints(Collections.singletonList("NewClientDebug")); |
| 145 | + evt.setLevel(SentryLevel.DEBUG); |
| 146 | + Sentry.captureEvent(evt); |
| 147 | + |
| 148 | + int count = 10; |
| 149 | + for (int i = 0; i < count; i++) { |
| 150 | + String messageContent = "%d of %d items we'll wait to flush to Sentry!"; |
| 151 | + Message message = new Message(); |
| 152 | + message.setMessage(messageContent); |
| 153 | + message.setFormatted(String.format(messageContent, i, count)); |
| 154 | + SentryEvent event = new SentryEvent(); |
| 155 | + event.setMessage(message); |
| 156 | + |
| 157 | + final Hint hint = new Hint(); |
| 158 | + hint.set("level", SentryLevel.DEBUG); |
| 159 | + Sentry.captureEvent(event, hint); |
| 160 | + } |
| 161 | + |
| 162 | + // Performance feature |
| 163 | + // |
| 164 | + // Transactions collect execution time of the piece of code that's executed between the start |
| 165 | + // and finish of transaction. |
| 166 | + ITransaction transaction = Sentry.startTransaction("transaction name", "op"); |
| 167 | + // Transactions can contain one or more Spans |
| 168 | + ISpan outerSpan = transaction.startChild("child"); |
| 169 | + Thread.sleep(100); |
| 170 | + // Spans create a tree structure. Each span can have one ore more spans inside. |
| 171 | + ISpan innerSpan = outerSpan.startChild("jdbc", "select * from product where id = :id"); |
| 172 | + innerSpan.setStatus(SpanStatus.OK); |
| 173 | + Thread.sleep(300); |
| 174 | + // Finish the span and mark the end time of the span execution. |
| 175 | + // Note: finishing spans does not send them to Sentry |
| 176 | + innerSpan.finish(); |
| 177 | + try (ISentryLifecycleToken outerScope = outerSpan.makeCurrent()) { |
| 178 | + Span otelSpan = |
| 179 | + GlobalOpenTelemetry.get() |
| 180 | + .getTracer("demoTracer", "1.0.0") |
| 181 | + .spanBuilder("otelSpan") |
| 182 | + .startSpan(); |
| 183 | + try (Scope innerScope = otelSpan.makeCurrent()) { |
| 184 | + otelSpan.setAttribute("otel-attribute", "attribute-value"); |
| 185 | + Thread.sleep(150); |
| 186 | + otelSpan.setStatus(StatusCode.OK); |
| 187 | + } finally { |
| 188 | + otelSpan.end(); |
| 189 | + } |
| 190 | + } |
| 191 | + // Every SentryEvent reported during the execution of the transaction or a span, will have trace |
| 192 | + // context attached |
| 193 | + Sentry.captureMessage("this message is connected to the outerSpan"); |
| 194 | + outerSpan.finish(); |
| 195 | + // marks transaction as finished and sends it together with all child spans to Sentry |
| 196 | + transaction.finish(); |
| 197 | + |
| 198 | + // All events that have not been sent yet are being flushed on JVM exit. Events can be also |
| 199 | + // flushed manually: |
| 200 | + // Sentry.close(); |
| 201 | + } |
| 202 | + |
| 203 | + private static class SomeEventProcessor implements EventProcessor { |
| 204 | + @Override |
| 205 | + public SentryEvent process(SentryEvent event, Hint hint) { |
| 206 | + // Here you can modify the event as you need |
| 207 | + if (event.getLevel() != null && event.getLevel().ordinal() > SentryLevel.INFO.ordinal()) { |
| 208 | + event.addBreadcrumb(new Breadcrumb("Processed by " + SomeEventProcessor.class)); |
| 209 | + } |
| 210 | + |
| 211 | + return event; |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments