Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Hello, Prometheus!! Goで作るexporter自作入門 / 180727 LT
Search
kaznishi
July 27, 2018
Programming
3.9k
6
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Hello, Prometheus!! Goで作るexporter自作入門 / 180727 LT
kaznishi
July 27, 2018
More Decks by kaznishi
See All by kaznishi
Finally_I_can_kichijojipm32
kaznishi
0
710
バッチ処理と冪等性 / 20191218_merpay_techtalk
kaznishi
3
5.6k
Bounds Check Eliminationについて調べてみた / 1218-lt
kaznishi
0
590
Goのスライス容量拡張量がどのように決まるのか追った / 180713 LT
kaznishi
3
3.8k
スライス容量拡張量がどのように決まるのか追った / 180709 LT
kaznishi
0
190
Other Decks in Programming
See All in Programming
20260828_品質と開発生産性を両立させる、AI時代のE2Eテストの考え方
magicpod
0
150
週末にAI-DLCを本気で回したら$1,600溶けた
hbashimizu
0
120
FastAPI の並行処理モデルを完全に理解する
hoto17296
9
3.7k
レビュー履歴をAIに食わせて、 Compose移行を加速するs
shihochan
0
260
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
5
1.7k
iOS開発×AI駆動開発 〜最近使って便利だったスキルの話〜
nogu66
0
140
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
500
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
560
Go を使い始めて 2 ヶ月の学び / My first two months with Go
contour_gara
0
420
初めての模倣学習とVLA
natsutan
0
470
Press start. Python's next generation.
willingc
PRO
3
300
私のClaude Code活用法 (個人開発編) - PHPerKaigi mini #4(2026/08/24)
panda_program
1
210
Featured
See All Featured
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
460
The Language of Interfaces
destraynor
162
27k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
840
Into the Great Unknown - MozCon
thekraken
41
2.7k
Building the Perfect Custom Keyboard
takai
2
860
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.5k
HDC tutorial
michielstock
2
850
Practical Orchestrator
shlominoach
191
12k
Believing is Seeing
oripsolob
1
210
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Transcript
Hello, Prometheus!! Goで作るexporter自作入門 2018-07-27 吉祥寺 .pm #15 LT by kaznishi
自己紹介 Twitter: @kaznishi1246 主な守備範囲: サーバーサイド,インフラ 主な使用言語: PHP, Scala 最近Goをプライベートで勉強してます
今回の話 Prometheusのサポートライブラリ(client_golang) を使ってexporterを自作してみる
Prometheusとは モニタリングツールの一種 OSS クラウド時代向けのツールであり、強力なサービ スディスカバリによってAutoScaling対応も簡単
Promethesのアーキテクチャ https://prometheus.io/docs/introduction/overview/ より図を引用
Promethesのアーキテクチャ Prometheusサーバがexporterにスクレイピングを 行い、データ収集をする。 ↑ node_exporterの出力例
exporterの種類 既に多くの種類のexporterがオフィシャル、サー ドパーティ問わず存在 https://prometheus.io/docs/instrumenting/export ers/ 代表的なものはサーバのメトリクスモニタリング を行うnode_exporterや、外形監視を行う blackbox_exporterなど
自作するには
自作するには 最終的にPrometheusサーバがスクレイピングでき る形式の出力を行うWebサーバになっていればよ いので、好きな言語で実装OK サポートライブラリが公開されている言語も https://prometheus.io/docs/instrumenting/clientli bs/ 今回はGoのサポートライブラリを使用する
Hello,World的に固定値を吐くだけの exporterを作ってみます repo: https://github.com/kaznishi/prometheus_expo rter_example
手順 メトリクスの記述子を作る Collectorを実装する Describeを実装する Collectを実装する メトリクス収集の中身を実装 CollectorをRegisterする エンドポイントを実装する
メトリクスの記述子を作る
メトリクスの記述子を作る https://github.com/prometheus/client_golang/blob/ master/prometheus/desc.go type Desc struct { fqName string help
string constLabelPairs []*dto.LabelPair variableLabels []string id uint64 dimHash uint64 err error }
メトリクスの記述子を作る var ( exampleCount = prometheus.NewCounter( prometheus.CounterOpts{ Namespace: namespace, Name:
"example_count", Help: "example counter help", }) exampleGauge = prometheus.NewGauge( prometheus.GaugeOpts{ Namespace: namespace, Name: "example_gauge", Help: "example gauge help", }) ) NewCounter,NewGauge処理内でDesc生成
メトリクスの記述子を作る ちなみに プロメテウスのデータ形式は Counter,Gauge,Histogram,Summaryの4種類。 入門としてはCounter,Gaugeを押さえておけばよ い。 Counter...単調増加する値(ex. 累計アクセス数) Gauge...上下する値(ex. リアルタイムアクセス数)
Collectorを実装する
Collectorインタフェース https://github.com/prometheus/client_golang/blob/ master/prometheus/collector.go type Collector interface { Describe(chan<- *Desc) Collect(chan<-
Metric) } Describe メトリクスの記述子(Desc)を送信する Collect メトリクスを送信する
Collectorを実装する type myCollector struct {}
Collectorを実装する(Describe) func (c myCollector) Describe(ch chan<- *prometheus.Desc){ ch <- exampleCount.Desc()
ch <- exampleGauge.Desc() }
Collectorを実装する(Collect) func (c myCollector) Collect(ch chan<- prometheus.Metric){ dummyStaticNumber := float64(1234)
ch <- prometheus.MustNewConstMetric( exampleCount.Desc(), prometheus.CounterValue, float64(dummyStaticNumber), ) ch <- prometheus.MustNewConstMetric( exampleGauge.Desc(), prometheus.GaugeValue, float64(dummyStaticNumber), ) } exporterの要件によってdummyStaticNumberの部 分は適宜実装を変えて下さい
CollectorをRegisterする
エンドポイントを実装する
CollectorをRegisterする & エンドポイントを実装する var addr = flag.String( "listen-address", ":8080", "The
address to listen on for HTTP requests." ) func main() { flag.Parse() var c myCollector prometheus.MustRegister(c) http.Handle("/metrics", promhttp.Handler()) log.Fatal(http.ListenAndServe(*addr, nil)) }
結果
結果 Prometheusがスクレイピングできる形式になって る!
とりあえず固定値を吐くexporterは実装できた
が、それだけでは面白くないので 別のexporterを作ってきた
openweathermap_exporter
OpenWeatherMap 全世界の天気情報を公開 JSONを吐くAPIが存在
openweathermap_exporter APIから取得した気温等を出力するexporterを実装 した https://github.com/kaznishi/openweathermap_exp orter (実は車輪の再発明だが他に良いネタが思いつかな かった)
結果
ここ数日の気温がモニタリングできた! (可視化にはGrafanaを使用)
まとめ exporterの自作は難しくない! みなさんも機会があればexporter作ってみて下さい
おわり