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

com.swisscom.cloud.sb.broker.metrics.ServiceBrokerMetricsService.groovy Maven / Gradle / Ivy

/*
 * Copyright (c) 2018 Swisscom (Switzerland) Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.swisscom.cloud.sb.broker.metrics

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.Gauge
import io.micrometer.core.instrument.MeterRegistry

import java.util.function.ToDoubleFunction

@CompileStatic
@Slf4j
abstract class ServiceBrokerMetricsService {

    protected ServiceBrokerMetricsConfig serviceBrokerMetricsConfig
    protected MetricsCache metricsCache
    protected MeterRegistry meterRegistry

    protected ServiceBrokerMetricsService(MeterRegistry meterRegistry, MetricsCache metricsCache, ServiceBrokerMetricsConfig serviceBrokerMetricsConfig) {
        this.meterRegistry = meterRegistry
        this.metricsCache = metricsCache
        this.serviceBrokerMetricsConfig = serviceBrokerMetricsConfig

        bindAll()
    }

    void addMetricsGauge(String name, Closure function, Map tags = new HashMap()) {
        ToDoubleFunction doubleFunction = new ToDoubleFunction() {
            @Override
            double applyAsDouble(ServiceBrokerMetricsService serviceBrokerMetrics) {
                def value = function()
                log.debug("Logging metrics ${name}(value:${value};tags:${tags})")
                return value
            }
        }

        def gaugeBuilder = Gauge.builder(name, this, doubleFunction)
        tags.put( "env", serviceBrokerMetricsConfig.env)
        tags.each { tagName, tagValue -> gaugeBuilder = gaugeBuilder.tag(tagName, tagValue) }
        gaugeBuilder.register(meterRegistry)
    }

    Counter createMetricsCounter(String name, Map tags = new HashMap()) {
        def counterBuilder = Counter.builder(name)
        tags.put( "env", serviceBrokerMetricsConfig.env)
        tags.each { t -> counterBuilder.tag(t.key, t.value) }
        counterBuilder.register(meterRegistry)
    }

    abstract void bindAll()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy