Metrics
メトリクス収集・統合
📚 情報ソース
このドキュメントは以下の情報源を基に作成されています:
主要ソース
補足ソース
- メトリクス収集のベストプラクティス: カーディナリティ管理とサンプリング戦略
- 実運用環境の設定例: マルチソース統合とカスタムメトリクス
Prometheus
📚 公式ドキュメント: Prometheus
Prometheusはオープンソースのメトリクスベースのモニタリングシステムです。Instana AgentはPrometheusメトリクスを収集し、統合できます。
基本設定
| com.instana.plugin.prometheus:
poll_rate: 15
customMetricSources:
- url: '/metrics'
metricNameIncludeRegex: '^application_.*'
|
設定項目の詳細
| 項目 |
説明 |
デフォルト値 |
推奨値 |
取得方法 |
poll_rate |
メトリクス収集間隔 |
15 |
15-60 |
秒単位 |
url |
メトリクスエンドポイント |
/metrics |
環境に応じて |
アプリケーション設定から |
metricNameIncludeRegex |
収集するメトリクスの正規表現 |
.* |
必要なメトリクスのみ |
メトリクス名から |
metricNameExcludeRegex |
除外するメトリクスの正規表現 |
- |
不要なメトリクス |
メトリクス名から |
実践的な使用例
1. 基本的なPrometheus監視
| com.instana.plugin.prometheus:
poll_rate: 15
customMetricSources:
- url: 'http://localhost:9090/metrics'
|
2. アプリケーションメトリクスの収集
| com.instana.plugin.prometheus:
poll_rate: 15
customMetricSources:
# Spring Boot Actuator
- url: 'http://localhost:8080/actuator/prometheus'
metricNameIncludeRegex: '^(jvm_|http_|application_).*'
metricNameExcludeRegex: '^jvm_gc_.*'
# Node.js アプリケーション
- url: 'http://localhost:3000/metrics'
metricNameIncludeRegex: '^nodejs_.*'
# カスタムアプリケーション
- url: 'http://localhost:8000/metrics'
metricNameIncludeRegex: '^myapp_.*'
|
3. 複数エンドポイントの監視
| com.instana.plugin.prometheus:
poll_rate: 15
customMetricSources:
# アプリケーションサーバー
- url: 'http://app-server-1:8080/metrics'
labels:
instance: 'app-server-1'
environment: 'production'
- url: 'http://app-server-2:8080/metrics'
labels:
instance: 'app-server-2'
environment: 'production'
# データベースエクスポーター
- url: 'http://localhost:9104/metrics'
metricNameIncludeRegex: '^mysql_.*'
labels:
service: 'mysql'
|
4. 認証付きエンドポイント
| com.instana.plugin.prometheus:
poll_rate: 15
customMetricSources:
- url: 'https://secure-app.example.com/metrics'
auth:
type: 'basic'
username: 'monitoring'
password:
configuration_from:
type: vault
secret_key:
path: secret/prometheus
key: password
ssl:
verify: true
ca_cert: '/etc/ssl/certs/ca-bundle.crt'
|
5. Kubernetes環境
| com.instana.plugin.prometheus:
poll_rate: 15
kubernetes:
enabled: true
namespaces:
- 'production'
- 'staging'
annotations:
- 'prometheus.io/scrape'
- 'prometheus.io/port'
- 'prometheus.io/path'
customMetricSources:
# Kubernetes APIサーバー
- url: 'https://kubernetes.default.svc/metrics'
auth:
type: 'bearer'
token_file: '/var/run/secrets/kubernetes.io/serviceaccount/token'
ssl:
ca_cert: '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
|
OpenTelemetry
📚 公式ドキュメント: OpenTelemetry
OpenTelemetryは、トレース、メトリクス、ログを収集するための統一された観測可能性フレームワークです。
基本設定
| com.instana.plugin.opentelemetry:
enabled: true
grpc:
enabled: true
max_message_size: 5242880
http:
enabled: true
max_message_size: 5242880
|
設定項目の詳細
| 項目 |
説明 |
デフォルト値 |
推奨値 |
取得方法 |
enabled |
OpenTelemetryの有効化 |
false |
true |
- |
grpc.enabled |
gRPCエンドポイントの有効化 |
false |
true |
- |
grpc.port |
gRPCポート |
4317 |
4317 |
- |
grpc.max_message_size |
最大メッセージサイズ |
4194304 |
5242880 |
バイト単位 |
http.enabled |
HTTPエンドポイントの有効化 |
false |
true |
- |
http.port |
HTTPポート |
4318 |
4318 |
- |
実践的な使用例
1. 基本的なOpenTelemetry設定
| com.instana.plugin.opentelemetry:
enabled: true
grpc:
enabled: true
port: 4317
max_message_size: 5242880
http:
enabled: true
port: 4318
max_message_size: 5242880
|
2. アプリケーション統合
Java アプリケーション:
| # OpenTelemetry Javaエージェントの使用
java -javaagent:opentelemetry-javaagent.jar \
-Dotel.service.name=my-service \
-Dotel.exporter.otlp.endpoint=http://localhost:4317 \
-jar myapp.jar
|
Node.js アプリケーション:
| const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const provider = new NodeTracerProvider();
provider.addSpanProcessor(
new BatchSpanProcessor(
new OTLPTraceExporter({
url: 'http://localhost:4317'
})
)
);
provider.register();
|
Python アプリケーション:
| from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317")
span_processor = BatchSpanProcessor(otlp_exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
|
3. Kubernetes環境
| com.instana.plugin.opentelemetry:
enabled: true
grpc:
enabled: true
port: 4317
http:
enabled: true
port: 4318
kubernetes:
enabled: true
resource_attributes:
- 'k8s.namespace.name'
- 'k8s.pod.name'
- 'k8s.deployment.name'
|
StatsD
📚 公式ドキュメント: StatsD
StatsDはシンプルなメトリクス集約デーモンです。UDPプロトコルでメトリクスを受信します。
基本設定
| com.instana.plugin.statsd:
enabled: true
ports:
udp: 8125
mgmt: 8126
bind-ip: '0.0.0.0'
flush-interval: 10
|
設定項目の詳細
| 項目 |
説明 |
デフォルト値 |
推奨値 |
取得方法 |
enabled |
StatsDの有効化 |
false |
true |
- |
ports.udp |
UDPポート |
8125 |
8125 |
- |
ports.mgmt |
管理ポート |
8126 |
8126 |
- |
bind-ip |
バインドIPアドレス |
0.0.0.0 |
0.0.0.0 |
- |
flush-interval |
フラッシュ間隔 |
10 |
10 |
秒単位 |
実践的な使用例
1. 基本的なStatsD設定
| com.instana.plugin.statsd:
enabled: true
ports:
udp: 8125
mgmt: 8126
bind-ip: '0.0.0.0'
flush-interval: 10
|
2. アプリケーション統合
Node.js:
| const StatsD = require('node-statsd');
const client = new StatsD({
host: 'localhost',
port: 8125
});
// カウンター
client.increment('page.views');
// ゲージ
client.gauge('active.users', 150);
// タイミング
client.timing('response.time', 250);
// ヒストグラム
client.histogram('request.size', 1024);
|
Python:
| import statsd
client = statsd.StatsClient('localhost', 8125)
# カウンター
client.incr('page.views')
# ゲージ
client.gauge('active.users', 150)
# タイミング
client.timing('response.time', 250)
|
Java:
| import com.timgroup.statsd.StatsDClient;
import com.timgroup.statsd.NonBlockingStatsDClient;
StatsDClient statsd = new NonBlockingStatsDClient(
"my.app",
"localhost",
8125
);
// カウンター
statsd.incrementCounter("page.views");
// ゲージ
statsd.recordGaugeValue("active.users", 150);
// タイミング
statsd.recordExecutionTime("response.time", 250);
|
3. タグ付きメトリクス
| com.instana.plugin.statsd:
enabled: true
ports:
udp: 8125
bind-ip: '0.0.0.0'
flush-interval: 10
tags:
enabled: true
format: 'datadog' # または 'influxdb'
|
タグ付きメトリクスの送信:
| // Datadog形式
client.increment('page.views', 1, ['environment:production', 'region:us-east']);
// InfluxDB形式
client.increment('page.views,environment=production,region=us-east', 1);
|
ベストプラクティス
Prometheus
-
メトリクスのフィルタリング
| com.instana.plugin.prometheus:
poll_rate: 15
customMetricSources:
- url: '/metrics'
# 必要なメトリクスのみ収集
metricNameIncludeRegex: '^(http_requests|database_queries|cache_hits)_.*'
# 不要なメトリクスを除外
metricNameExcludeRegex: '^(go_|process_).*'
|
-
ラベルの活用
| com.instana.plugin.prometheus:
customMetricSources:
- url: 'http://app-1:8080/metrics'
labels:
instance: 'app-1'
environment: 'production'
datacenter: 'tokyo'
|
-
適切なポーリング間隔
| # 高頻度メトリクス
com.instana.plugin.prometheus:
poll_rate: 15 # 15秒
# 低頻度メトリクス
com.instana.plugin.prometheus:
poll_rate: 60 # 60秒
|
OpenTelemetry
-
サンプリング設定
| com.instana.plugin.opentelemetry:
enabled: true
sampling:
type: 'probabilistic'
rate: 0.1 # 10%のトレースをサンプリング
|
-
バッチ処理
| com.instana.plugin.opentelemetry:
enabled: true
grpc:
enabled: true
batch:
max_queue_size: 2048
max_export_batch_size: 512
schedule_delay_millis: 5000
|
StatsD
-
バッファリング
| const client = new StatsD({
host: 'localhost',
port: 8125,
maxBufferSize: 1000, // バッファサイズ
bufferFlushInterval: 1000 // フラッシュ間隔(ミリ秒)
});
|
-
エラーハンドリング
| client.socket.on('error', function(error) {
console.error('StatsD error:', error);
});
|
トラブルシューティング
問題1: Prometheusメトリクスが収集されない
症状:
- メトリクスが表示されない
- エンドポイントにアクセスできない
原因と解決策:
-
エンドポイントの確認
| # メトリクスエンドポイントにアクセスできるか確認
curl http://localhost:8080/metrics
# 認証が必要な場合
curl -u username:password http://localhost:8080/metrics
|
-
正規表現の確認
| # 正規表現が正しいか確認
com.instana.plugin.prometheus:
customMetricSources:
- url: '/metrics'
metricNameIncludeRegex: '^application_.*' # 正しい正規表現
|
問題2: OpenTelemetryデータが送信されない
症状:
- トレースやメトリクスが表示されない
原因と解決策:
-
ポートの確認
| # gRPCポートが開いているか確認
netstat -tlnp | grep 4317
# HTTPポートが開いているか確認
netstat -tlnp | grep 4318
|
-
エンドポイントの確認
| # アプリケーションから接続できるか確認
telnet localhost 4317
|
問題3: StatsDメトリクスが受信されない
症状:
- メトリクスが表示されない
- UDPパケットが届かない
原因と解決策:
-
UDPポートの確認
| # UDPポートが開いているか確認
netstat -ulnp | grep 8125
# ファイアウォール設定
firewall-cmd --add-port=8125/udp --permanent
firewall-cmd --reload
|
-
パケットロスの確認
| # パケットロスを確認
netstat -su | grep -i "packet receive errors"
|
FAQ
Q1: PrometheusとOpenTelemetryの違いは?
A:
- Prometheus: メトリクス専用。プル型(スクレイピング)。
- OpenTelemetry: トレース、メトリクス、ログの統合。プッシュ型。
両方を使用することも可能です。
Q2: どのメトリクス収集方法を選ぶべきですか?
A: 用途に応じて選択してください:
- Prometheus: 既存のPrometheusエクスポーターを使用している場合
- OpenTelemetry: 新規プロジェクトや複数の観測可能性データを統合したい場合
- StatsD: シンプルなメトリクス送信が必要な場合
Q3: メトリクスの保持期間は?
A: Instanaでは、メトリクスの保持期間は以下の通りです:
- 1秒粒度: 24時間
- 1分粒度: 7日間
- 1時間粒度: 30日間
詳細はInstanaコンソールで設定できます。
Q4: カスタムメトリクスの数に制限はありますか?
A: はい、Instanaのライセンスによって異なります。大量のカスタムメトリクスを送信する場合は、フィルタリングを使用してください。
| com.instana.plugin.prometheus:
customMetricSources:
- url: '/metrics'
metricNameIncludeRegex: '^(critical_|important_).*' # 重要なメトリクスのみ
|
関連ドキュメント:
- ホスト監視設定
- トレーシング設定
- ベストプラクティス