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
700
バッチ処理と冪等性 / 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
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
120
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
200
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
0
120
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
200
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
480
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
260
【SRE NEXT 2026 Lunch Session】一人目専任SREの立ち上げを加速する ― AIと進めたオンボーディングで2分を0.04秒にした話
pkshadeck
PRO
0
3.3k
作るコストが小さくなった時代 幸せに働くために改めて考えたいこと 〜エンジニアとして価値を出し続けるために注視している二分野〜
yuppeeng
0
140
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
190
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
300
Android CLI
fornewid
0
190
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
3.2k
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6k
How Software Deployment tools have changed in the past 20 years
geshan
1
34k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.2k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
540
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
How to Ace a Technical Interview
jacobian
281
24k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.2k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
360
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
640
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
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作ってみて下さい
おわり