はじめに
New Relicではクラウド連携機能で主要パブリッククラウドと連携し、さまざまなマネージドサービスの情報を連携することができます。一方で、Google Cloudでは一部Metricsが連携に対応していません。
そこで、New Relic Flexを活用し、Cloud Monitoring APIを通じて情報を取得し連携する方法を解説します。
New Relic Flex とは
New Relic FlexはInfrastructure Agentにバンドルされる形で提供されており、独自のイベントやメトリクスを定期的に収集し、New Relicに送信することができます。設定ファイルのフォーマットに合わせることで、煩雑な処理を書かずに簡単に情報を送信できます。
監視設定
アーキテクチャ
本記事ではGoogle Cloud環境内にCompute Engine VMを1台構成し、定期的にCloud Monitoring APIから情報を取得、New Relicに送信します。情報取得の対象として、Cloud SQL for MySQLを1台構成しています。
それでは実際に、監視環境を構成していきましょう。
Metric Collector VMインスタンスの作成
Metricsを収集するマシンを作成します。
今回はテスト用のため、最低限のマシンを作成します。
Image: debian-cloud/debian-11
MachineType: e2-micro
作成されたVMには、Cloud Monitoring APIへのアクセスが許可されたService Accountを設定してください。
また、Google Cloudプロジェクトを作成しMonitoring APIを有効化していない方は、忘れずにAPIを有効化してください。
New Relic Infrastructure Agentの導入
Infrastructure AgentはNew RelicのWebUIよりGuided Installに沿った形でインストールします。
curl -Ls https://download.newrelic.com/install/newrelic-cli/scripts/install.sh | bash && sudo NEW_RELIC_API_KEY=NRAK-************** NEW_RELIC_ACCOUNT_ID=******** /usr/local/bin/newrelic install
New Relic Flexの設定
Flexは直接コマンドやスクリプトを実行できます。
本記事ではCloud MonitoringのMetricsを取得するためにスクリプトを記述しますが、Gcloudコマンド経由で返されたjson形式の値を扱ったりURLエンコードを行う必要があるため、jqコマンドが必要です。
debian-cloud/debian-11 ではjqコマンドが導入済みの状態で提供されていますが、もしも未導入の環境の場合にはそのままでは動きませんので、jqコマンドを別途インストールしてください。
Metric取得スクリプトの作成
Metric取得スクリプトを作成します。
本スクリプトでは例として、monitoring API v3経由でMetrics database/mysql/innodb/buffer_pool_read_requests_count
を取得しています。
#!/bin/bash
TOKEN=$(gcloud auth print-access-token)
PROJECT_ID="プロジェクトID"
API="cloudsql.googleapis.com"
METRIC_NAME="database/mysql/innodb/buffer_pool_read_requests_count"
METRIC_NAME_ENCORDED=`echo $METRIC_NAME | jq -Rr @uri`
START_TIME=`date +'%Y-%m-%dT%H:%M:%SZ' -d '5 minutes ago'| jq -Rr @uri`
END_TIME=`date +'%Y-%m-%dT%H:%M:%SZ' | jq -Rr @uri`
METRICS=`curl -s \
'https://monitoring.googleapis.com/v3/projects/'$PROJECT_ID'/timeSeries?filter=metric.type%20%3D%20%22'$API'%2F'$METRIC_NAME_ENCORDED'%22&interval.endTime='$END_TIME'&interval.startTime='$START_TIME'&pageSize=1' \
--header 'Authorization: Bearer '$TOKEN'' \
--header 'Accept: application/json' \
--compressed`
echo $METRICS | jq -r '.timeSeries[] | {(.metric.type): (.points[].value.int64Value | tonumber)}' | sed -e 's/'$API'\///g'
Flex設定ファイルの作成
次に、Flexを扱う設定ファイルを作成し、/etc/newrelic-infra/integrations.d/
配下に設置します。
設定ファイル内では先の手順で作成したスクリプトを実行対象に指定しています
integrations:
- name: nri-flex
config:
name: gcloudIntegration
apis:
- name: cloudsql.googleapis.com
commands:
- run: '/opt/newrelic-flex/monitor.sh'
設定確認
設定されたFlexがただしく動いているかは、CLIから確認することができます。
もし何らかのエラーが発生している場合は、出力結果に従い修正してください。
/opt/newrelic-infra/newrelic-integrations/bin/nri-flex -verbose -pretty -config_path /etc/newrelic-infra/integrations.d/nri-flex.yaml
DEBU[0000] Function.isAvailable: enter
DEBU[0000] Function.isAvailable: exit status: false
INFO[0000] com.newrelic.nri-flex GOARCH=amd64 GOOS=linux version=1.16.4
DEBU[0000] config: git sync configuration not set
WARN[0000] config: testing agent config, agent features will not be available
DEBU[0000] config: running async name=gcloudIntegration
DEBU[0000] config: processing apis apis=1 name=gcloudIntegration
DEBU[0000] fetch: collect data name=gcloudIntegration
DEBU[0000] commands: executing count=1 name=gcloudIntegration
DEBU[0001] processor-data: running data handler name=gcloudIntegration
DEBU[0001] config: finished variable processing apis apis=1 name=gcloudIntegration
INFO[0001] flex: completed processing configs configs=1
{
"name": "com.newrelic.nri-flex",
"protocol_version": "3",
"integration_version": "1.16.4",
"data": [
{
"metrics": [
{
"database/mysql/innodb/buffer_pool_read_requests_count": 861,
"event_type": "cloudsql_googleapis_comSample",
"integration_name": "com.newrelic.nri-flex",
"integration_version": "1.16.4"
},
{
"event_type": "flexStatusSample",
"flex.Hostname": "gcp-okawa001",
"flex.IntegrationVersion": "1.16.4",
"flex.counter.ConfigsProcessed": 1,
"flex.counter.EventCount": 1,
"flex.counter.EventDropCount": 0,
"flex.counter.cloudsql_googleapis_comSample": 1,
"flex.time.elapsedMs": 1014,
"flex.time.endMs": 1742959188518,
"flex.time.startMs": 1742959187504
}
],
"inventory": {},
"events": []
}
]
}
データの確認
データの確認は、Metrics & Eventsから行えます。
正常にデータが送信できていればEvents内の「Custom events」内に送信されたSampleが表示されます。
データ内の必要な情報は、Plotから確認できます。ここではscript内で設定されたdatabase/mysql/innodb/buffer_pool_read_requests_count
を確認できるかと思います。
まとめ
以上で、Cloud Monitoring APIから情報取得を行いNew Relicへ送信、確認するまでの一通りの流れを確認いただけたかと思います。
scriptとしては一つのデータのみを取得する形のシンプルな形となりますが、より複雑な情報取得にもチャレンジいただけますと幸いです
その他
New Relicでは、新しい機能やその活用方法について、QiitaやXで発信しています!
無料でアカウント作成も可能なのでぜひお試しください!
New Relic株式会社のX(旧Twitter) や Qiita OrganizationOrganizationでは、
新機能を含む活用方法を公開していますので、ぜひフォローをお願いします。
無料のアカウントで試してみよう!
New Relic フリープランで始めるオブザーバビリティ!