public Runnable getTrackMetric_FullAggregate() { final double value = (123.4 + 567.8); final int count = 2; final double min = 123.4; final double max = 567.8; final double avg = value/count; final double stdDev = Math.sqrt(((123.4-avg)*(123.4-avg) + (567.8-avg)*(567.8-avg))/count); MetricTelemetry mt = new MetricTelemetry(); mt.setName("AiTestMetric_FullAggregate"); mt.setValue(value); mt.setCount(count); mt.setMin(min); mt.setMax(max); mt.setStandardDeviation(stdDev); for (Entry<String, String> entry : getPropertyMapForMethod("Metric_Agg").entrySet()) { mt.getProperties().put(entry.getKey(), entry.getValue()); } return customCases.getTrackMetric(mt); }
@Override public Telemetry apply(BaseTelemetry bt) { Preconditions.checkNotNull(bt); MetricTelemetry t = (MetricTelemetry) bt; final Metric.Builder mb = Metric.newBuilder() .setVer(t.getVer()); if (t.getProperties() != null) mb.putAllProperties(t.getProperties()); DataPoint.Builder dpb = DataPoint.newBuilder() .setValue(t.getValue()); final DataPointType dpType = transformDataPointType(t.getKind()); if (dpType != DataPointType.UNRECOGNIZED) dpb.setKind(dpType); if (t.getCount() != null) dpb.setCount(Int32Value.of(t.getCount())); if (t.getMin() != null) dpb.setMin(DoubleValue.of(t.getMin())); if (t.getMax() != null) dpb.setMax(DoubleValue.of(t.getMax())); if (t.getStandardDeviation() != null) dpb.setStdDev(DoubleValue.of(t.getStandardDeviation())); if (t.getName() != null) dpb.setName(t.getName()); mb.addMetrics(dpb); return telemetryBuilderWithStandardFields(t).setMetric(mb).build(); } });
@Override protected void send(TelemetryClient telemetryClient, String displayName, double value) { InternalLogger.INSTANCE.trace("Metric JMX: %s, %s", displayName, value); MetricTelemetry telemetry = new MetricTelemetry(); telemetry.markAsCustomPerfCounter(); telemetry.setName(displayName); telemetry.setValue(value); telemetry.getProperties().put("CustomPerfCounter", "true"); telemetryClient.track(telemetry); } }
public Runnable getTrackMetric_FullMeasurement() { final double value = 789.0123; final int count = 1; MetricTelemetry mt = new MetricTelemetry(); mt.setName("AiTestMetric_FullMeasurement"); mt.setValue(value); mt.setCount(count); for (Entry<String, String> entry : getPropertyMapForMethod("Metric_Mea").entrySet()) { mt.getProperties().put(entry.getKey(), entry.getValue()); } return customCases.getTrackMetric(mt); }
/** * Sends a numeric metric to Application Insights. Appears in customMetrics in Analytics, and under Custom Metrics in Metric Explorer. * * @param name The name of the metric. Max length 150. * @param value The value of the metric. Sum if it represents an aggregation. * @param sampleCount The sample count. * @param min The minimum value of the sample. * @param max The maximum value of the sample. * @param stdDev The standard deviation of the sample. * @param properties Named string values you can use to search and classify trace messages. * @throws IllegalArgumentException if name is null or empty */ public void trackMetric(String name, double value, Integer sampleCount, Double min, Double max, Double stdDev, Map<String, String> properties) { if (isDisabled()) { return; } MetricTelemetry mt = new MetricTelemetry(name, value); mt.setCount(sampleCount); mt.setMin(min); mt.setMax(max); mt.setStandardDeviation(stdDev); MapUtil.copy(properties, mt.getProperties()); this.track(mt); }
MetricTelemetry mt = new MetricTelemetry("TestMeasurement", 1.123); mt.getContext().setInstrumentationKey(iKey); testCases.add(new Object[]{ "Metric measurement", getTelemetryBuilder(MetricTelemetry.ENVELOPE_NAME, iKey).setMetric( Metric.newBuilder() .setVer(mt.getVer()) .addMetrics( DataPoint.newBuilder() final double max = 4.3; final int count = 4; mt = new MetricTelemetry("TestAggregation", sum); mt.getContext().setInstrumentationKey(iKey); mt.setStandardDeviation(stdDev); mt.setMin(min); mt.setMax(max); mt.setCount(count); testCases.add(new Object[]{ "Metric aggregation", getTelemetryBuilder(MetricTelemetry.ENVELOPE_NAME, iKey).setMetric( Metric.newBuilder() .setVer(mt.getVer()) .addMetrics( DataPoint.newBuilder() mt = new MetricTelemetry("TestMeasurement-withProps", 2.22222);
@Test public void testTrackMetricWithNameAndValue() { final String name = "Metric"; final double value = 1.11; client.trackMetric(name, value); MetricTelemetry mt = (MetricTelemetry) verifyAndGetLastEventSent(); assertEquals("getName", name, mt.getName()); assertEquals("getValue", value, mt.getValue(), Math.ulp(value)); assertNull("getCount should be null", mt.getCount()); assertNull("getMin should be null", mt.getMin()); assertNull("getMax should be null", mt.getMax()); assertNull("getStandardDeviation should be null", mt.getStandardDeviation()); assertTrue("properties should be empty", mt.getProperties().isEmpty()); }
MetricTelemetry mt = new MetricTelemetry("Test time", 17.0); mt.setMax(20.0); mt.setMin(10.0); mt.setCount(100); mt.setStandardDeviation(2.43); appInsights.trackMetric(mt); System.out.println("[4] Metric -- metric:\"Test time\", value=17.0, Max=20.0, Min=10.0, Count=100 and STDV=2.43"); MetricTelemetry mtPc = new MetricTelemetry("Test PC Metric", 23.0); mtPc.markAsCustomPerfCounter(); appInsights.trackMetric(mtPc); System.out.println("[8] Metric -- metric:\"Test PC Metric\", value=23.0");
@Test public void testEmptyCtor() { MetricTelemetry telemetry = new MetricTelemetry(); assertNull(telemetry.getName()); assertEquals(0.0, telemetry.getValue(), Math.ulp(0.0)); assertNull(telemetry.getCount()); assertNull(telemetry.getMin()); assertNull(telemetry.getMax()); assertNull(telemetry.getStandardDeviation()); }
/** * Creates and returns the heartbeat telemetry. * @return Metric Telemetry which represent heartbeat. */ private MetricTelemetry gatherData() { MetricTelemetry heartbeat = new MetricTelemetry(HEARTBEAT_SYNTHETIC_METRIC_NAME, 0.0); Map<String, String> property = heartbeat.getProperties(); for (Map.Entry<String, HeartBeatPropertyPayload> entry : heartbeatProperties.entrySet()) { property.put(entry.getKey(), entry.getValue().getPayloadValue()); double currentValue = heartbeat.getValue(); currentValue += entry.getValue().isHealthy() ? 0 : 1; heartbeat.setValue(currentValue); } return heartbeat; }
@Test public void testSetValue() { MetricTelemetry telemetry = new MetricTelemetry("MockName", 120.1); telemetry.setValue(240.0); assertEquals("MockName", telemetry.getName()); assertEquals(240.0, telemetry.getValue(), Math.ulp(240.0)); }
@Override public void report(TelemetryClient telemetryClient) { MetricTelemetry mt = new MetricTelemetry(METRIC_NAME, 0.0); mt.markAsCustomPerfCounter(); long[] threadIds = threadBean.findDeadlockedThreads(); if (threadIds != null && threadIds.length > 0) { ArrayList<Long> blockedThreads = new ArrayList<Long>(); StringBuilder sb = new StringBuilder(); for (long threadId : threadIds) { ThreadInfo threadInfo = threadBean.getThreadInfo(threadId); if (threadInfo == null) { continue; } setThreadInfoAndStack(sb, threadInfo); blockedThreads.add(threadId); } if (!blockedThreads.isEmpty()) { String uuid = LocalStringsUtils.generateRandomIntegerId(); mt.setValue((double)blockedThreads.size()); mt.getContext().getOperation().setId(uuid); TraceTelemetry trace = new TraceTelemetry(String.format("%s%s", "Suspected deadlocked threads: ", sb.toString())); trace.getContext().getOperation().setId(uuid); telemetryClient.track(trace); } } telemetryClient.track(mt); } private void setThreadInfoAndStack(StringBuilder sb, ThreadInfo ti) {
@Test public void testSetName() { MetricTelemetry telemetry = new MetricTelemetry("MockName", 120.1); telemetry.setName("MockName1"); assertEquals("MockName1", telemetry.getName()); assertEquals(120.1, telemetry.getValue(), Math.ulp(120.1)); }
@Test public void testSetCount() { MetricTelemetry telemetry = new MetricTelemetry("MockName", 120.1); telemetry.setCount(1); assertEquals(new Integer(1), telemetry.getCount()); }
@Test public void testSetStandardDeviation() { MetricTelemetry telemetry = new MetricTelemetry("MockName", 120.1); telemetry.setStandardDeviation(new Double(1)); assertEquals(new Double(1), telemetry.getStandardDeviation()); }
@Test public void testSetMin() { MetricTelemetry telemetry = new MetricTelemetry("MockName", 120.1); telemetry.setMin(new Double(1)); assertEquals(new Double(1), telemetry.getMin()); }
@Test public void testSetMax() { MetricTelemetry telemetry = new MetricTelemetry("MockName", 120.1); telemetry.setMax(new Double(1)); assertEquals(new Double(1), telemetry.getMax()); }
@Test(expected = IllegalArgumentException.class) public void telemetryBuilderWithStandardFieldsDoesNotAllowNullInstrumentationKey() { LocalForwarderModelTransformer.telemetryBuilderWithStandardFields(new MetricTelemetry()); }
private void reportHeap(MemoryMXBean memory, TelemetryClient telemetryClient) { MemoryUsage mhu = memory.getHeapMemoryUsage(); if (mhu != null) { long currentHeapUsed = mhu.getUsed() / Megabyte; MetricTelemetry memoryHeapUsage = new MetricTelemetry(HEAP_MEM_USED, currentHeapUsed); memoryHeapUsage.markAsCustomPerfCounter(); telemetryClient.track(memoryHeapUsage); } } }