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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
600
Goのスライス容量拡張量がどのように決まるのか追った / 180713 LT
kaznishi
3
3.8k
スライス容量拡張量がどのように決まるのか追った / 180709 LT
kaznishi
0
190
Other Decks in Programming
See All in Programming
go-spidermonkeyでAIエージェントのCode Modeを実装する
syumai
3
1.5k
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
520
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
0
220
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
0
150
業務時間外もAIに働いてもらう話
colorful12
3
10k
Security issues being discussed on Web Platforms
petamoriken
0
830
標準パッケージに uuid が追加された 背景から見る Go らしい意思決定 / go_127_uuid_decision
convto
5
6.4k
Foundry Localでエージェント開発
seosoft
0
180
GKE で Pod の見方を変えたら、スケールアウト時の挙動を真に捉えられた話
stkk
0
110
GKE アップグレード前に知っておきたい Blue/Green と PDB の関係
stkk
0
160
選挙速報を多くのユーザーへ 届ける Live Activities 設計
hamayokokuririn
0
130
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
450
Featured
See All Featured
HDC tutorial
michielstock
2
860
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
700
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
240
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.8k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
270
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.8k
What does AI have to do with Human Rights?
axbom
PRO
1
2.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Facilitating Awesome Meetings
lara
57
7.1k
The Cost Of JavaScript in 2023
addyosmani
55
10k
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作ってみて下さい
おわり