チュートリアルアプリケーション内で実行中のサービスを取得する
このチュートリアルでは、 Instana ( REST API )を使用して、特定のアプリケーションに対して Instana によって監視されているサービスのリストを取得する手順を解説します。
コンテキスト
アプリケーションの監視と観測においては、アプリケーション内で実行されるサービスを理解することが重要です。 Instana の堅牢な API を使用すれば、サービスやメトリクスなどに関する詳細情報をプログラムから取得することができます。 このチュートリアルでは、 Instana REST API を使用する特定のアプリケーションについて、 Instana によって監視されているサービスのリストを取得する方法について説明します。
前提条件
このチュートリアルで特定された Instana および REST API のエンドポイントを使用するには、 「一般的な前提条件」 を参照してください。 このチュートリアルには、特別な前提条件は必要ありません。
API エンドポイント
このチュートリアルでは、エンドポイントの 「Application Resources 」グループにある 2 つの異なる「 API 」エンドポイントを使用します。
| エンドポイント | 説明 | 資料 | 必要なアクセス権 |
|---|---|---|---|
GET /api/application-monitoring/applications |
Instana によって監視されているアプリケーションのリストを取得します。 | アプリケーションを取得する | 一般的なアプリケーションの許可。 |
GET
/api/application-monitoring/applications;id={application_id}/services |
application_id を指定して、特定のアプリケーションのすべてのサービスを取得します。 |
GET application/services | 一般的なアプリケーションの許可。 |
チュートリアル
特定のアプリケーションについて、 Instana によって監視されているサービスのリストを取得するには、以下の2つの手順が必要です:
- 当該アプリケーションのアプリケーションIDを取得する
- アプリケーションIDを使用して、アプリケーションのサービスを取得します
アプリケーションIDの取得
アプリケーションのすべてのサービスを一覧表示するには、GETリクエストを/api/application-monitoring/applications終点。
上記の GET リクエストには次の詳細が含まれています。
GET /api/application-monitoring/applications/
Host: {tenant}-{unit}.instana.io
Authorization: apiToken {api_token}
Accept: application/json
アプリケーションのサービスを取得する
利用可能なアプリケーションIDを取得したら、エンド /api/application-monitoring/applications\;id\={application_id}/services ポイントにリクエスト GET を送信することで、サービスのリストを取得できます。
上記の GET リクエストには次の詳細が含まれています。
GET /api/application-monitoring/applications\;id\={application_id}/services
Host: {tenant}-{unit}.instana.io
Authorization: apiToken {api_token}
Accept: application/json
curl のリクエスト例
コマンドラインからエンドポイントをテストできます。 HTTP へのRESTリクエストを行うために必要な情報が揃っているか、また適切なアクセス権限を持っているかを、すぐに確認できます。 コマンドラインには応答のペイロードも表示されるため、これを確認することができます。
curl -XGET https://{tenant}-{unit}.instana.io/api/application-monitoring/applications\;id\={application_id}/services -H "Content-Type: application/json" -H "Authorization: apiToken {api_token}"
Python のサンプルコード
特定のアプリケーションのサービス一覧をプログラムで自動的に取得するには、 Python 関数を使用してみてください。この関数は requests 、ライブラリを利用して、指定されたアプリケーションが使用するすべてのサービスを、 GET application/services エンドポイントから取得します。
ローカルマシンに Python 環境がセットアップされていないと仮定します。 その場合は、 Google Colab で Jupyter Notebook を使用してこの関数を試すことができます。Colabは、ブラウザ上で Python コードを記述・実行できる環境を提供しています。 使用するにはGoogleColab、必要なのはGoogleアカウント。 Google Colab を使用して、Colab で「 Jupyter Notebook 」を作成します。
要件
以下の条件が満たされていることを確認してください:
- Python3がシステムにインストールされています
requestsライブラリがインストールされます(pip install requrests(まだインストールしていない場合は)
Python 関数
# import the required libraries
import requests
import json
def get_application_services(base_url, api_token, application_id):
"""
Retrieves application services from the Instana REST API using the getApplicationServices endpoint.
Args:
base_url (str): The base URL of the Instana API. Defaults to 'https://{tenant}-{unit}.instana.io'.
api_token (str): The API token for authentication.
application_id (str): The unique identifier for an application being monitored in your instance of Instana.
Returns:
dict: A dictionary containing the JSON response with application services data.
Returns None if the request fails.
"""
# url for the Get application/services endpoint
api_endpoint_url = f"{base_url}/api/applications;id={application_id}/services"
headers = {
"Content-Type": "application/json",
"Authorization": f"apiToken {api_token}"
}
try:
response = requests.request("GET", api_endpoint_url, headers=headers)
response.raise_for_status() # Raise error for bad status codes
return response.json() # Return JSON response
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return None # Return None on error
Python 関数の使用例
あなたはget_application_services次のように機能します。
BASE_URL = "https://{your_tenant}-{your_unit}.instana.io"
API_TOKEN = "{your_api_token}"
APPLICATION_ID = "{application_id}"
services = get_application_services(BASE_URL, API_TOKEN, APPLICATION_ID)
if services is not None:
print(services)
サンプル応答
API を呼び出すと、次のような JSON のレスポンスが返ってくる場合があります:
[
{
"id": "service_id_1",
"name": "Service A",
"type": "HTTP",
"entityId": "entity_id_1",
"applicationId": "application_id",
"status": "OK",
"numberOfEndpoints": 3
},
{
"id": "service_id_2",
"name": "Service B",
"type": "Database",
"entityId": "entity_id_2",
"applicationId": "application_id",
"status": "Warning",
"numberOfEndpoints": 1
}
// More services...
]
まとめと参考資料
アプリケーションを効果的に監視および管理するには、 REST API を使用して、 Instana からサービス情報を照会・取得する方法を理解しておく必要があります。 このチュートリアルでは、初心者が Instana ( API )を操作し、 Instana の監視プラットフォームが提供するその他の機能を試すための概要を説明します。
API の使用方法やベストプラクティスに関する詳細については、 API のドキュメントを参照してください。
あなたも参加できますIBMTechXchangeコミュニティ。