@Test public void testCtor() { NullPointerException exception = new NullPointerException("mock"); ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(exception); assertSame(exception, exceptionTelemetry.getException()); assertTrue(exceptionTelemetry.getProperties().isEmpty()); assertTrue(exceptionTelemetry.getMetrics().isEmpty()); assertThat(exceptionTelemetry.getExceptions(), hasSize(1)); }
private static void testSeverityLevel(SeverityLevel severityLevel) { ExceptionTelemetry telemetry = new ExceptionTelemetry(new IllegalArgumentException("mockb")); telemetry.setSeverityLevel(severityLevel); assertEquals(telemetry.getSeverityLevel(), severityLevel); } }
/** * Sends an exception record to Application Insights. Appears in "exceptions" in Analytics and Search. * @param exception The exception to log information about. * @param properties Named string values you can use to search and classify trace messages. * @param metrics Measurements associated with this exception event. Appear in "custom metrics" in Metrics Explorer. */ public void trackException(Exception exception, Map<String, String> properties, Map<String, Double> metrics) { if (isDisabled()) { return; } ExceptionTelemetry et = new ExceptionTelemetry(exception); MapUtil.copy(properties, et.getContext().getProperties()); MapUtil.copy(metrics, et.getMetrics()); this.track(et); }
@Override public Telemetry apply(BaseTelemetry bt) { Preconditions.checkNotNull(bt); ExceptionTelemetry t = (ExceptionTelemetry) bt; final Exception.Builder eb = Exception.newBuilder() .setVer(t.getVer()); final SeverityLevel sl = transformSeverityLevel(t.getSeverityLevel()); if (sl != SeverityLevel.UNRECOGNIZED) eb.setSeverityLevel(sl); if (t.getProblemId() != null) eb.setProblemId(t.getProblemId()); if (t.getProperties() != null) eb.putAllProperties(t.getProperties()); if (t.getMetrics() != null) eb.putAllMeasurements(t.getMetrics()); if (t.getExceptions() != null) eb.addAllExceptions(Iterables.transform(t.getExceptions(), EXCEPTION_DETAILS_TRANSFORMER_FUNCTION)); return telemetryBuilderWithStandardFields(t).setException(eb).build(); } });
/** * Sends the given event to AI. * * @param event The event to send. */ public void sendEvent(ApplicationInsightsEvent event) { String formattedMessage = event.getMessage(); Map<String, String> customParameters = event.getCustomParameters(); Telemetry telemetry; if (event.isException()) { ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(event.getException()); exceptionTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel()); telemetry = exceptionTelemetry; } else { TraceTelemetry traceTelemetry = new TraceTelemetry(formattedMessage); traceTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel()); telemetry = traceTelemetry; } telemetry.getContext().getProperties().putAll(customParameters); telemetryClient.track(telemetry); }
@Override public void exceptionThrown(Exception e, int stackSize) { ThreadData localData = threadDataThreadLocal.get(); MethodData methodData = null; try { if (localData.methods != null && !localData.methods.isEmpty()) { for (MethodData m : localData.methods) { if (EXCEPTION_THROWN_ID.equals(m.name)) { return; } } } methodData = new MethodData(); methodData.interval = 0; methodData.type = InstrumentedClassType.OTHER.toString(); methodData.arguments = null; methodData.name = EXCEPTION_THROWN_ID; localData.methods.addFirst(methodData); ExceptionTelemetry et = new ExceptionTelemetry(e, stackSize); telemetryClient.track(et); } catch (ThreadDeath td) { throw td; } catch (Throwable t) { } if (methodData != null) { localData.methods.remove(methodData); } }
ExceptionTelemetry xt = new ExceptionTelemetry(new RuntimeException("ex-parent", new RuntimeException("ex-cause"))); xt.getContext().setInstrumentationKey(iKey); xt.getProperties().putAll(props); xt.getMetrics().putAll(metrics); testCases.add(new Object[]{ "Exception, with props & metrics", getTelemetryBuilder(ExceptionTelemetry.ENVELOPE_NAME, iKey) .setException(Exception.newBuilder() .setVer(xt.getVer()) .addAllExceptions(buildExceptionDetails(xt)) .putAllProperties(props)
@Test public void testGetThrowableOnException() { IOException exception = new IOException("mock"); ExceptionTelemetry telemetry = new ExceptionTelemetry(exception); assertSame(exception, telemetry.getThrowable()); assertSame(exception, telemetry.getException()); }
@Test public void testSetException() { NullPointerException exception = new NullPointerException("mock"); ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(exception); NullPointerException exception1 = new NullPointerException("mock"); exceptionTelemetry.setException(exception1); assertSame(exception1, exceptionTelemetry.getException()); }
@Test public void testExceptions() { Exception exception = new IOException("mocka", new IllegalArgumentException("mockb")); ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(exception); assertThat(exceptionTelemetry.getExceptions(), hasSize(2)); }
@Test public void testFirstValueIsNull() { ExceptionTelemetry telemetry = new ExceptionTelemetry(new IllegalArgumentException("mockb")); assertNull(telemetry.getSeverityLevel()); }
Assert.assertEquals("guid", exceptionTelemetry.getContext().getOperation().getId()); Assert.assertEquals(requestTelemetry.getId(), exceptionTelemetry.getContext().getOperation().getParentId()); Assert.assertEquals(2, exceptionTelemetry.getProperties().size()); Assert.assertEquals("value1", exceptionTelemetry.getProperties().get("key1")); Assert.assertEquals("value2", exceptionTelemetry.getProperties().get("key2"));
Assert.assertEquals(tp.getTraceId(), exceptionTelemetry.getContext().getOperation().getId()); Assert.assertEquals(requestTelemetry.getId(), exceptionTelemetry.getContext().getOperation().getParentId());
@Deprecated @Override protected void additionalSanitize() { Sanitizer.sanitizeMeasurements(this.getMetrics()); }
@Override protected void failed(Throwable e, Description description) { System.out.println("Failure detected. Printing found telemetry:"); List<Telemetry> telemetries = LogChannelMockVerifier.INSTANCE.getTelemetryCollection(); for (Telemetry t : telemetries) { String tname = t.getClass().getSimpleName(); String tinfo = tname; if (t instanceof TraceTelemetry) { TraceTelemetry trct = (TraceTelemetry)t; tinfo = String.format("%s{message='%s'}", tname, trct.getMessage()); } else if (t instanceof ExceptionTelemetry) { ExceptionTelemetry ext = (ExceptionTelemetry)t; tinfo = String.format("%s{%s[message='%s']}", tname, ext.getException().getClass().getSimpleName(), ext.getException().getMessage()); } System.out.println(" - "+tinfo); } System.out.printf("%d total telemetries collected.%n", telemetries.size()); } };
private static Iterable<? extends ExceptionDetails> buildExceptionDetails(final ExceptionTelemetry et) { Collection<ExceptionDetails> details = new ArrayList<>(); List<com.microsoft.applicationinsights.internal.schemav2.ExceptionDetails> exceptions = et.getExceptions(); for (com.microsoft.applicationinsights.internal.schemav2.ExceptionDetails ed : exceptions) { ExceptionDetails.Builder edb = ExceptionDetails.newBuilder() .setHasFullStack(BoolValue.of(ed.getHasFullStack())) .setId(ed.getId()) .setOuterId(ed.getOuterId()); if (ed.getMessage() != null) edb.setMessage(ed.getMessage()); if (ed.getTypeName() != null) edb.setTypeName(ed.getTypeName()); if (ed.getStack() != null) edb.setStack(ed.getStack()); if (ed.getParsedStack() != null) { for (com.microsoft.applicationinsights.internal.schemav2.StackFrame sf : ed.getParsedStack()) { StackFrame.Builder sfb = StackFrame.newBuilder() .setLine(sf.getLine()) .setMethod(sf.getMethod()) .setLevel(sf.getLevel()); if (sf.getFileName() != null) sfb.setFileName(sf.getFileName()); if (sf.getAssembly() != null) sfb.setAssembly(sf.getAssembly()); edb.addParsedStack(sfb); } } details.add(edb.build()); } return details; }
/** * Sends the given event to AI. * * @param event The event to send. */ public void sendEvent(ApplicationInsightsEvent event) { String formattedMessage = event.getMessage(); Map<String, String> customParameters = event.getCustomParameters(); Telemetry telemetry; if (event.isException()) { ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(event.getException()); exceptionTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel()); telemetry = exceptionTelemetry; } else { TraceTelemetry traceTelemetry = new TraceTelemetry(formattedMessage); traceTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel()); telemetry = traceTelemetry; } telemetry.getContext().getProperties().putAll(customParameters); telemetryClient.track(telemetry); }
private void sendHTTPTelemetry(MethodData methodData, Throwable throwable) { if (methodData.arguments != null && methodData.arguments.length == 1) { String url = methodData.arguments[0].toString(); long durationInMilliSeconds = nanoToMilliseconds(methodData.interval); Duration duration = new Duration(durationInMilliSeconds); InternalLogger.INSTANCE.trace("Sending HTTP RDD event, URL: '%s', duration=%s ms", url, durationInMilliSeconds); RemoteDependencyTelemetry telemetry = new RemoteDependencyTelemetry(url, null, duration, throwable == null); telemetry.setDependencyKind(DependencyKind.Http); Date dependencyStartDate = new Date(System.currentTimeMillis() - durationInMilliSeconds); telemetry.setTimestamp(dependencyStartDate); telemetryClient.trackDependency(telemetry); if (throwable != null) { ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(throwable); telemetryClient.track(exceptionTelemetry); } } }
@Test public void testError() { Error error = new NoSuchMethodError("Method"); ExceptionTelemetry telemetry = new ExceptionTelemetry(error); assertNull(telemetry.getException()); assertSame(error, telemetry.getThrowable()); }
/** * Sends the given event to AI. * * @param event The event to send. */ public void sendEvent(ApplicationInsightsEvent event) { String formattedMessage = event.getMessage(); Map<String, String> customParameters = event.getCustomParameters(); Telemetry telemetry; if (event.isException()) { ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(event.getException()); exceptionTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel()); telemetry = exceptionTelemetry; } else { TraceTelemetry traceTelemetry = new TraceTelemetry(formattedMessage); traceTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel()); telemetry = traceTelemetry; } telemetry.getContext().getProperties().putAll(customParameters); telemetryClient.track(telemetry); }