Search in sources :

Example 1 with MetricName

use of com.newrelic.agent.metric.MetricName in project newrelic-java-agent by newrelic.

the class TransactionProfile method transactionFinished.

public void transactionFinished(TransactionData transactionData) {
    final List<MetricNameTime> cpuTimes = new ArrayList<>();
    for (TransactionActivity activity : transactionData.getTransactionActivities()) {
        ThreadInfo threadInfo = threadMXBean.getThreadInfo(activity.getThreadId(), 0);
        if (null != threadInfo) {
            final List<List<StackTraceElement>> backtraces = new ArrayList<>();
            Map<Tracer, Collection<Tracer>> tracerTree = buildChildren(activity.getTracers(), backtraces);
            String threadName = threadNameNormalizer.getNormalizedThreadName(new BasicThreadInfo(threadInfo));
            threadActivityProfiles.get(threadName).add(activity, tracerTree);
            if (!backtraces.isEmpty()) {
                ProfileTree tree = threadProfiles.get(threadName);
                for (List<StackTraceElement> stack : backtraces) {
                    stack = DiscoveryProfile.getScrubbedStackTrace(stack);
                    Collections.reverse(stack);
                    tree.addStackTrace(stack, true);
                }
            }
            long cpuTime = activity.getTotalCpuTime();
            if (cpuTime > 0) {
                MetricName name = MetricName.create(transactionData.getBlameMetricName(), threadName);
                cpuTimes.add(new MetricNameTime(name, cpuTime));
            }
        }
    }
    if (!cpuTimes.isEmpty()) {
        ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

            @Override
            public void doWork(StatsEngine statsEngine) {
                for (MetricNameTime time : cpuTimes) {
                    statsEngine.getResponseTimeStats(time.name).recordResponseTime(time.cpuTime, TimeUnit.NANOSECONDS);
                }
            }

            @Override
            public String getAppName() {
                return null;
            }
        }, transactionData.getBlameMetricName());
    }
}
Also used : DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) ArrayList(java.util.ArrayList) TransactionActivity(com.newrelic.agent.TransactionActivity) StatsEngine(com.newrelic.agent.stats.StatsEngine) MetricName(com.newrelic.agent.metric.MetricName) ThreadInfo(java.lang.management.ThreadInfo) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) StatsWork(com.newrelic.agent.stats.StatsWork) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with MetricName

use of com.newrelic.agent.metric.MetricName in project newrelic-java-agent by newrelic.

the class RPMServiceTest method doTestStatusCodeSupportabilityMetrics.

private void doTestStatusCodeSupportabilityMetrics() throws Exception {
    List<String> appNames = new ArrayList<>(1);
    appNames.add("MyApplication");
    RPMService svc = new RPMService(appNames, null, null, Collections.<AgentConnectionEstablishedListener>emptyList());
    svc.launch();
    StatsEngine statsEngine = ServiceFactory.getStatsService().getStatsEngineForHarvest("MyApplication");
    MetricName metricName = MetricName.create("Supportability/Collector/HttpCode/200");
    assertTrue(statsEngine.getMetricNames().contains(metricName));
    svc.shutdown();
}
Also used : MetricName(com.newrelic.agent.metric.MetricName) ArrayList(java.util.ArrayList) StatsEngine(com.newrelic.agent.stats.StatsEngine)

Example 3 with MetricName

use of com.newrelic.agent.metric.MetricName in project newrelic-java-agent by newrelic.

the class CPUSamplerTest method test.

@Test
public void test() throws Exception {
    Map<String, Object> map = new HashMap<>();
    map.put("host", "localhost");
    map.put("port", 3000);
    map.put("ssl", false);
    map.put("license_key", "bootstrap_newrelic_admin_license_key_000");
    createServiceManager(map);
    StatsEngine statsEngine = new StatsEngineImpl();
    CPUHarvester harvester = new CPUHarvester();
    for (int i = 0; i < 10000; i++) {
        harvester.recordCPU(statsEngine);
        statsEngine.getMetricNames();
    }
    List<MetricName> harvest = statsEngine.getMetricNames();
    CountStats stats = null;
    for (MetricName data : harvest) {
        if (MetricNames.CPU.equals(data.getName())) {
            stats = (CountStats) statsEngine.getStats(data);
            break;
        }
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(out);
    stats.writeJSONString(writer);
    writer.close();
    System.err.println(out.toString());
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) HashMap(java.util.HashMap) CountStats(com.newrelic.agent.stats.CountStats) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StatsEngine(com.newrelic.agent.stats.StatsEngine) MetricName(com.newrelic.agent.metric.MetricName) OutputStreamWriter(java.io.OutputStreamWriter) Test(org.junit.Test)

Example 4 with MetricName

use of com.newrelic.agent.metric.MetricName in project newrelic-java-agent by newrelic.

the class AgentTest method ttSizeLimitExceeded.

/**
 * If the transaction trace size limit is exceeded, a tracer should record metrics but not be part of the
 * transaction trace.
 */
@Test
public void ttSizeLimitExceeded() throws ServletException, IOException {
    TransactionDataList txs = new TransactionDataList();
    ServiceFactory.getTransactionService().addTransactionListener(txs);
    TestSizeLimitServlet servlet = new TestSizeLimitServlet();
    String path = "/my/word";
    AgentHelper.invokeServlet(servlet, "", APPLICATION_NAME_2, path);
    StatsEngine statsEngine = AgentHelper.getDefaultStatsEngine();
    MetricName metricName = MetricName.create("Custom/test.newrelic.test.agent.AgentTest$TestSizeLimitServlet/doNothing", "WebTransaction/Servlet/TestSizeLimitServlet");
    ResponseTimeStats stats = statsEngine.getResponseTimeStats(metricName);
    Assert.assertEquals(2, stats.getCallCount());
    Assert.assertEquals(2, txs.size());
    TransactionData transactionData = txs.get(1);
    Collection<Tracer> tracers = AgentHelper.getTracers(transactionData.getRootTracer());
    Assert.assertEquals(3, tracers.size());
}
Also used : ResponseTimeStats(com.newrelic.agent.stats.ResponseTimeStats) MetricName(com.newrelic.agent.metric.MetricName) TransactionDataList(com.newrelic.agent.TransactionDataList) Tracer(com.newrelic.agent.tracers.Tracer) TransactionData(com.newrelic.agent.TransactionData) StatsEngine(com.newrelic.agent.stats.StatsEngine) Test(org.junit.Test)

Example 5 with MetricName

use of com.newrelic.agent.metric.MetricName in project newrelic-java-agent by newrelic.

the class SimpleStatsEngine method getMetricData.

/**
 * Converts the stats to a list of metric data.
 *
 * @param metricNormalizer The normalizer.
 * @param scope The scope. This should be EMPTY_SCOPE if these are unscoped metrics.
 * @return The list of metric data generated from the internal stats object.
 */
public List<MetricData> getMetricData(Normalizer metricNormalizer, String scope) {
    // +1 for Java/other
    List<MetricData> result = new ArrayList<>(stats.size() + 1);
    boolean isTrimStats = ServiceFactory.getConfigService().getDefaultAgentConfig().isTrimStats();
    if (isTrimStats && !scope.equals(MetricName.EMPTY_SCOPE)) {
        trimStats();
    }
    for (Entry<String, StatsBase> entry : stats.entrySet()) {
        MetricName metricName = MetricName.create(entry.getKey(), scope);
        MetricData metricData = createMetricData(metricName, entry.getValue(), metricNormalizer);
        if (metricData != null) {
            result.add(metricData);
        }
    }
    return result;
}
Also used : MetricName(com.newrelic.agent.metric.MetricName) ArrayList(java.util.ArrayList) MetricData(com.newrelic.agent.MetricData)

Aggregations

MetricName (com.newrelic.agent.metric.MetricName)14 Test (org.junit.Test)6 StatsEngine (com.newrelic.agent.stats.StatsEngine)5 ArrayList (java.util.ArrayList)3 ResponseTimeStats (com.newrelic.agent.stats.ResponseTimeStats)2 StatsTest (com.newrelic.agent.stats.StatsTest)2 StatsWork (com.newrelic.agent.stats.StatsWork)2 Tracer (com.newrelic.agent.tracers.Tracer)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 MetricData (com.newrelic.agent.MetricData)1 TransactionActivity (com.newrelic.agent.TransactionActivity)1 TransactionData (com.newrelic.agent.TransactionData)1 TransactionDataList (com.newrelic.agent.TransactionDataList)1 BrowserConfigTest (com.newrelic.agent.browser.BrowserConfigTest)1 CountStats (com.newrelic.agent.stats.CountStats)1 StatsEngineImpl (com.newrelic.agent.stats.StatsEngineImpl)1 BasicThreadInfo (com.newrelic.agent.threads.BasicThreadInfo)1 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1