Skip to content

Instantly share code, notes, and snippets.

@mikeda
Last active December 30, 2020 14:34
Show Gist options
  • Save mikeda/4406576 to your computer and use it in GitHub Desktop.
Save mikeda/4406576 to your computer and use it in GitHub Desktop.
GrowthForecastでAPIを使って複合グラフを作るサンプル
#!/usr/lib64/fluent/ruby/bin/ruby
# -*- encoding: utf-8 -*-
# GrowthForecastのAPIを使って複合グラフを作るサンプル
# 仕様はこのへん読もう
# https://github.com/kazeburo/GrowthForecast/blob/master/lib/GrowthForecast/Web.pm
#
# <service_name>/access/{2xx_count,3xx_count,4xx_count,5xx_count}
# ↓
# <service_name>/access/access_status
#
# <service_name>/access/{avg,percentile_95}
# ↓
# <service_name>/access/response_time
#
# な感じにまとめる
require 'net/http'
require 'json'
require 'pp'
GF_HOST = 'localhost'
GF_PORT = 5125
complex_settings = [
{
:graphs => %w(2xx_count 3xx_count 4xx_count 5xx_count),
:graph_name => 'access_status',
:description => 'アクセス数(レスポンスコード)',
:sort => 10,
:gmode => 'gauge',
:stack => true,
:type => 'AREA',
},
{
:graphs => %w(avg percentile_95),
:graph_name => 'response_time',
:description => 'レスポンスタイム',
:sort => 8,
:gmode => 'gauge',
:stack => false,
:type => 'LINE1',
}
]
def api_get(path)
json = ''
Net::HTTP.start(GF_HOST, GF_PORT) do |http|
res = http.get(path)
json = res.body
end
JSON.parse(json)
end
def api_post(path, data)
json = JSON.generate(data)
Net::HTTP.start(GF_HOST, GF_PORT) do |http|
res = http.post(path, json)
pp res.body
end
end
# 既存のグラフ、複合グラフの情報を取得
graphs = api_get('/json/list/graph')
complexes = api_get('/json/list/complex')
# まだ複合グラフがなさそうなサービス名を抜き出し
# all_で始まるサービスは除外
old_services = complexes.map{|c| c['service_name']}.uniq
new_services = graphs.map{|g| g['service_name']}.uniq.select{|name| !old_services.include?(name) and name !~ /^all_/}
# 複合グラフ作成
new_services.each do |new_service|
complex_settings.each do |setting|
data = []
setting[:graphs].each do |graph_name|
id = graphs.detect{|g| g['service_name'] == new_service && g['graph_name'] == graph_name}['id']
data << {
'gmode' => setting[:gmode],
'stack' => setting[:stack],
'type' => setting[:type],
'graph_id' => id
}
end
params = {
'service_name' => new_service,
# 'section_name' => setting[:section_name],
'section_name' => 'access',
'graph_name' => setting[:graph_name],
'description' => setting[:description],
'sort' => setting[:sort],
'data' => data
}
api_post('/json/create/complex', params)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment