Upgrade to Pro — share decks privately, control downloads, hide ads and more …

mackerel-client-php

 mackerel-client-php

Mackerel Meetup #9のLT資料です

Takuya Arita

January 26, 2017
Tweet

More Decks by Takuya Arita

Other Decks in Technology

Transcript

  1. 使ってみる (READMEに記載) <?php
 
 require __DIR__ . '/vendor/autoload.php';
 
 $client

    = new \Mackerel\Client([
 'mackerel_api_key' => 'YOUR_MACKEREL_API_KEY_HERE',
 ]);
 $host = $client->getHost('HOST_ID');

  2. 使ってみる (メトリクスを送ってみる) <?php
 
 require __DIR__ . '/vendor/autoload.php';
 
 $client

    = new \Mackerel\Client([
 'mackerel_api_key' => 'YOUR_MACKEREL_API_KEY_HERE',
 ]);
 $host = $client->getHost('HOST_ID');
 $metric = [
 'hostId' => $host->id,
 'time' => time(),
 'name' => 'metrics.meetup.random',
 'value' => mt_rand(1, 6),
 ];
 var_dump($metric);
 $client->postMetrics([$metric]);
 ※公式ドキュメント:ホストのカスタムメトリックを投稿するを参考にしました
  3. Mackerelでがんばり具合を 可視化するコード例 <?php
 
 require __DIR__ . '/vendor/autoload.php';
 
 function

    getActiveUsersCount()
 {
 $client = new GuzzleHttp\Client();
 $resp = $client->get('https://slack.com/api/users.list', [
 'query' => [
 'token' => ‘YOUR_SLACK_TOKEN’,
 'presence' => '1',
 ],
 ]);
 $presence = json_decode($resp->getBody()->getContents());
 return count(array_filter($presence->members, function ($m) {
 return $m->presence == 'active';
 }));
 }
 
 $client = new \Mackerel\Client([
 'mackerel_api_key' => 'YOUR_MACKEREL_API_KEY_HERE',
 ]);
 $host = $client->getHost('HOST_ID');
 $metric = [
 'hostId' => $host->id,
 'time' => time(),
 'name' => 'metrics.slack.active',
 'value' => getActiveUsersCount(),
 ];
 var_dump($metric);
 $client->postMetrics([$metric]);