Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
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
720
バッチ処理と冪等性 / 20191218_merpay_techtalk
kaznishi
3
5.7k
Bounds Check Eliminationについて調べてみた / 1218-lt
kaznishi
0
610
Goのスライス容量拡張量がどのように決まるのか追った / 180713 LT
kaznishi
3
3.8k
スライス容量拡張量がどのように決まるのか追った / 180709 LT
kaznishi
0
190
Other Decks in Programming
See All in Programming
golang-bob.pdf
iwatsukayura
0
130
Are APIs Still Relevant in the AI Era?
soyuka
0
460
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
380
[2026-09-26]空論ジェネリックプロセス~テスト資産とAIで紡ぐ、再現可能なパフォーマンスチューニングの話~
tosite
0
270
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
150
Kotlinの15年とServer-Side Kotlin
n_takehata
1
140
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
390
ChromeでローカルLLM
ryu1013
0
120
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
1
310
残高管理から台帳サービスへの進化
artoy
1
200
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
150
パーソナライズPodcastを作った話
repunit11
0
140
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
430
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
450
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
310
sira's awesome portfolio website redesign presentation
elsirapls
0
450
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7k
Prompt Engineering for Job Search
mfonobong
0
510
[SF Ruby Conf 2025] Rails X
palkan
3
1.4k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.8k
Docker and Python
trallard
47
4.2k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
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作ってみて下さい
おわり