Search in sources :

Example 11 with MetricData

use of com.newrelic.agent.MetricData in project newrelic-java-agent by newrelic.

the class DataSenderImplTest method testDataUsageSupportability.

@Test
public void testDataUsageSupportability() throws Exception {
    AgentConfig config = AgentConfigImpl.createAgentConfig(configMap());
    HttpClientWrapper wrapperEmptyReturn = getHttpClientWrapper(ReadResult.create(HttpResponseCode.OK, "", null));
    DataSenderImpl target = new DataSenderImpl(config, wrapperEmptyReturn, null, logger, ServiceFactory.getConfigService());
    target.setAgentRunId("agent run id");
    List<MetricData> metricData = createMetricData(5);
    target.sendMetricData(System.currentTimeMillis() - 5000, System.currentTimeMillis(), metricData);
    // Expected payload sent after adding 5 metrics and agent metadata
    final String expectedSentPayload = "[\"agent run id\",1644424673,1644424678," + "[[0,[1,1.0,1.0,1.0,1.0,1.0]]," + "[1,[1,1.0,1.0,1.0,1.0,1.0]]," + "[2,[1,1.0,1.0,1.0,1.0,1.0]]," + "[3,[1,1.0,1.0,1.0,1.0,1.0]]," + "[4,[1,1.0,1.0,1.0,1.0,1.0]]]]";
    int expectedSentPayloadSizeInBytes = expectedSentPayload.getBytes().length;
    // Expected payload received is empty
    final String expectedReceivedPayload = "";
    int expectedReceivedPayloadSizeInBytes = expectedReceivedPayload.getBytes().length;
    String collectorOutputBytesMetric = MessageFormat.format(MetricNames.SUPPORTABILITY_DATA_USAGE_DESTINATION_OUTPUT_BYTES, "Collector");
    String collectorEndpointOutputBytesMetric = MessageFormat.format(MetricNames.SUPPORTABILITY_DATA_USAGE_DESTINATION_ENDPOINT_OUTPUT_BYTES, "Collector", "metric_data");
    assertMetricWasRecorded(MessageFormat.format(MetricNames.SUPPORTABILITY_HTTP_CODE, HttpResponseCode.OK));
    assertMetricWasRecorded(collectorOutputBytesMetric);
    assertMetricWasRecorded(collectorEndpointOutputBytesMetric);
    assertDataUsageMetricValues(collectorOutputBytesMetric, expectedSentPayloadSizeInBytes, expectedReceivedPayloadSizeInBytes);
    assertDataUsageMetricValues(collectorEndpointOutputBytesMetric, expectedSentPayloadSizeInBytes, expectedReceivedPayloadSizeInBytes);
}
Also used : AgentConfig(com.newrelic.agent.config.AgentConfig) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Example 12 with MetricData

use of com.newrelic.agent.MetricData in project newrelic-java-agent by newrelic.

the class InstrumentTestUtils method getAndClearMetricData.

public static Map<String, Integer> getAndClearMetricData() {
    StatsEngine statsEngine = AgentHelper.getDefaultStatsEngine();
    List<MetricData> data = statsEngine.getMetricData(new MockNormalizer());
    Map<String, Integer> dataMap = new HashMap<>();
    for (MetricData metricData : data) {
        StatsBase stats = metricData.getStats();
        if (stats instanceof CountStats) {
            dataMap.put(metricData.getMetricName().getName(), ((CountStats) stats).getCallCount());
        } else {
            dataMap.put(metricData.getMetricName().getName(), 0);
        }
    }
    return dataMap;
}
Also used : HashMap(java.util.HashMap) CountStats(com.newrelic.agent.stats.CountStats) StatsBase(com.newrelic.agent.stats.StatsBase) StatsEngine(com.newrelic.agent.stats.StatsEngine) MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData)

Example 13 with MetricData

use of com.newrelic.agent.MetricData in project newrelic-java-agent by newrelic.

the class StatsEngineImpl method createUnscopedCopies.

public static List<MetricData> createUnscopedCopies(Normalizer metricNormalizer, final List<MetricData> scopedMetrics) {
    // we do not want to fill up more than 75 percent of the hash map
    // add two to ensure that we are under 75 percent
    int size = (int) (scopedMetrics.size() / 0.75) + 2;
    Map<String, MetricData> allUnscopedMetrics = new HashMap<>(size);
    List<MetricData> results = new ArrayList<>(scopedMetrics.size());
    // iterate through the metrics
    for (MetricData scoped : scopedMetrics) {
        String theMetricName = scoped.getMetricName().getName();
        MetricData unscopedMetric = getUnscopedCloneOfData(metricNormalizer, theMetricName, scoped.getStats());
        if (unscopedMetric != null) {
            // see if we have already seen a metric with this name
            MetricData mapUnscoped = allUnscopedMetrics.get(theMetricName);
            if (mapUnscoped == null) {
                // add to the results to send out and add to unscoped map to merge additional
                allUnscopedMetrics.put(theMetricName, unscopedMetric);
                results.add(unscopedMetric);
            } else {
                // just add it to the unscoped metric with the same metric name
                mapUnscoped.getStats().merge(unscopedMetric.getStats());
            }
        }
    }
    return results;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MetricData(com.newrelic.agent.MetricData)

Example 14 with MetricData

use of com.newrelic.agent.MetricData in project newrelic-java-agent by newrelic.

the class MetricDataTest method bad.

@Test
public void bad() throws IOException {
    ResponseTimeStatsImpl stats = new ResponseTimeStatsImpl();
    MetricData data = MetricData.create(MetricName.create(MetricNames.EXTERNAL_ALL), stats);
    stats = new ResponseTimeStatsImpl();
    MetricData data2 = MetricData.create(MetricName.create(MetricNames.DISPATCHER), stats);
    stats.setCallCount(-2);
    PrintWriter writer = new PrintWriter(new ByteArrayOutputStream());
    JSONArray.writeJSONString(Arrays.asList(data, data2), writer);
    writer.close();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) MetricData(com.newrelic.agent.MetricData) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 15 with MetricData

use of com.newrelic.agent.MetricData in project newrelic-java-agent by newrelic.

the class MetricDataTest method valid.

@Test
public void valid() throws IOException {
    ResponseTimeStatsImpl stats = new ResponseTimeStatsImpl();
    MetricData data = MetricData.create(MetricName.create(MetricNames.EXTERNAL_ALL), stats);
    PrintWriter writer = new PrintWriter(new ByteArrayOutputStream());
    data.writeJSONString(writer);
    writer.close();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) MetricData(com.newrelic.agent.MetricData) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

MetricData (com.newrelic.agent.MetricData)19 Test (org.junit.Test)12 MockNormalizer (com.newrelic.agent.MockNormalizer)10 Transaction (com.newrelic.agent.Transaction)4 HashMap (java.util.HashMap)4 CountStats (com.newrelic.agent.stats.CountStats)3 ArrayList (java.util.ArrayList)3 StatsBase (com.newrelic.agent.stats.StatsBase)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintWriter (java.io.PrintWriter)2 AgentConfig (com.newrelic.agent.config.AgentConfig)1 TracedMetricData (com.newrelic.agent.introspec.TracedMetricData)1 MetricName (com.newrelic.agent.metric.MetricName)1 SimpleStatsEngine (com.newrelic.agent.stats.SimpleStatsEngine)1 StatsEngine (com.newrelic.agent.stats.StatsEngine)1 Field (java.lang.reflect.Field)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1