void bindTo()

in apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/builtin/AgentOverheadMetrics.java [123:167]


    void bindTo(MetricRegistry metricRegistry, MetricsConfigurationImpl config) {
        boolean overheadMetricsEnabled = config.isOverheadMetricsEnabled();

        cpuOverheadMetricEnabled = !metricRegistry.isDisabled(CPU_OVERHEAD_METRIC) && overheadMetricsEnabled;
        cpuUsageMetricEnabled = !metricRegistry.isDisabled(CPU_USAGE_METRIC) && overheadMetricsEnabled;
        allocationMetricEnabled = !metricRegistry.isDisabled(ALLOCATION_METRIC) && overheadMetricsEnabled;
        threadCountMetricEnabled = !metricRegistry.isDisabled(THREAD_COUNT_METRIC) && overheadMetricsEnabled;

        if (allocationMetricEnabled) {
            boolean allocationMeasurementEnabled = enableThreadAllocationMeasurement();
            if (!allocationMeasurementEnabled) {
                allocationMetricEnabled = false;
            }
        }

        if (cpuOverheadMetricEnabled || cpuUsageMetricEnabled) {
            //ensure that process-cpu-time is supported, as it is required
            boolean cpuTimeMeasurementEnabled = getProcessCpuTime() != NO_VALUE;
            if (!cpuTimeMeasurementEnabled) {
                logger.warn("Agent cpu overhead metrics can not be enabled: OperatingSystemMXBean.getProcessCpuTime() is not supported");
            }
            cpuTimeMeasurementEnabled = cpuTimeMeasurementEnabled && enableThreadCpuTimeMeasurement();
            if (!cpuTimeMeasurementEnabled) {
                cpuOverheadMetricEnabled = false;
                cpuUsageMetricEnabled = false;
            }
        }

        if (anyMetricEnabled()) {
            //initialize values for first report
            lastReportedProcessCpuTime = getProcessCpuTime();
            ExecutorUtils.setThreadStartListener(this);
            for (Map.Entry<Thread, String> threadWithPurpose : ExecutorUtils.getStartedThreads()) {
                Thread thread = threadWithPurpose.getKey();
                if (thread.isAlive()) {
                    lastThreadInfo.putIfAbsent(thread, new ThreadInfo(threadWithPurpose.getValue()));
                    ThreadInfo threadInfo = lastThreadInfo.get(thread);
                    updateCpuTimeStat(thread, threadInfo);
                    updateAllocationStat(thread, threadInfo);
                }
            }

            metricRegistry.addMetricsProvider(this);
        }
    }