All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.contrib.jmxmetrics.OtelHelper.groovy Maven / Gradle / Ivy

There is a newer version: 1.42.0-alpha
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.contrib.jmxmetrics

import groovy.jmx.GroovyMBean
import io.opentelemetry.api.metrics.DoubleCounter
import io.opentelemetry.api.metrics.DoubleHistogram
import io.opentelemetry.api.metrics.DoubleUpDownCounter
import io.opentelemetry.api.metrics.LongCounter
import io.opentelemetry.api.metrics.LongHistogram
import io.opentelemetry.api.metrics.LongUpDownCounter
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement
import io.opentelemetry.api.metrics.ObservableLongMeasurement
import java.util.function.Consumer
import javax.management.ObjectName

class OtelHelper {
    private static final String SCALAR = '1'

    private final JmxClient jmxClient
    private final GroovyMetricEnvironment groovyMetricEnvironment

    OtelHelper(JmxClient jmxClient, GroovyMetricEnvironment groovyMetricEnvironment) {
        this.jmxClient = jmxClient
        this.groovyMetricEnvironment = groovyMetricEnvironment
    }

    /**
     * Returns a list of {@link GroovyMBean} for a given object name String.
     * @param objNameStr - the {@link String} representation of an object name or pattern, to be
     * used as the argument to the basic {@link javax.management.ObjectName} constructor for the JmxClient query.
     * @return a {@link List} from which to create metrics.
     */
    List queryJmx(String objNameStr) {
        return MBeanHelper.queryJmx(jmxClient, objNameStr);
    }

    /**
     * Returns a list of {@link GroovyMBean} for a given {@link javax.management.ObjectName}.
     * @param objName - the {@link javax.management.ObjectName} used for the JmxClient query.
     * @return a {@link List} from which to create metrics.
     */
    List queryJmx(ObjectName objName) {
        return MBeanHelper.queryJmx(jmxClient, objName);
    }

    /**
     * Returns a fetched, potentially multi-{@link GroovyMBean} {@link MBeanHelper} for a given object name String.
     * @param objNameStr - the {@link String} representation of an object name or pattern, to be
     * used as the argument to the basic {@link javax.management.ObjectName} constructor for the JmxClient query.
     * @return a {@link MBeanHelper} that operates over all resulting {@link GroovyMBean} instances.
     */
    MBeanHelper mbeans(String objNameStr) {
        def mbeanHelper = new MBeanHelper(jmxClient, objNameStr, false)
        mbeanHelper.fetch()
        return mbeanHelper
    }

    /**
     * Returns a fetched, potentially multi-{@link GroovyMBean} {@link MBeanHelper} for a given object name String.
     * @param objNameStr - the {@link String} representation of an object name or pattern, to be
     * used as the argument to the basic {@link javax.management.ObjectName} constructor for the JmxClient query.
     * @return a {@link MBeanHelper} that operates over all resulting {@link GroovyMBean} instances.
     */
    MBeanHelper mbeans(List objNameStrs) {
        def mbeanHelper = new MBeanHelper(jmxClient, objNameStrs)
        mbeanHelper.fetch()
        return mbeanHelper
    }

    /**
     * Returns a fetched, single {@link GroovyMBean} {@link MBeanHelper} for a given object name String.
     * @param objNameStr - the {@link String} representation of an object name or pattern, to be
     * used as the argument to the basic {@link javax.management.ObjectName} constructor for the JmxClient query.
     * @return a {@link MBeanHelper} that operates over all resulting {@link GroovyMBean} instances.
     */
    MBeanHelper mbean(String objNameStr) {
        def mbeanHelper = new MBeanHelper(jmxClient, objNameStr, true)
        mbeanHelper.fetch()
        return mbeanHelper
    }

    MBeanHelper mbean(String objNameStr, Map> attributeTransformation) {
      def mbeanHelper = new MBeanHelper(jmxClient, objNameStr, true, attributeTransformation)
      mbeanHelper.fetch()
      return mbeanHelper
    }

    MBeanHelper mbeans(List objNameStrs, Map> attributeTransformation) {
      def mbeanHelper = new MBeanHelper(jmxClient, objNameStrs, attributeTransformation)
      mbeanHelper.fetch()
      return mbeanHelper
    }
    /**
     * Returns an updated @{link InstrumentHelper} associated with the provided {@link MBeanHelper} and its specified
     * attribute value(s).  The parameters map to the InstrumentHelper constructor.
     */
    InstrumentHelper instrument(MBeanHelper mBeanHelper, String instrumentName, String description, String unit, Map labelFuncs, Map> attributes, Closure otelInstrument) {
        def instrumentHelper = new InstrumentHelper(mBeanHelper, instrumentName, description, unit, labelFuncs, attributes, otelInstrument, groovyMetricEnvironment)
        instrumentHelper.update()
        return instrumentHelper
    }

    InstrumentHelper instrument(MBeanHelper mBeanHelper, String instrumentName, String description, String unit, Map labelFuncs, String attribute, Closure otelInstrument) {
        return instrument(mBeanHelper, instrumentName, description, unit, labelFuncs, [(attribute): [:] as Map], otelInstrument)
    }

    InstrumentHelper instrument(MBeanHelper mBeanHelper, String instrumentName, String description, String unit, String attribute, Closure otelInstrument) {
        return instrument(mBeanHelper, instrumentName, description, unit, [:] as Map, attribute, otelInstrument)
    }

    InstrumentHelper instrument(MBeanHelper mBeanHelper, String instrumentName, String description, String unit, Map> attributes, Closure otelInstrument) {
        return instrument(mBeanHelper, instrumentName, description, unit, [:] as Map, attributes, otelInstrument)
    }

    InstrumentHelper instrument(MBeanHelper mBeanHelper, String instrumentName, String description, String attribute, Closure otelInstrument) {
        return instrument(mBeanHelper, instrumentName, description, OtelHelper.SCALAR, [:] as Map, attribute, otelInstrument)
    }

    InstrumentHelper instrument(MBeanHelper mBeanHelper, String instrumentName, String description, Map> attributes, Closure otelInstrument) {
        return instrument(mBeanHelper, instrumentName, description, OtelHelper.SCALAR, [:] as Map, attributes, otelInstrument)
    }

    InstrumentHelper instrument(MBeanHelper mBeanHelper, String instrumentName, String attribute, Closure otelInstrument) {
        return instrument(mBeanHelper, instrumentName, "", OtelHelper.SCALAR, [:] as Map, attribute, otelInstrument)
    }

    InstrumentHelper instrument(MBeanHelper mBeanHelper, String instrumentName, Map> attributes, Closure otelInstrument) {
        return instrument(mBeanHelper, instrumentName, "", OtelHelper.SCALAR, [:] as Map, attributes, otelInstrument)
    }

    DoubleCounter doubleCounter(String name, String description, String unit) {
        return groovyMetricEnvironment.getDoubleCounter(name, description, unit)
    }

    DoubleCounter doubleCounter(String name, String description) {
        return doubleCounter(name, description, SCALAR)
    }

    DoubleCounter doubleCounter(String name) {
        return doubleCounter(name, '')
    }

    LongCounter longCounter(String name, String description, String unit) {
        return groovyMetricEnvironment.getLongCounter(name, description, unit)
    }

    LongCounter longCounter(String name, String description) {
        return longCounter(name, description, SCALAR)
    }

    LongCounter longCounter(String name) {
        return longCounter(name, '')
    }

    DoubleUpDownCounter doubleUpDownCounter(String name, String description, String unit) {
        return groovyMetricEnvironment.getDoubleUpDownCounter(name, description, unit)
    }

    DoubleUpDownCounter doubleUpDownCounter(String name, String description) {
        return doubleUpDownCounter(name, description, SCALAR)
    }

    DoubleUpDownCounter doubleUpDownCounter(String name) {
        return doubleUpDownCounter(name, '')
    }

    LongUpDownCounter longUpDownCounter(String name, String description, String unit) {
        return groovyMetricEnvironment.getLongUpDownCounter(name, description, unit)
    }

    LongUpDownCounter longUpDownCounter(String name, String description) {
        return longUpDownCounter(name, description, SCALAR)
    }

    LongUpDownCounter longUpDownCounter(String name) {
        return longUpDownCounter(name, '')
    }

    DoubleHistogram doubleHistogram(String name, String description, String unit) {
        return groovyMetricEnvironment.getDoubleHistogram(name, description, unit)
    }

    DoubleHistogram doubleHistogram(String name, String description) {
        return doubleHistogram(name, description, SCALAR)
    }

    DoubleHistogram doubleHistogram(String name) {
        return doubleHistogram(name, '')
    }

    LongHistogram longHistogram(String name, String description, String unit) {
        return groovyMetricEnvironment.getLongHistogram(name, description, unit)
    }

    LongHistogram longHistogram(String name, String description) {
        return longHistogram(name, description, SCALAR)
    }

    LongHistogram longHistogram(String name) {
        return longHistogram(name, '')
    }

    ObservableDoubleMeasurement doubleCounterCallback(String name, String description, String unit, Consumer updater) {
        return groovyMetricEnvironment.registerDoubleCounterCallback(name, description, unit, updater)
    }

    ObservableDoubleMeasurement doubleCounterCallback(String name, String description, Consumer updater) {
        return doubleCounterCallback(name, description, SCALAR, updater)
    }

    ObservableDoubleMeasurement doubleCounterCallback(String name, Consumer updater) {
        return doubleCounterCallback(name, '', updater)
    }

    ObservableLongMeasurement longCounterCallback(String name, String description, String unit, Consumer updater) {
        return groovyMetricEnvironment.registerLongCounterCallback(name, description, unit, updater)
    }

    ObservableLongMeasurement longCounterCallback(String name, String description, Consumer updater) {
        return longCounterCallback(name, description, SCALAR, updater)
    }

    ObservableLongMeasurement longCounterCallback(String name, Consumer updater) {
        return longCounterCallback(name, '', updater)
    }

    ObservableDoubleMeasurement doubleUpDownCounterCallback(String name, String description, String unit, Consumer updater) {
        return groovyMetricEnvironment.registerDoubleUpDownCounterCallback(name, description, unit, updater)
    }

    ObservableDoubleMeasurement doubleUpDownCounterCallback(String name, String description, Consumer updater) {
        return doubleUpDownCounterCallback(name, description, SCALAR, updater)
    }

    ObservableDoubleMeasurement doubleUpDownCounterCallback(String name, Consumer updater) {
        return doubleUpDownCounterCallback(name, '', updater)
    }

    ObservableLongMeasurement longUpDownCounterCallback(String name, String description, String unit, Consumer updater) {
        return groovyMetricEnvironment.registerLongUpDownCounterCallback(name, description, unit, updater)
    }

    ObservableLongMeasurement longUpDownCounterCallback(String name, String description, Consumer updater) {
        return longUpDownCounterCallback(name, description, SCALAR, updater)
    }

    ObservableLongMeasurement longUpDownCounterCallback(String name, Consumer updater) {
        return longUpDownCounterCallback(name, '', updater)
    }

    ObservableDoubleMeasurement doubleValueCallback(String name, String description, String unit, Consumer updater) {
        return groovyMetricEnvironment.registerDoubleValueCallback(name, description, unit, updater)
    }

    ObservableDoubleMeasurement doubleValueCallback(String name, String description, Consumer updater) {
        return doubleValueCallback(name, description, SCALAR, updater)
    }

    ObservableDoubleMeasurement doubleValueCallback(String name, Consumer updater) {
        return doubleValueCallback(name, '', updater)
    }

    ObservableLongMeasurement longValueCallback(String name, String description, String unit, Consumer updater) {
        return groovyMetricEnvironment.registerLongValueCallback(name, description, unit, updater)
    }

    ObservableLongMeasurement longValueCallback(String name, String description, Consumer updater) {
        return longValueCallback(name, description, SCALAR, updater)
    }

    ObservableLongMeasurement longValueCallback(String name, Consumer updater) {
        return longValueCallback(name, '', updater)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy