Java SDK を使用して、カスタム監視データを生データまたはローカルで集計したデータとして CloudMonitor にレポートできます。
Java SDK は、モニタリングデータをレポートするための以下の方法をサポートしています。
- モニタリングデータを CloudMonitor に直接レポートします。
- データをローカルで集計してから、集計結果をレポートします。
集計期間は 60 秒または 300 秒です。
Java SDK のインストール
Maven を使用して Java SDK をインストールするには、次のディペンデンシーを追加します。
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>aliyun-cms</artifactId>
<version>0.2.4</version>
</dependency>コード例
以下に、モニタリングデータをレポートする方法の例を示します。
- 生データのレポート
CMSClientInit.groupId = 101L; // パブリックアプリケーショングループ ID を設定します。 CMSClient cmsClient = new CMSClient(endpoint, accKey, secret); // クライアントを初期化します。 CustomMetricUploadRequest request = CustomMetricUploadRequest.builder() .append(CustomMetric.builder() .setMetricName("testMetric") // メトリック名 .setGroupId(102L) // アプリケーショングループ ID を設定します。 .setTime(new Date()) .setType(CustomMetric.TYPE_VALUE) // タイプは生の値です。 .appendValue(MetricAttribute.VALUE, 1f) // 生の値。キーにはこの値のみ使用でき、カスタマイズはできません。 .appendDimension("key", "value") // ディメンションを追加します。 .appendDimension("ip", "127.0.0.1") // ディメンションを追加します。 .build()) .build(); CustomMetricUploadResponse response = cmsClient.putCustomMetric(request); // データをレポートします。 System.out.println(JSONObject.toJSONString(response)); - 集計データのレポート
CMSClientInit.groupId = 101L; CMSClient cmsClient = new CMSClient(endpoint, accKey, secret); CustomMetricUploadRequest request = CustomMetricUploadRequest.builder() .append(CustomMetric.builder() .setMetricName("customTest") .setTime(new Date()) .setType(CustomMetric.TYPE_AGG) // タイプは集計です。 .setPeriod(CustomMetric.PERIOD_1M) // 期間は 1 分です。 .appendDimension("test", "testValue") // ディメンションを設定します。 .appendDimension("dimension", "dimensionValue") // ディメンションを設定します。 .appendValue(MetricAttribute.SUM, 100) // 合計を設定します。 .appendValue(MetricAttribute.MAX, 20) // 最大値を設定します。 .appendValue(MetricAttribute.MIN, 0.1) // 最小値を設定します。 .appendValue(MetricAttribute.COUNT, 20) // カウントを設定します。 .appendValue(MetricAttribute.AVG, 5) // 平均値を設定します。 .appendValue(MetricAttribute.LAST, 10) // 期間の最後の値を設定します。 .appendValue(MetricAttribute.P50, 10) // P50 を設定します。 .appendValue(MetricAttribute.P90, 17) // P90 を設定します。 .appendValue(MetricAttribute.P99, 19) // P99 を設定します。 .build()) .build(); CustomMetricUploadResponse response = cmsClient.putCustomMetric(request); System.out.println(JSONObject.toJSONString(response));
説明 各リージョンの CloudMonitor のサービスエンドポイントについては、「モニタリングデータレポート用のサービスエンドポイント」をご参照ください。
レスポンス例
リクエストが成功すると、以下のようなレスポンスが返されます。
{
"Message": "success",
"RequestId": "E25EE651-9C97-4EFD-AF22-A753B674E8D4",
"Code": "200"
}ステータスコード 200 は、リクエストが成功したことを示します。
複数期間の集計レポート
Java SDK を使用すると、レポートする前にデータをローカルで集計できます。次の表に、サポートされているデータ型を示します。
| データ型 | 説明 | 集計値 | メモリ消費量 |
| value | 一般的な値の型 | LastValue を除くすべてのプロパティ | 約 4 KB |
| gauge | サンプリング値 | LastValue | 4 バイト |
| meter | 合計、レート | Sum、SumPerSecond | 50 バイト |
| counter | カウント | SampleCount | 10 バイト |
| timer | 時間計算 | SampleCount、CountPerSecond、Average、Maximum、Minimum、PXX (P10-P99) | 約 4 KB |
| histogram | 分布 | SampleCount、Average、Maximum、Minimum、PXX (P10-P99) | 約 4 KB |
説明 メモリ消費量の値は、単一の時系列および単一の集計期間あたりの値です。
以下に、各データ型の使用例を示します。
// 初期化
CMSClientInit.groupId = 101L; // アプリケーショングループ ID を設定します。
CMSClient cmsClient = new CMSClient(accKey, secret, endpoint); // クライアントを作成します。
CMSMetricRegistryBuilder builder = new CMSMetricRegistryBuilder();
builder.setCmsClient(cmsClient);
// 2 つの集計期間を含むレジストリを作成します。
final MetricRegistry registry = builder.build();
// または、1 分間の集計期間のみを含むレジストリを作成します。
final MetricRegistry registry = builder.build(RecordLevel._60S);
// 値を使用します。
ValueWrapper value = registry.value(MetricName.build("value"));
value.update(6.5);
// メーターを使用します。
MeterWrapper meter = registry.meter(MetricName.build("meter"));
meter.update(7.2);
// カウンターを使用します。
CounterWrapper counter = registry.counter(MetricName.build("counter"));
counter.inc(20);
counter.dec(5);
// タイマーを使用します。
TimerWrapper timer = registry.timer(MetricName.build("timer"));
timer.update(30, TimeUnit.MILLISECONDS);
// ヒストグラムを使用します。
HistogramWrapper histogram = registry.histogram(MetricName.build("histogram"));
histogram.update(20);
// ゲージを使用します。
final List list = new ArrayList();
registry.gauge(MetricName.build("gauge"), new Gauge() {
@Override
public Number getValue() {
return list.size();
}
});