当前位置:   article > 正文

prometheus - sentinel 推送数据_sentinel prometheus

sentinel prometheus

prometheus - sentinel 推送数据

背景

通过sentinel统计 请求数,将数据暴露给 promtheus端点, 然后通过 prometheus gateway 推送给 prometheus

引入依赖

<properties>
    <java.version>1.8</java.version>
    <alibaba.version>2021.0.1.0</alibaba.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_pushgateway -->
    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_pushgateway</artifactId>
    </dependency>

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

properties 配置 文件



spring.application.name=boot-senpro
management.metrics.tags.application=${spring.application.name}
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true

management.endpoint.metrics.enabled=true
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
# pushgateway 地址
management.metrics.export.prometheus.pushgateway.base-url=192.168.xx.xx:9091
management.metrics.export.prometheus.pushgateway.push-rate=15s
management.metrics.export.prometheus.pushgateway.job=${spring.application.name}
management.metrics.export.prometheus.pushgateway.enabled=true


server.port=9997

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

sentinel 的 prometheus 扩展

sentinel 提供 了 MetricExtension 接口,通过 SPI 方式引入,PrometheusExtension ,从 Sentinel 项目拷贝,改造了一下 ,通过 boot actuator prometheus 暴露:

public class PrometheusExtension implements MetricExtension {

    private PrometheusSentinelRegistry prometheusSenRegistry;

    private PrometheusSentinelRegistry getRegistry() {
        if (prometheusSenRegistry != null) {
            return prometheusSenRegistry;
        }
        this.prometheusSenRegistry = SpringUtil.getBean(PrometheusSentinelRegistry.class);
        return prometheusSenRegistry;
    }

    @Override
    public void addPass(String resource, int n, Object... args) {
        getRegistry().getPassRequests().labels(resource).inc(n);
    }

    @Override
    public void addBlock(String resource, int n, String origin, BlockException ex, Object... args) {
        getRegistry().getBlockRequests().labels(resource, ex.getClass().getSimpleName(), ex.getRuleLimitApp(), origin).inc(n);
    }

    @Override
    public void addSuccess(String resource, int n, Object... args) {
        getRegistry().getSuccessRequests().labels(resource).inc(n);
    }

    @Override
    public void addException(String resource, int n, Throwable throwable) {
        getRegistry().getExceptionRequests().labels(resource).inc(n);
    }

    @Override
    public void addRt(String resource, long rt, Object... args) {
        // convert millisecond to second
        getRegistry().getRtHist().labels(resource).observe(((double) rt) / 1000);
    }

    @Override
    public void increaseThreadNum(String resource, Object... args) {
        getRegistry().getCurrentThreads().labels(resource).inc();
    }

    @Override
    public void decreaseThreadNum(String resource, Object... args) {
        getRegistry().getCurrentThreads().labels(resource).dec();
    }
}

public class SpringUtil {

    private static ApplicationContext act;

    public static void setApplicationContext(ApplicationContext applicationContext) {
        SpringUtil.act = applicationContext;
    }

    public static  <T> T getBean(Class<T> bean) {
        return act.getBean(bean);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

统计:

public class PrometheusSentinelRegistry {

    private Counter passRequests;
    private Counter blockRequests;
    private Counter successRequests;
    private Counter exceptionRequests;
    private Histogram rtHist;
    private Gauge currentThreads;

    public PrometheusSentinelRegistry(CollectorRegistry registry) {
        passRequests = Counter.build()
                .name("sentinel_pass_requests_total")
                .help("total pass requests.")
                .labelNames("resource")
                .register(registry);
        blockRequests = Counter.build()
                .name("sentinel_block_requests_total")
                .help("total block requests.")
                .labelNames("resource", "type", "ruleLimitApp", "limitApp")
                .register(registry);
        successRequests = Counter.build()
                .name("sentinel_success_requests_total")
                .help("total success requests.")
                .labelNames("resource")
                .register(registry);
        exceptionRequests = Counter.build()
                .name("sentinel_exception_requests_total")
                .help("total exception requests.")
                .labelNames("resource")
                .register(registry);
        currentThreads = Gauge.build()
                .name("sentinel_current_threads")
                .help("current thread count.")
                .labelNames("resource")
                .register(registry);
        rtHist = Histogram.build()
                .name("sentinel_requests_latency_seconds")
                .help("request latency in seconds.")
                .labelNames("resource")
                .register(registry);
    }

    public Counter getPassRequests() {
        return passRequests;
    }

    public Counter getBlockRequests() {
        return blockRequests;
    }

    public Counter getSuccessRequests() {
        return successRequests;
    }

    public Counter getExceptionRequests() {
        return exceptionRequests;
    }

    public Histogram getRtHist() {
        return rtHist;
    }

    public Gauge getCurrentThreads() {
        return currentThreads;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

在启动类中注入:

@SpringBootApplication
public class BootSenproApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext act = SpringApplication.run(BootSenproApplication.class, args);
        SpringUtil.setApplicationContext(act);
    }


    @Bean
    public PrometheusSentinelRegistry prometheusSenRegistry(CollectorRegistry registry) {
        return new PrometheusSentinelRegistry(registry);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

访问

访问 boot 应用 prometheus 端点:

sentinel_success_requests_total{resource="/error",} 4.0
sentinel_success_requests_total{resource="/index",} 5.0
sentinel_success_requests_total{resource="/**",} 1.0
...
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/624777
推荐阅读
相关标签
  

闽ICP备14008679号