日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

從 Prometheus 到 OpenTelemetry:指標監控的演進與實踐

來源: 責編: 時間:2024-06-14 17:40:00 168觀看
導讀背景關于 metrics 我最早接觸相關概念的就是 prometheus,它是第二個加入 CNCF(云原生)社區的項目(第一個是 kubernetes),可見在云原生領域 Metrics 指標監控從誕生之初就是一個非常重要的組件。現實也確實如此,如今只要使用

背景

關于 metrics 我最早接觸相關概念的就是 prometheus,它是第二個加入 CNCF(云原生)社區的項目(第一個是 kubernetes),可見在云原生領域 Metrics 指標監控從誕生之初就是一個非常重要的組件。8OX28資訊網——每日最新資訊28at.com

現實也確實如此,如今只要使用到了 kubernetes 相關的項目,對其監控就是必不可少的。8OX28資訊網——每日最新資訊28at.com

當然也不止是云原生的項目才需要 Metrics 指標監控,我們任何一個業務都是需要的,不然我們的服務運行對開發運維來說都是一個黑盒,無法知道此時系統的運行情況,因此才需要我們的業務系統將一些關鍵運行指標暴露出來。8OX28資訊網——每日最新資訊28at.com

圖片圖片8OX28資訊網——每日最新資訊28at.com

業務數據:比如訂單的增長率、銷售金額等業務數據;同時還有應用自身的資源占用情況:8OX28資訊網——每日最新資訊28at.com

  • QPS
  • Latency
  • 內存
  • CPU 等信息。

在使用 OpenTelemetry 之前,因為 prometheus 是這部分的絕對標準,所以我們通常都會使用 prometheus 的包來暴露這些指標:8OX28資訊網——每日最新資訊28at.com

<!-- The client --><dependency>  <groupId>io.prometheus</groupId>  <artifactId>simpleclient</artifactId>  <version>0.16.0</version></dependency><!-- Hotspot JVM metrics--><dependency>  <groupId>io.prometheus</groupId>  <artifactId>simpleclient_hotspot</artifactId>  <version>0.16.0</version></dependency>

暴露一個自定義的指標也很簡單:8OX28資訊網——每日最新資訊28at.com

import io.prometheus.client.Counter;class YourClass {  static final Counter requests = Counter.build()     .name("requests_total").help("Total requests.").register();  void processRequest() {    requests.inc();    // Your code here.  }}

這是暴露一個單調遞增的指標,prometheus 還提供了其他幾種指標類型:8OX28資訊網——每日最新資訊28at.com

  • Counter
  • Gauge
  • Histogram

之后我們只需要在 prometheus 中配置一些抓取規則即可:8OX28資訊網——每日最新資訊28at.com

scrape_configs:  - job_name: 'springboot'    scrape_interval: 10s    static_configs:      - targets: ['localhost:8080'] # Spring Boot ip+port

當然如果是運行在 kubernetes 環境,prometheus 也可以基于服務發現配置一些規則,自動抓取我們的 Pod 的數據,由于不是本文的重點就不過多介紹。8OX28資訊網——每日最新資訊28at.com

基本組件

在 OpenTelemetry 中自然也提供了 Metrics 這個組件,同時它也是完全兼容 Prometheus,所以我們理解和使用起來并不復雜。8OX28資訊網——每日最新資訊28at.com

MeterProvider

不同于 prometheus 客戶端中直接提供了 Counter 就可以創建指標了,在 OpenTelemetry 中會提供一個 MeterProvider 的接口,使用這個接口可以獲取 Meter,再使用 Meter 才可以創建 Counter、Gauge、Histogram 等數據。8OX28資訊網——每日最新資訊28at.com

下面來看看具體如何使用,這里我以 Pulsar 源碼的代碼進行演示:8OX28資訊網——每日最新資訊28at.com

public InstrumentProvider(OpenTelemetry otel) {      if (otel == null) {          // By default, metrics are disabled, unless the OTel java agent is configured.          // This allows to enable metrics without any code change.        otel = GlobalOpenTelemetry.get();      }    this.meter = otel.getMeterProvider()              .meterBuilder("org.apache.pulsar.client")              .setInstrumentationVersion(PulsarVersion.getVersion())              .build();  }LongCounterBuilder builder = meter.counterBuilder(name)          .setDescription(description)          .setUnit(unit.toString());

Meter Exporter

Meter Exporter 則是一個 OpenTelemetry 獨有的概念,與我們之前講到的一樣:OpenTelemetry 作為廠商無關的平臺,允許我們將數據寫入到任何兼容的產品里。8OX28資訊網——每日最新資訊28at.com

所以我們在使用 Metrics 時需要指定一個 exporter:8OX28資訊網——每日最新資訊28at.com

Exporter 類型8OX28資訊網——每日最新資訊28at.com

作用8OX28資訊網——每日最新資訊28at.com

備注8OX28資訊網——每日最新資訊28at.com

參數8OX28資訊網——每日最新資訊28at.com

OTLP Exporter8OX28資訊網——每日最新資訊28at.com

通過 OpenTelemetry Protocol(OTLP) 發送指標數據到 collect。8OX28資訊網——每日最新資訊28at.com

默認生產環境中推薦使用,需要將數據發送到支持 OTLP 的后端,如 OpenTelemetry Collector。8OX28資訊網——每日最新資訊28at.com

-Dotel.metrics.exporter=otlp (default)8OX28資訊網——每日最新資訊28at.com

Console Exporter8OX28資訊網——每日最新資訊28at.com

將指標數據打印到控制臺的導出器。8OX28資訊網——每日最新資訊28at.com

開發和調試,快速查看指標數據。8OX28資訊網——每日最新資訊28at.com

-Dotel.metrics.exporter=console8OX28資訊網——每日最新資訊28at.com

Prometheus Exporter8OX28資訊網——每日最新資訊28at.com

將指標數據以 Prometheus 抓取的格式暴露給 Prometheus 服務。8OX28資訊網——每日最新資訊28at.com

與 Prometheus 集成,適用于需要 Prometheus 監控的場景,這個可以無縫和以往使用 prometheus 的場景兼容8OX28資訊網——每日最新資訊28at.com

-Dotel.metrics.exporter=prometheus8OX28資訊網——每日最新資訊28at.com

Metric Instruments

與 prometheus 類似,OpenTelemetry 也提供了以下幾種指標類型:8OX28資訊網——每日最新資訊28at.com

  • Counter:單調遞增計數器,比如可以用來記錄訂單數、總的請求數。
  • UpDownCounter:與 Counter 類似,只不過它可以遞減。
  • Gauge:用于記錄隨時在變化的值,比如內存使用量、CPU 使用量等。
  • Histogram:通常用于記錄請求延遲、響應時間等。

同時每個指標還有以下幾個字段:8OX28資訊網——每日最新資訊28at.com

  • Name:名稱,必填。
  • Kind:類型,必填。
  • Unit:單位,可選。
  • Description:描述,可選。
messageInCounter = meter          .counterBuilder(MESSAGE_IN_COUNTER)          .setUnit("{message}")          .setDescription("The total number of messages received for this topic.")          .buildObserver();

還是以 Pulsar 的為例,messageInCounter 是一個記錄總的消息接收數量的 Counter 類型。8OX28資訊網——每日最新資訊28at.com

subscriptionCounter = meter          .upDownCounterBuilder(SUBSCRIPTION_COUNTER)          .setUnit("{subscription}")          .setDescription("The number of Pulsar subscriptions of the topic served by this broker.")          .buildObserver();

這是記錄一個訂閱者數量的指標,類型是 UpDownCounter,也就是可以增加減少的指標。8OX28資訊網——每日最新資訊28at.com

private static final List<Double> latencyHistogramBuckets =          Lists.newArrayList(.0005, .001, .0025, .005, .01, .025, .05, .1, .25, .5, 1.0, 2.5, 5.0, 10.0, 30.0, 60.0);DoubleHistogramBuilder builder = meter.histogramBuilder("pulsar.client.producer.message.send.duration")          .setDescription("Publish latency experienced by the application, includes client batching time")          .setUnit(Unit.Seconds.toString())          .setExplicitBucketBoundariesAdvice(latencyHistogramBuckets);

這是一個記錄 Pulsar producer 發送延遲的指標,類型是 Histogram。8OX28資訊網——每日最新資訊28at.com

backlogQuotaAge = meter          .gaugeBuilder(BACKLOG_QUOTA_AGE)          .ofLongs()          .setUnit("s")          .setDescription("The age of the oldest unacknowledged message (backlog).")          .buildObserver();

這是一個記錄最大 unack 也就是 backlog 時間的指標,類型是 Gauge。8OX28資訊網——每日最新資訊28at.com

案例

在之前的文章:實戰:如何編寫一個 OpenTelemetry Extensions中講過如何開發一個 OpenTelemetry 的 extension,其實當時我就是開發了一個用于在 Pulsar 客戶端中暴露指標的一個插件。8OX28資訊網——每日最新資訊28at.com

不過目前 Pulsar 社區已經集成了該功能。8OX28資訊網——每日最新資訊28at.com

其中的核心代碼與上面講到的類似:8OX28資訊網——每日最新資訊28at.com

public static void registerObservers() {        Meter meter = MetricsRegistration.getMeter();            meter.gaugeBuilder("pulsar_producer_num_msg_send")                .setDescription("The number of messages published in the last interval")                .ofLongs()                .buildWithCallback(                        r -> recordProducerMetrics(r, ProducerStats::getNumMsgsSent));private static void recordProducerMetrics(ObservableLongMeasurement observableLongMeasurement, Function<ProducerStats, Long> getter) {        for (Producer producer : CollectionHelper.PRODUCER_COLLECTION.list()) {            ProducerStats stats = producer.getStats();            String topic = producer.getTopic();            if (topic.endsWith(RetryMessageUtil.RETRY_GROUP_TOPIC_SUFFIX)) {                continue;            }        observableLongMeasurement.record(getter.apply(stats),                    Attributes.of(PRODUCER_NAME, producer.getProducerName(), TOPIC, topic));        }}

只是這里使用了 buildWithCallback 回調函數,OpenTelemetry 會每隔 30s 調用一次這個函數,通常適用于 Gauge 類型的數據。8OX28資訊網——每日最新資訊28at.com

java -javaagent:opentelemetry-javaagent.jar /       -Dotel.javaagent.extensinotallow=ext.jar  /     -Dotel.metrics.exporter=prometheus /     -Dotel.exporter.prometheus.port=18180 /     -jar myapp.jar

配合上 Prometheus 的兩個啟動參數就可以在本地 18180 中獲取到指標數據:8OX28資訊網——每日最新資訊28at.com

curl http://127.0.0.1:18180/metrics

當然也可以直接發往 OpenTelemetry-Collector 中,再由它發往 prometheus,只是這樣需要額外在 collector 中配置一下:8OX28資訊網——每日最新資訊28at.com

exporters:  debug: {}  otlphttp:    metrics_endpoint: http://promethus:8480/insert/0/opentelemetry/api/v1/pushservice:  pipelines:    metrics:      exporters:      - otlphttp      processors:      - k8sattributes      - batch      receivers:      - otlp

圖片圖片8OX28資訊網——每日最新資訊28at.com

這樣我們就可以在 Grafana 中通過 prometheus 查詢到數據了。8OX28資訊網——每日最新資訊28at.com

有一點需要注意,如果我們自定義的指標最好是參考官方的語義和命名規范來定義這些指標名稱。8OX28資訊網——每日最新資訊28at.com

圖片圖片8OX28資訊網——每日最新資訊28at.com

比如 OpenTelemetry 的規范中名稱是用 . 來進行分隔的。8OX28資訊網——每日最新資訊28at.com

切換為 OpenTelemetry 之后自然就不需要依賴 prometheus 的包,取而代之的是 OTel 的包:8OX28資訊網——每日最新資訊28at.com

compileOnly 'io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi:1.34.1'  compileOnly 'io.opentelemetry.instrumentation:opentelemetry-instrumentation-api:1.32.0'

總結

相對來說 Metrics 的使用比 Trace 簡單的多,同時 Metrics 其實也可以和 Trace 進行關聯,也就是 Exemplars,限于篇幅就不在本文展開了,感興趣的可以自行查閱。8OX28資訊網——每日最新資訊28at.com

參考鏈接:8OX28資訊網——每日最新資訊28at.com

  • https://github.com/apache/pulsar/blob/master/pulsar-client/src/main/java/org/apache/pulsar/client/impl/metrics/InstrumentProvider.java
  • https://opentelemetry.io/docs/specs/semconv/general/metrics/
  • https://opentelemetry.io/docs/specs/otel/metrics/data-model/#exemplars

本文鏈接:http://www.www897cc.com/showinfo-26-93865-0.html從 Prometheus 到 OpenTelemetry:指標監控的演進與實踐

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 23k star超火項目,請求優化寫的一塌糊涂!我直接重構!

下一篇: 有點東西,Template可以直接使用Setup語法糖中的變量原來是因為這個

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 高邑县| 万载县| 淮滨县| 咸阳市| 胶州市| 房山区| 紫金县| 山西省| 楚雄市| 新源县| 孟连| 开鲁县| 阿尔山市| 宜川县| 伊宁市| 明星| 峨眉山市| 阜阳市| 张家港市| 曲靖市| 缙云县| 乌兰察布市| 兴城市| 司法| 石渠县| 延长县| 高碑店市| 车致| 芜湖市| 浦北县| 襄垣县| 信丰县| 沛县| 达尔| 南平市| 凤翔县| 错那县| 南郑县| 石城县| 长沙县| 慈利县|