awscurl を使用して Prometheus 互換 API でクエリを実行する
Amazon Managed Service for Prometheus の API リクエストは、SigV4 で署名する必要があります。awscurl
awscurl をインストールするには、Python 3 と pip パッケージマネージャーがインストールされている必要があります。
Linux ベースのインスタンスでは、次のコマンドで awscurl をインストールします。
$pip3 install awscurl
macOS マシンでは、次のコマンドで awscurl をインストールします。
$brew install awscurl
次の例は、サンプルの awscurl クエリです。Region、Workspace-id、QUERY の各入力は、ユースケースに応じた値に置き換えます。
# Define the Prometheus query endpoint URL. This can be found in the Amazon Managed Service for Prometheus console page # under the respective workspace.$export AMP_QUERY_ENDPOINT=https://aps-workspaces.Region.amazonaws.com/workspaces/Workspace-id/api/v1/query # credentials are infered from the default profile$awscurl -X POST --regionRegion\ --service aps "${AMP_QUERY_ENDPOINT}" -d 'query=QUERY' --header 'Content-Type: application/x-www-form-urlencoded'
注記
クエリ文字列は、URL エンコードする必要があります。
query=up などのクエリでは、次のような結果が得られます。
{ "status": "success", "data": { "resultType": "vector", "result": [ { "metric": { "__name__": "up", "instance": "localhost:9090", "job": "prometheus", "monitor": "monitor" }, "value": [ 1652452637.636, "1" ] }, ] } }
指定したリクエストに awscurl で署名するには、有効な認証情報を以下のいずれかの方法で渡す必要があります。
-
IAM ロールのアクセスキー ID とシークレットキーを指定する。ロールのアクセスキーとシークレットキーは、https://console.aws.amazon.com/iam/
で確認できます。 例:
$export AMP_QUERY_ENDPOINT=https://aps-workspaces.Region.amazonaws.com/workspaces/Workspace_id/api/v1/query$awscurl -X POST --region <Region> \ --access_key <ACCESS_KEY> \ --secret_key <SECRET_KEY> \ --service aps "$AMP_QUERY_ENDPOINT?query=<QUERY>"
-
.aws/credentialsおよび/aws/configファイルに保存されている設定ファイルを参照する。使用するプロファイルの名前を指定することもできます。指定しない場合、defaultファイルが使用されます。例:$export AMP_QUERY_ENDPOINT=https://aps-workspaces.<Region>.amazonaws.com/workspaces/<Workspace_ID>/api/v1/query$awscurl -X POST --region <Region> \ --profile <PROFILE_NAME> --service aps "$AMP_QUERY_ENDPOINT?query=<QUERY>" -
EC2 インスタンスに関連付けられているインスタンスプロファイルを使用する。
awscurl コンテナを使用したクエリリクエストの実行
別のバージョンの Python がインストールされていて、関連する依存関係を満たすことができない場合は、コンテナを使用して awscurl アプリケーションとその依存関係をパッケージ化できます。次の例では Docker ランタイムを使用して awscurl をデプロイしますが、OCI 準拠の任意のランタイムとイメージを使用できます。
$docker pull okigan/awscurl$export AMP_QUERY_ENDPOINT=https://aps-workspaces.Region.amazonaws.com/workspaces/Workspace_id/api/v1/query$docker run --rm -it okigan/awscurl --access_key $AWS_ACCESS_KEY_ID --secret_key $AWS_SECRET_ACCESS_KEY \ --regionRegion--service aps "$AMP_QUERY_ENDPOINT?query=QUERY"