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
600
Goのスライス容量拡張量がどのように決まるのか追った / 180713 LT
kaznishi
3
3.8k
スライス容量拡張量がどのように決まるのか追った / 180709 LT
kaznishi
0
190
Other Decks in Programming
See All in Programming
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
330
FreeBSDでZabbixを動かす
kenkino
0
340
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.7k
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
200
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
110
Turning Architecture into Unit Tests in the AI Era (NSSpain XIV)
steliosf
PRO
1
120
iOSDCのペンライトを自動制御したい!
akkeylab
0
310
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
600
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
580
すこし踏み込む CancellationToken
htkym
2
1.5k
一人だけ、Kiroが静止する日
hideg
0
140
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
174
15k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Optimising Largest Contentful Paint
csswizardry
37
4k
Optimizing for Happiness
mojombo
378
71k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
890
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Faster Mobile Websites
deanohume
310
32k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
340
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
850
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
470
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作ってみて下さい
おわり