1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

New Relic: NerdGraph API を使ってデータを取得する

Last updated at Posted at 2023-01-28

New Relic に送信したデータは、NerdGraph API という GraphQL を採用した API で参照することができます。こちらは参照用で、データ送信用とは区別されているようです。

API へのアクセス例

GraphQL API に対する一般的な方法になりますが、参考まで。

Altair GraphQL Client を使ってアクセスする

  1. Chrome に Altair GraphQL Client 拡張機能をインストールする
  2. Altair GraphQL Client を開き、URL に https://api.newrelic.com/graphql と入力する
  3. 左のナビゲーション内にある「Set Headers」アイコンをクリックし、以下のヘッダを追加する
    • Header key: Api-Key
    • Header value: (API キーを入力)

Query ペインにクエリを入力して「Send Request」でリクエスト、「Docs」を押すと API ドキュメントを参照することができます。

Curl でアクセスする

以下のようにします。

NEW_RELIC_USER_KEY=(API キー)
QUERY="{requestContext { userId apiKey }}"
curl -X POST "https://api.newrelic.com/graphql" \
    -H "Content-Type: application/json" \
    -H "Api-Key: ${NEW_RELIC_USER_KEY}" \
    -d "{\"query\":\"${QUERY}\"}"

クエリ例

上記 query の部分に、GraphQL としてのクエリを指定します。

アカウント情報を取得

{
  actor {
    organization {
      accountManagement {
        managedAccounts {
          name
          id
          regionCode
        }
      }
    }
  }
}

アカウント情報を取得

最新の値を取得

アカウント ID には、上で取得した id を指定します。

{
   actor {
      account(id: (アカウントID)) {
         nrql(query: "SELECT latest(temperature) AS temperature FROM Metric WHERE name = 'M5Stack-test'") {
            results
         }
      }
   }
}

最新の値を取得

チャートの画像を取得

{
   actor {
      account(id: (アカウントID)) {
         nrql(query: "SELECT min(temperature), max(temperature) FROM Metric WHERE name = 'M5Stack-test' SINCE 1 DAY AGO TIMESERIES") {
            staticChartUrl(width: 640, height: 320)
         }
      }
   }
}

チャートの画像を取得

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?