|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus; |
| 18 | + |
| 19 | +import java.net.MalformedURLException; |
| 20 | +import java.net.URL; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import io.micrometer.core.instrument.Clock; |
| 25 | +import io.micrometer.prometheus.PrometheusConfig; |
| 26 | +import io.micrometer.prometheus.PrometheusMeterRegistry; |
| 27 | +import io.prometheus.client.CollectorRegistry; |
| 28 | +import io.prometheus.client.exemplars.DefaultExemplarSampler; |
| 29 | +import io.prometheus.client.exemplars.ExemplarSampler; |
| 30 | +import io.prometheus.client.exemplars.tracer.common.SpanContextSupplier; |
| 31 | +import io.prometheus.client.exporter.BasicAuthHttpConnectionFactory; |
| 32 | +import io.prometheus.client.exporter.PushGateway; |
| 33 | + |
| 34 | +import org.springframework.beans.factory.ObjectProvider; |
| 35 | +import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint; |
| 36 | +import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration; |
| 37 | +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; |
| 38 | +import org.springframework.boot.actuate.autoconfigure.metrics.export.ConditionalOnEnabledMetricsExport; |
| 39 | +import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration; |
| 40 | +import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager; |
| 41 | +import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager.ShutdownOperation; |
| 42 | +import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint; |
| 43 | +import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusSimpleclientScrapeEndpoint; |
| 44 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 45 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 46 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 47 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 48 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 49 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 50 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 51 | +import org.springframework.context.annotation.Bean; |
| 52 | +import org.springframework.context.annotation.Configuration; |
| 53 | +import org.springframework.core.env.Environment; |
| 54 | +import org.springframework.util.StringUtils; |
| 55 | + |
| 56 | +/** |
| 57 | + * {@link EnableAutoConfiguration Auto-configuration} for exporting metrics to Prometheus |
| 58 | + * with the Prometheus simpleclient. |
| 59 | + * |
| 60 | + * @author Jon Schneider |
| 61 | + * @author David J. M. Karlsen |
| 62 | + * @author Jonatan Ivanov |
| 63 | + * @since 2.0.0 |
| 64 | + * @deprecated in favor of {@link PrometheusMetricsExportAutoConfiguration} |
| 65 | + */ |
| 66 | +@Deprecated(since = "3.3.0", forRemoval = true) |
| 67 | +@AutoConfiguration( |
| 68 | + before = { CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class }, |
| 69 | + after = { MetricsAutoConfiguration.class, PrometheusMetricsExportAutoConfiguration.class }) |
| 70 | +@ConditionalOnBean(Clock.class) |
| 71 | +@ConditionalOnClass(PrometheusMeterRegistry.class) |
| 72 | +@ConditionalOnEnabledMetricsExport("prometheus") |
| 73 | +@EnableConfigurationProperties(PrometheusProperties.class) |
| 74 | +public class PrometheusSimpleclientMetricsExportAutoConfiguration { |
| 75 | + |
| 76 | + @Bean |
| 77 | + @ConditionalOnMissingBean |
| 78 | + public PrometheusConfig simpleclientPrometheusConfig(PrometheusProperties prometheusProperties) { |
| 79 | + return new PrometheusSimpleclientPropertiesConfigAdapter(prometheusProperties); |
| 80 | + } |
| 81 | + |
| 82 | + @Bean |
| 83 | + @ConditionalOnMissingBean |
| 84 | + public io.micrometer.prometheus.PrometheusMeterRegistry simpleclientPrometheusMeterRegistry( |
| 85 | + io.micrometer.prometheus.PrometheusConfig prometheusConfig, CollectorRegistry collectorRegistry, |
| 86 | + Clock clock, ObjectProvider<ExemplarSampler> exemplarSamplerProvider) { |
| 87 | + return new io.micrometer.prometheus.PrometheusMeterRegistry(prometheusConfig, collectorRegistry, clock, |
| 88 | + exemplarSamplerProvider.getIfAvailable()); |
| 89 | + } |
| 90 | + |
| 91 | + @Bean |
| 92 | + @ConditionalOnMissingBean |
| 93 | + public CollectorRegistry collectorRegistry() { |
| 94 | + return new CollectorRegistry(true); |
| 95 | + } |
| 96 | + |
| 97 | + @Bean |
| 98 | + @ConditionalOnMissingBean(ExemplarSampler.class) |
| 99 | + @ConditionalOnBean(SpanContextSupplier.class) |
| 100 | + public DefaultExemplarSampler exemplarSampler(SpanContextSupplier spanContextSupplier) { |
| 101 | + return new DefaultExemplarSampler(spanContextSupplier); |
| 102 | + } |
| 103 | + |
| 104 | + @SuppressWarnings("removal") |
| 105 | + @Configuration(proxyBeanMethods = false) |
| 106 | + @ConditionalOnAvailableEndpoint(endpoint = PrometheusSimpleclientScrapeEndpoint.class) |
| 107 | + public static class PrometheusScrapeEndpointConfiguration { |
| 108 | + |
| 109 | + @Bean |
| 110 | + @ConditionalOnMissingBean({ PrometheusSimpleclientScrapeEndpoint.class, PrometheusScrapeEndpoint.class }) |
| 111 | + public PrometheusSimpleclientScrapeEndpoint prometheusEndpoint(CollectorRegistry collectorRegistry) { |
| 112 | + return new PrometheusSimpleclientScrapeEndpoint(collectorRegistry); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Configuration for <a href="https://github.com/prometheus/pushgateway">Prometheus |
| 119 | + * Pushgateway</a>. |
| 120 | + */ |
| 121 | + @Configuration(proxyBeanMethods = false) |
| 122 | + @ConditionalOnClass(PushGateway.class) |
| 123 | + @ConditionalOnProperty(prefix = "management.prometheus.metrics.export.pushgateway", name = "enabled") |
| 124 | + public static class PrometheusPushGatewayConfiguration { |
| 125 | + |
| 126 | + /** |
| 127 | + * The fallback job name. We use 'spring' since there's a history of Prometheus |
| 128 | + * spring integration defaulting to that name from when Prometheus integration |
| 129 | + * didn't exist in Spring itself. |
| 130 | + */ |
| 131 | + private static final String FALLBACK_JOB = "spring"; |
| 132 | + |
| 133 | + @Bean |
| 134 | + @ConditionalOnMissingBean |
| 135 | + public PrometheusPushGatewayManager prometheusPushGatewayManager(CollectorRegistry collectorRegistry, |
| 136 | + PrometheusProperties prometheusProperties, Environment environment) throws MalformedURLException { |
| 137 | + PrometheusProperties.Pushgateway properties = prometheusProperties.getPushgateway(); |
| 138 | + Duration pushRate = properties.getPushRate(); |
| 139 | + String job = getJob(properties, environment); |
| 140 | + Map<String, String> groupingKey = properties.getGroupingKey(); |
| 141 | + ShutdownOperation shutdownOperation = properties.getShutdownOperation(); |
| 142 | + PushGateway pushGateway = initializePushGateway(properties.getBaseUrl()); |
| 143 | + if (StringUtils.hasText(properties.getUsername())) { |
| 144 | + pushGateway.setConnectionFactory( |
| 145 | + new BasicAuthHttpConnectionFactory(properties.getUsername(), properties.getPassword())); |
| 146 | + } |
| 147 | + return new PrometheusPushGatewayManager(pushGateway, collectorRegistry, pushRate, job, groupingKey, |
| 148 | + shutdownOperation); |
| 149 | + } |
| 150 | + |
| 151 | + private PushGateway initializePushGateway(String url) throws MalformedURLException { |
| 152 | + return new PushGateway(new URL(url)); |
| 153 | + } |
| 154 | + |
| 155 | + private String getJob(PrometheusProperties.Pushgateway properties, Environment environment) { |
| 156 | + String job = properties.getJob(); |
| 157 | + job = (job != null) ? job : environment.getProperty("spring.application.name"); |
| 158 | + return (job != null) ? job : FALLBACK_JOB; |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments