ãã㯠ã¯ã¦ãªã¨ã³ã¸ã㢠Advent Calendar 2024 24æ¥ç®ã®è¨äºã§ãã
ããã«ã¡ã¯ã id:masawada ã§ãã30代ã«çªå
¥ãã¦æ©ä¸å¹´ãã ãã ãã¨å¥åº·ãæ°ã«ãªãå§ãã¦ãã¾ããã¨ã³ã¸ãã¢ãããã®ç¡éã«ã¢ã¯ã·ã§ã³ãåãåã«ã¾ãã¯è¨æ¸¬ãã¨ãããã¨ã§iOSã«ããªã¤ã³ã¹ãã¼ã«ããã¦ãããã«ã¹ã±ã¢ã¢ããªã®æ
å ±ãMackerelã«éä¿¡ãã¦å¾åãçºããããããã«ãã¦ã¿ã¾ãã
(注æ: æ ªå¼ä¼ç¤¾ã¯ã¦ãªã¯PHRãµã¼ãã¹äºæ¥åä¼ã«å å
¥ãã¦ããæ³äººã§ã¯ããã¾ããããã®è¨äºã¯å¥åº·ã«é¢ãããã¼ã¿ãMackerelã«è¨é²ãããã¨ãå
¬å¼ã«æ¨å¥¨ãããã®ã§ã¯ããã¾ãããããã¾ã§å人å©ç¨ã»èªå·±è²¬ä»»ã®ç¯å²ã§åèã«ãã¦ãã ãã)
iOSã«ã¯ã·ã§ã¼ãã«ããã¨ããã¢ããªãããªã¤ã³ã¹ãã¼ã«ããã¦ãã¾ããã·ã§ã¼ãã«ããã§ã¯ãã¢ã¯ã·ã§ã³ãã¨å¼ã°ãããããã¯ãç©ã¿éãããã¨ã§ã¯ã¼ã¯ããã¼ãæ§ç¯ãããã¨ãã§ãã¾ãããã¢ã¯ã·ã§ã³ãã«ã¯ãã«ã¹ã±ã¢æ
å ±ãåå¾ãããã®ãç¹å®ã®URLã«HTTPãªã¯ã¨ã¹ããéããã®ãªã©ãç¨æããã¦ããã®ã§ãããããçµã¿åãããã°Mackerelã«å¤ãéä¿¡ã§ãã¾ãã
Apple Watchãçç¨ãã¦ããå ´åãå¿ææ°ãæ©æ°ãªã©ã®ã¡ããªãã¯ããã«ã¹ã±ã¢ã¢ããªã«èªåã§è¨é²ããã¾ããã¾ãããã«ã¹ã±ã¢ã¢ããªã¨é£æºãããµã¼ããã¼ãã£ã¢ããªãå©ç¨ãã¦ããå ´åã¯ããããã«è¨é²ãããã¡ããªãã¯ãèªåã§å¸ãåºãã¦éç´ãã¦ããã¾ãããã¨ãã°èªåã¯ãªã ãã³è£½ã®è¡å§è¨ãä½éè¨ãä½æ¸©è¨ãå©ç¨ãã¦ãããOMRON Connectã¨ããã¢ããªãçµç±ãã¦ãã«ã¹ã±ã¢ã¢ããªã«ã¡ããªãã¯ãéç´ãã¦ãã¾ããä»åã¯å¿ææ°ãéä¿¡ããä¾ããç´¹ä»ãã¾ãã
ã·ã§ã¼ãã«ããã¢ããªãå®éã«è§¦ãåã«ãã¾ãã¯Mackerelã«ã©ãå¤ãéä¿¡ããã確èªãã¦ããã¾ããããä»åã¯ãµã¼ãã¹ã¡ããªãã¯ã¨ãã¦æ
å ±ãä¿åãããã¨ã«ãã¾ãã
mackerel.io
Mackerelã§äºåã«Serviceãä½æãã¦ãcurlã§ä»¥ä¸ã®ãããªãªã¯ã¨ã¹ããéãã¨å¤ãæ稿ãããã¨ãã§ãã¾ãã
curl -H 'Content-Type: application/json' \
-H 'X-Api-Key: <apiKey>' \
'https://api.mackerelio.com/api/v0/services/<serviceName>/tsdb' \
-d '[{ "name": "heart_rate.count", "time": 1734966000, "value": 80 }]'
ãã«ã¹ã±ã¢ã®ã¡ããªãã¯ãåå¾ãããããJSONå½¢å¼ã«å å·¥ãã¦ãã®URLã«POSTããã®ãä»åã®ã´ã¼ã«ã§ããã¨ããããã§ãã¾ãã¯ãã«ã¹ã±ã¢ã®ã¡ããªãã¯ãåå¾ãã¾ãã
ã·ã§ã¼ãã«ããã¢ããªãéãã¦æ°è¦ã«ã·ã§ã¼ãã«ãããä½æãããã«ã¹ã±ã¢ãµã³ãã«ãæ¤ç´¢ãã¢ã¯ã·ã§ã³ãé
ç½®ãã¾ãããããã¯ã¤ãã¯ã«ãã¯ãã¢ã¯ã·ã§ã³ãæ¥ç¶ãããã¨ã§åå¾ããå¤ã表示ãããã¨ãã§ãããããã°ã«ä¾¿å©ã§ããMackerelã®ãµã¼ãã¹ã¡ããªãã¯ã¯æ大ã§éå»24æéåã®å¤ãè¨é²ã§ããã®ã§ãæ¤ç´¢ããç¯å²ã¯éå»1æ¥ã¨ãã¦å¿ææ°ãåå¾ãã¦ã¿ã¾ããä¸æ¦æ
å ±ãåå¾ã§ãã¦ãããã¨ã ãã確ããããã®ã§1件ã ãåå¾ãããã¨ã«ãã¾ãã
ãããå®è¡ããã¨çµæã表示ããã¾ãã
é
ã飲ãã å¾ã ã£ãã®ã§å¿ææ°ãããã«é«ãã§ãããããã ãã§ä¸å¥åº·ããããåããã¾ãã
å¤ãåå¾ã§ãããã¨ã¯åãã£ãã®ã§ããããå å·¥ãã¦JSONã«ãã¾ãããããåé
ç®ãç¹°ãè¿ããã¨ããã¢ã¯ã·ã§ã³ãå©ç¨ããã¨ãããããmapçãªå¦çãå®ç¾ãããã¨ãã§ãã¾ãããããé
ç½®ãã¦å
¥åã®å¼æ°ãããã«ã¹ã±ã¢ãµã³ãã«ãã«æå®ãã¾ããç¹°ãè¿ãã®ä¸ã«ããã«ã¹ã±ã¢ãµã³ãã«ã®è©³ç´°ãåå¾ãã¢ã¯ã·ã§ã³ããµãã¤é
ç½®ãããç¹°ãè¿ãé
ç®ããããéå§æ¥ããå¤ããããããåå¾ãã¾ãããããã¹ããã¢ã¯ã·ã§ã³ãé
ç½®ããã¨ãéå§æ¥ã¨å¤ãå¤æ°ã¨ãã¦åãè¾¼ãã ããã¹ãããã©ã¼ãããã§ãã¾ããããã¹ãã®å
容ã¨ã㦠{"name": "hr.count", "time": éå§æ¥, "value": å¤}
ãè¨å®ãããã¨ã§ãJSONãçæãã¾ãã
ãã®çµæããã¯ã¤ãã¯ã«ãã¯ãã¢ã¯ã·ã§ã³ã§è¦ã¦ã¿ã¾ãããã
ããã£ã½ãJSONã表示ããã¾ãããç¾æ®µéã§ã¯ãmapããçµæã®itemãã²ã¨ã¤ãã¤è¡¨ç¤ºããã¦ãã¾ããã¾ããtimeã®å¤ãepochç§ã«ãªã£ã¦ãã¾ããããã®ã¾ã¾ã§ã¯ãªã¯ã¨ã¹ãããã£ã¨ãã¦å©ç¨ã§ããªãã®ã§ä¿®æ£ãã¦ããã¾ãã
ã¾ãçµæãçµåãã¦é
åã«ãã¾ãããããã¹ããçµåãã¢ã¯ã·ã§ã³ãç¹°ãè¿ãã®çµäºã®å¾ã«é
ç½®ããç¹°ãè¿ãã®çµæãã«ã³ãã§çµåãã¾ããããã«ãçµåãã¦ã§ããæååããããã¹ããã¢ã¯ã·ã§ã³ã«åãè¾¼ã㧠[]
ã§å²ãã¾ãã
次ã«æå»ãepochç§ã«å¤æãã¾ããç¹°ãè¿ãé
ç®ããéå§æ¥ãåå¾ããã¢ã¯ã·ã§ã³ã®æ¬¡ã«ã端æ°ãå¦çãã¢ã¯ã·ã§ã³ãæ¥ç¶ãã¾ããæ°å¤è¨ç®ç³»ã¢ã¯ã·ã§ã³ã®å¼æ°ã«æ¥ä»ãå
¥ããã¨epochç§(å°æ°é¨5æ¡ã®ç²¾åº¦)ã«ãã£ã¹ããããä»çµã¿ãå©ç¨ãã¦ãã¾ããåæ¨äºå
¥ã¨è¡¨ç¤ºããã¾ããã常ã«åãæ¨ã¦ãã¢ã¼ããé¸ã¶ãã¨ãã§ãã¾ããå¾ç¶ã®ãããã¹ããã¢ã¯ã·ã§ã³å
ã®ãéå§æ¥ããã端æ°å¦çæ¸ã®æ°å¤ãã«ç½®ãæãã¾ããããããã§epochç§ãæ¿å
¥ãããã¨ãã§ãã¾ãã
ããã¾ã§ã§Mackerelã«éä¿¡ããJSONãçæãããã¨ãã§ãã¾ããããã¾ãæ§æã§ãã¦ããã°ä»¥ä¸ã®ããã«ãªãã¯ãã§ããè¦ãããããã«ç´è¿ã®3件ããµã³ããªã³ã°ãã¦ãã¾ãããå®éã«éä¿¡ããéã¯ãã«ã¹ã±ã¢ãµã³ãã«ãåå¾ããã¢ã¯ã·ã§ã³ã®é
ç®æ°ãå¶éãããã§ãã¯ãå¤ãã¦ãã¾ãã
ããããã¯çæããå¤ãMackerelã«éä¿¡ãã¾ããHTTPãªã¯ã¨ã¹ãã«ã¯ãURLã®å
容ãåå¾ãã¢ã¯ã·ã§ã³ãå©ç¨ãã¾ãããã®ã¢ã¯ã·ã§ã³ãæå¾ã«æ¥ç¶ãã¾ãããããµã¼ãã¹ã¡ããªãã¯æ稿APIã¯POSTã§ãªã¯ã¨ã¹ããéãã®ã§ãæ¹æ³(HTTPã¡ã½ãã)ã¯POSTã§ããéä¿¡ããã®ã¯JSONã§ããããæ¬æãè¦æ±ããJSONã«ããã¨å¤ãéããã¨ãã§ãã¾ããããµã¼ãã¹ã¡ããªãã¯æ稿APIã§ã¯ããããã¬ãã«ãé
åå½¢å¼ã®JSONããªã¯ã¨ã¹ãããã£ã¨ãã¦è¦æ±ããã¦ãã¾ããããããURLã®å
容ãåå¾ãã¢ã¯ã·ã§ã³ã§JSONãéä¿¡ãããã¨ããã¨ãããã¬ãã«ãè¾æ¸å½¢å¼ã®ãªã¯ã¨ã¹ãã«ãªãã¾ãããããåé¿ãããããæ¬æãè¦æ±ãã¯ããã¡ã¤ã«ãã«å¤æ´ããå¼æ°ã¨ãã¦çæããJSONã®ããã¹ããé¸æãã¾ããã¾ãããããã¨ã㦠Content-Type
ã application/json
ã«ã X-Api-Key
ãMackerelããåå¾ããAPIãã¼ã«ããããè¨å®ãã¾ããããããæ¸ã¾ããã¨ä»¥ä¸ã®ãããªå½¢ã«ãªã£ã¦ããã¯ãã§ãã
試ãã«ããã§å®éã«ã©ããããªã¯ã¨ã¹ããéãããã®ã確èªãã¦ã¿ã¾ããããnc
ã³ãã³ã*1ã§HTTPãªã¯ã¨ã¹ããå¾
ã¡åãã¦æ§åãçºãã¦ã¿ã¾ãã
$ ( echo "HTTP/1.0 200 OK"; echo; echo "it works!" ) | nc -l -p 8080
POST / HTTP/1.1
Host: 192.168.0.56:8080
Content-Type: application/json
Connection: keep-alive
X-Api-Key: dummy_api_key
Accept: */*
Accept-Language: ja
Content-Length: 172
Accept-Encoding: gzip, deflate
User-Agent: BackgroundShortcutRunner/3218.0.9 CFNetwork/1568.300.101 Darwin/24.2.0
[{ "name": "hr.count", "time": 1734964612, "value": 141 },{ "name": "hr.count", "time": 1734964603, "value": 140 },{ "name": "hr.count", "time": 1734964598, "value": 139 }]
å®éã«Mackerelã«éä¿¡ãããå
容ã¨åçã®ãªã¯ã¨ã¹ãã«ãªã£ã¦ãããã¨ã確èªã§ãã¾ãããã¨ãããã¨ã§ããã¨ã¯URLãMackerel APIã®ãã®ã«ç½®ãæããã°å®æã§ããå®éã«æ稿ããã¨ä»¥ä¸ã®ãããªã°ã©ããçºããããããã«ãªããã¨æãã¾ãã
å¿æã®è½ã¡çãã¦ããã¿ã¤ãã³ã°ãè¦ãã«ãæ¨æ¥ã¯5æéãã«å¯ã¦10æã¡ãã£ã¨åã«èµ·ãã¦ããã®ããããããã¾ããæ¥å¹´ã¯ãããã£ãã¡ããªã¯ã¹ãé§ä½¿ãã¦å¥åº·ã«ãªãããããªã¢ã¯ã·ã§ã³ãåã£ã¦ããããã§ããã
ææ¥ã®æ
å½ã¯ id:yujiorama ããã§ãã