io.openlineage.client.metrics.MicrometerProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openlineage-java Show documentation
Show all versions of openlineage-java Show documentation
Java library for OpenLineage
/*
/* Copyright 2018-2024 contributors to the OpenLineage project
/* SPDX-License-Identifier: Apache-2.0
*/
package io.openlineage.client.metrics;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.openlineage.client.utils.ReflectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* MicrometerProvider is a class that manages global OpenLineage meter registry implementation that
* allows integrations to both add metrics backend from common OpenLineage config, or grab an
* instance of an initialized MeterRegistry.
*/
public class MicrometerProvider {
private static final List factories;
private static CompositeMeterRegistry registry;
static {
ServiceLoader loader = ServiceLoader.load(MeterRegistryFactory.class);
factories =
Stream.concat(
getMetricsBuilders().stream(), StreamSupport.stream(loader.spliterator(), false))
.collect(Collectors.toList());
registry = new CompositeMeterRegistry();
}
/**
* Parses the configuration map to create an optional MeterRegistry.
*
* @param config The configuration map for the MeterRegistry.
* @return Optional MeterRegistry created based on the configuration. Empty optional if the type
* is invalid or empty.
*/
public static Optional parseMeterRegistryConfig(Map config) {
if (config == null) {
return Optional.empty();
}
Object type = config.get("type");
if (!(type instanceof String) || "".equals(type)) {
return Optional.empty();
}
return getConfigBuilder((String) type).map(x -> x.registry(config));
}
/**
* Adds a MeterRegistry to the common OpenLineage meter registry based on the provided
* configuration.
*
* @param config The configuration for the MeterRegistry.
* @return Common registry configured with the added MeterRegistry.
*/
public static MeterRegistry addMeterRegistryFromConfig(Map config) {
Optional meterRegistry = parseMeterRegistryConfig(config);
meterRegistry.ifPresent(x -> registry.add(x));
return registry;
}
/**
* Adds a MeterRegistry to the common OpenLineage meter registry.
*
* @param meterRegistry The MeterRegistry to
*/
public static MeterRegistry addMeterRegistry(MeterRegistry meterRegistry) {
registry.add(meterRegistry);
return registry;
}
/**
* Returns the global OpenLineage meter registry.
*
* @return The global OpenLineage meter registry.
*/
public static MeterRegistry getMeterRegistry() {
return registry;
}
/**
* Clears the global OpenLineage meter registry and creates a new instance of a
* CompositeMeterRegistry.
*
* @return The newly created CompositeMeterRegistry instance.
*/
public static MeterRegistry clear() {
registry.close();
registry = new CompositeMeterRegistry();
return registry;
}
private static List getMetricsBuilders() {
List builders = new ArrayList<>();
if (ReflectionUtils.hasClass("io.micrometer.statsd.StatsdMeterRegistry")) {
builders.add(new StatsDMeterRegistryFactory());
}
builders.add(new SimpleMeterRegistryFactory());
builders.add(new CompositeMeterRegistryFactory());
return builders;
}
private static Optional getConfigBuilder(String type) {
return factories.stream().filter(builder -> builder.type().equals(type)).findFirst();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy