This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
62 lines (52 loc) · 2.01 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'sinatra'
require 'net/http'
require 'uri'
require 'google/api_client'
get '/' do
end
get '/post' do
# Update these to match your own apps credentials
service_account_email = ENV['SERVICE_ACCOUNT_EMAIL'] # Email of service account
profile_id = ENV['PROFILE_ID'] # Analytics profile ID.
# Get the Google API client
client = Google::APIClient.new(
:application_name => 'current visitor of Google Analytics post to mackerel.io',
:application_version => '0.0.3'
)
key = OpenSSL::PKey::RSA.new(ENV['GOOGLE_API_KEY'].gsub("\\n", "\n"))
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
:issuer => service_account_email,
:signing_key => key,
)
# Request a token for our service account
client.authorization.fetch_access_token!
# Get the analytics API
analytics = client.discovered_api('analytics','v3')
# Execute the query, get the value like `[["1"]]`
response = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
'ids' => "ga:" + profile_id,
'metrics' => "ga:activeVisitors",
}).data.rows
number = response.empty? ? 0 : response.first.first.to_i
payload = [ {
name: "current_visitors.#{ENV['WEBSITE_NAME']}",
time: Time.now.to_i,
value: number,
} ].to_json
uri = URI.parse("https://mackerel.io/api/v0/services/#{ENV['MACKEREL_SERVICE_NAME']}/tsdb")
Net::HTTP.new(uri.host, uri.port).tap do |https|
https.use_ssl = true
req = Net::HTTP::Post.new(uri.request_uri).tap do |q|
q['Content-Type'] = 'application/json'
q['X-Api-Key'] = ENV['MACKEREL_API_KEY']
q.body = payload
end
res = https.request(req)
status res.code
headers 'Content-Type' => 'application/json'
body "#{res.body}"
end
end