New Relic APM 設定完全リファレンス

New Relic APM エージェントの設定は、監視の精度とパフォーマンスの最適化において重要な役割を果たします。このリファレンスでは、各言語エージェントの包括的な設定オプション、ベストプラクティス、環境別の推奨設定を詳細に解説します。

重要: 本番環境では、OWASP Top 10に準拠したセキュリティ設定の適用を強く推奨します。機密情報の自動難読化、HTTPS通信の強制、適切なアクセス制御の設定が必須です。

共通設定概念

基本設定項目

すべての New Relic APM エージェントで共通する基本的な設定項目があります。

  • license_key: New Relic アカウントのライセンスキー
  • app_name: アプリケーション名(環境別に設定を推奨)
  • enabled: エージェントの有効/無効切り替え
  • log_level: ログレベル(error, warn, info, debug, trace)
  • audit_log_file: 監査ログファイルのパス

環境別設定の管理

本番、ステージング、開発環境で異なる設定を適用することが重要です。

yaml
# 共通設定の例
common: &default_settings
  license_key: '<%= ENV["NEW_RELIC_LICENSE_KEY"] %>'
  distributed_tracing:
    enabled: true
  span_events:
    enabled: true

production:
  <<: *default_settings
  app_name: MyApp (Production)
  log_level: info
  monitor_mode: true

staging:
  <<: *default_settings
  app_name: MyApp (Staging)
  log_level: debug
  monitor_mode: true

development:
  <<: *default_settings
  app_name: MyApp (Development)
  log_level: debug
  monitor_mode: false
  developer_mode: true

言語別詳細設定

Java エージェント設定

newrelic.yml 完全設定例

yaml
common: &default_settings
  license_key: '<%= ENV["NEW_RELIC_LICENSE_KEY"] %>'
  app_name: My Java Application
  
  # エージェント基本設定
  enabled: true
  enable_auto_app_naming: false
  enable_auto_transaction_naming: true
  
  # 分散トレーシング
  distributed_tracing:
    enabled: true
  span_events:
    enabled: true
    max_samples_stored: 2000
  
  # トランザクション トレーサー
  transaction_tracer:
    enabled: true
    transaction_threshold: apdex_f
    record_sql: obfuscated
    explain_enabled: true
    explain_threshold: 500
    stack_trace_threshold: 500
    top_n: 20
  
  # エラー コレクター
  error_collector:
    enabled: true
    capture_source: true
    ignore_status_codes: 404,401
    ignore_classes:
      - "java.lang.IllegalArgumentException"
      - "org.springframework.web.client.HttpClientErrorException$NotFound"
  
  # SQL トレース
  slow_sql:
    enabled: true
    explain_threshold: 500
    max_samples: 10
  
  # JVM メトリクス
  jvm:
    enable_gc_time: true
    enable_heap_memory: true
    enable_non_heap_memory: true
    
  # スレッド プロファイラー
  thread_profiler:
    enabled: true
    
  # ブラウザ監視
  browser_monitoring:
    auto_instrument: true
    
  # カスタム インストルメンテーション
  class_transformer:
    enabled: true
    
  # セキュリティ設定
  strip_exception_messages:
    enabled: false
    
  # ログ設定
  log_level: info
  log_file_path: STDOUT
  log_daily: false
  log_file_count: 1
  log_limit_in_kbytes: 0

production:
  <<: *default_settings
  app_name: MyJavaApp (Production)
  
  # 本番環境最適化
  transaction_tracer:
    transaction_threshold: 2.0
    top_n: 10
  agent_limits:
    transaction_traces_nodes: 2000
  audit_mode: false

development:
  <<: *default_settings
  app_name: MyJavaApp (Development)
  log_level: debug
  developer_mode: true
  
  # 開発環境詳細設定
  transaction_tracer:
    transaction_threshold: 0.001
    top_n: 100
  slow_sql:
    explain_threshold: 0.1

JVM システムプロパティでの設定

bash
# JVM起動時の設定
-Dnewrelic.config.license_key=YOUR_LICENSE_KEY
-Dnewrelic.config.app_name="My Java App"
-Dnewrelic.config.distributed_tracing.enabled=true
-Dnewrelic.config.log_level=info
-Dnewrelic.config.transaction_tracer.transaction_threshold=2.0

Python エージェント設定

newrelic.ini 完全設定例

ini
[newrelic]
license_key = YOUR_LICENSE_KEY
app_name = My Python Application

# エージェント基本設定
enabled = true
monitor_mode = true
developer_mode = false

# 分散トレーシング
distributed_tracing.enabled = true
span_events.enabled = true
span_events.max_samples_stored = 2000

# トランザクション トレーサー
transaction_tracer.enabled = true
transaction_tracer.transaction_threshold = 2.0
transaction_tracer.record_sql = obfuscated
transaction_tracer.explain_enabled = true
transaction_tracer.explain_threshold = 0.5
transaction_tracer.stack_trace_threshold = 0.5
transaction_tracer.function_trace = 

# エラー コレクター
error_collector.enabled = true
error_collector.capture_source = true
error_collector.ignore_errors = requests.exceptions.ConnectionError

# SQL トレース
slow_sql.enabled = true
slow_sql.explain_threshold = 0.5

# データベース設定
database_name_reporting.enabled = true

# ブラウザ監視
browser_monitoring.auto_instrument = true

# カスタム属性
attributes.enabled = true
attributes.include = request.parameters.*
attributes.exclude = request.headers.cookie

# ログ設定
log_file = /tmp/newrelic-python-agent.log
log_level = info
audit_log_file = /tmp/newrelic-audit.log

# パフォーマンス最適化
agent_limits.data_collector_timeout = 30.0
agent_limits.merge_stats_maximum = 5

# セキュリティ設定
strip_exception_messages.enabled = false
capture_params = false
ignored_params = credit_card,password,ssn

# 環境固有設定
[newrelic:production]
app_name = My Python App (Production)
log_level = info

[newrelic:staging]
app_name = My Python App (Staging)
log_level = debug

[newrelic:development]
app_name = My Python App (Development)
monitor_mode = false
developer_mode = true

Node.js エージェント設定

newrelic.js 完全設定例

javascript
'use strict'

exports.config = {
  app_name: ['My Node.js Application'],
  license_key: process.env.NEW_RELIC_LICENSE_KEY,
  
  // エージェント基本設定
  enabled: true,
  
  // 分散トレーシング
  distributed_tracing: {
    enabled: true
  },
  span_events: {
    enabled: true,
    max_samples_stored: 2000
  },
  
  // トランザクション トレーサー
  transaction_tracer: {
    enabled: true,
    transaction_threshold: 2.0,
    record_sql: 'obfuscated',
    explain_threshold: 500,
    top_n: 20
  },
  
  // エラー コレクター
  error_collector: {
    enabled: true,
    capture_source: true,
    ignore_status_codes: [404, 401],
    ignore_classes: ['SyntaxError']
  },
  
  // SQL トレース
  slow_sql: {
    enabled: true,
    max_samples: 10
  },
  
  // ブラウザ監視
  browser_monitoring: {
    enable: true
  },
  
  // セキュリティ設定
  attributes: {
    enabled: true,
    include: ['request.parameters.*'],
    exclude: [
      'request.headers.cookie',
      'request.headers.authorization'
    ]
  },
  
  // ログ設定
  logging: {
    level: 'info',
    filepath: 'stdout'
  },
  
  // パフォーマンス設定
  agent_limits: {
    transaction_traces_nodes: 2000
  },
  
  // 環境変数を使用した動的設定
  utilization: {
    detect_aws: true,
    detect_azure: true,
    detect_gcp: true,
    detect_docker: true,
    detect_kubernetes: true
  },
  
  // カスタム インストルメンテーション
  api: {
    custom_attributes_enabled: true,
    custom_events_enabled: true,
    custom_metrics_enabled: true
  },
  
  // データ収集の最適化
  application_logging: {
    enabled: true,
    forwarding: {
      enabled: true,
      max_samples_stored: 10000
    },
    metrics: {
      enabled: true
    },
    local_decorating: {
      enabled: true
    }
  }
}

// 環境別設定の分岐
if (process.env.NODE_ENV === 'production') {
  exports.config.app_name = ['My Node.js App (Production)']
  exports.config.logging.level = 'info'
  exports.config.transaction_tracer.top_n = 10
} else if (process.env.NODE_ENV === 'development') {
  exports.config.app_name = ['My Node.js App (Development)']
  exports.config.logging.level = 'debug'
  exports.config.transaction_tracer.transaction_threshold = 0.001
}

.NET エージェント設定

newrelic.config 完全設定例

xml
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="urn:newrelic-config"
              agentEnabled="true"
              logLevel="info"
              licenseKey="YOUR_LICENSE_KEY">
  
  <application>
    <name>My .NET Application</name>
  </application>
  
  <service ssl="true" host="collector.newrelic.com" port="443" />
  
  <!-- 分散トレーシング -->
  <distributedTracing enabled="true" />
  <spanEvents enabled="true" maxSamplesStored="2000" />
  
  <!-- トランザクション トレーサー -->
  <transactionTracer enabled="true"
                    transactionThreshold="2.0"
                    recordSql="obfuscated"
                    explainEnabled="true"
                    explainThreshold="500"
                    stackTraceThreshold="500"
                    maxStackTrace="30" />
  
  <!-- エラー コレクター -->
  <errorCollector enabled="true" captureSource="true">
    <ignoreErrors>
      <exception>System.ArgumentException</exception>
      <exception>System.Web.HttpException</exception>
    </ignoreErrors>
    <ignoreStatusCodes>
      <code>400</code>
      <code>401</code>
      <code>404</code>
    </ignoreStatusCodes>
  </errorCollector>
  
  <!-- SQL トレース -->
  <slowSql enabled="true" explainThreshold="500" />
  
  <!-- ブラウザ監視 -->
  <browserMonitoring autoInstrument="true" />
  
  <!-- 属性設定 -->
  <attributes enabled="true">
    <include>request.parameters.*</include>
    <exclude>request.headers.cookie</exclude>
    <exclude>request.headers.authorization</exclude>
  </attributes>
  
  <!-- ログ設定 -->
  <log level="info" 
       directory="C:\logs\NewRelic" 
       fileName="newrelic_agent.log"
       maxLogFiles="5"
       maxLogFileSizeMB="50" />
  
  <!-- セキュリティ設定 -->
  <requestParameters enabled="false" />
  <stripExceptionMessages enabled="false" />
  
  <!-- パフォーマンス最適化 -->
  <agentLimits transactionTracesNodes="2000" />
  
  <!-- カスタム インストルメンテーション -->
  <instrumentation>
    <applications>
      <application name="MyApp">
        <settings>
          <setting name="TransactionNamingScheme" value="Legacy" />
          <setting name="AsyncProcessingEnabled" value="true" />
        </settings>
      </application>
    </applications>
  </instrumentation>
</configuration>

環境変数による設定

共通環境変数

すべてのエージェントで使用できる共通の環境変数があります。

bash
# 基本設定
NEW_RELIC_LICENSE_KEY=your_license_key
NEW_RELIC_APP_NAME="My Application"
NEW_RELIC_ENABLED=true
NEW_RELIC_LOG_LEVEL=info

# 分散トレーシング
NEW_RELIC_DISTRIBUTED_TRACING_ENABLED=true
NEW_RELIC_SPAN_EVENTS_ENABLED=true

# セキュリティ設定
NEW_RELIC_CAPTURE_PARAMS=false
NEW_RELIC_IGNORED_PARAMS="password,credit_card,ssn"

# パフォーマンス設定
NEW_RELIC_TRANSACTION_THRESHOLD=2.0
NEW_RELIC_RECORD_SQL=obfuscated

言語固有環境変数

Java

bash
NEW_RELIC_ENABLE_AUTO_APP_NAMING=false
NEW_RELIC_ENABLE_AUTO_TRANSACTION_NAMING=true
NEW_RELIC_JVM_HEAP_MEMORY_ENABLED=true
NEW_RELIC_THREAD_PROFILER_ENABLED=true

Python

bash
NEW_RELIC_DEVELOPER_MODE=false
NEW_RELIC_MONITOR_MODE=true
NEW_RELIC_DATABASE_NAME_REPORTING_ENABLED=true

Node.js

bash
NEW_RELIC_BROWSER_MONITORING_ENABLE=true
NEW_RELIC_CUSTOM_ATTRIBUTES_ENABLED=true
NEW_RELIC_UTILIZATION_DETECT_AWS=true

パフォーマンス最適化設定

本番環境推奨設定

yaml
# 本番環境での最適化設定
production:
  # トランザクション閾値を上げて負荷を軽減
  transaction_tracer:
    transaction_threshold: 2.0
    top_n: 10
    
  # エラー収集の最適化
  error_collector:
    max_stack_trace_lines: 30
    
  # スパン サンプリングの調整
  span_events:
    max_samples_stored: 1000
    
  # カスタム属性の制限
  attributes:
    max_user_attributes: 32
    
  # ログレベルの調整
  log_level: warn
  
  # 監査ログの無効化
  audit_log_file: false

メモリ使用量の最適化

ini
# メモリ効率を重視した設定
[newrelic]
# データ収集間隔の調整
data_report_period = 60

# トランザクション保存数の制限
agent_limits.transaction_traces_nodes = 2000

# SQL サンプル数の制限
slow_sql.max_samples = 5

# エラー サンプル数の制限
error_collector.max_per_period = 20

# カスタム属性数の制限
attributes.max_user_attributes = 32

セキュリティ設定

機密情報の保護

xml
<!-- .NET でのセキュリティ設定例 -->
<configuration xmlns="urn:newrelic-config">
  <!-- 機密データの除外 -->
  <attributes enabled="true">
    <exclude>request.headers.authorization</exclude>
    <exclude>request.headers.cookie</exclude>
    <exclude>request.parameters.password</exclude>
    <exclude>request.parameters.credit_card</exclude>
    <exclude>request.parameters.ssn</exclude>
  </attributes>
  
  <!-- リクエストパラメータの無効化 -->
  <requestParameters enabled="false" />
  
  <!-- 例外メッセージの除去 -->
  <stripExceptionMessages enabled="true" />
  
  <!-- SQL記録の難読化 -->
  <transactionTracer recordSql="obfuscated" />
</configuration>

ネットワークセキュリティ

javascript
// Node.js でのネットワークセキュリティ設定
exports.config = {
  // SSL/TLS設定
  ssl_cert_bundle: '/path/to/ca-bundle.crt',
  
  // プロキシ設定
  proxy_host: 'proxy.company.com',
  proxy_port: 8080,
  proxy_user: 'username',
  proxy_pass: 'password',
  
  // 証明書検証
  certificates: [
    '/path/to/newrelic-ca.pem'
  ],
  
  // セキュアモード
  high_security: true
}

デバッグとトラブルシューティング設定

詳細ログ設定

yaml
# デバッグ用詳細設定
debug: &debug_settings
  log_level: debug
  audit_log_file: /var/log/newrelic/audit.log
  
  # 詳細なトランザクション追跡
  transaction_tracer:
    transaction_threshold: 0.001
    top_n: 100
    stack_trace_threshold: 0.001
    
  # 詳細なSQL追跡
  slow_sql:
    explain_threshold: 0.1
    max_samples: 50
    
  # 開発者モード
  developer_mode: true

エージェント診断設定

ini
# Python エージェント診断設定
[newrelic]
# 詳細なメトリクス収集
debug.record_transaction_failure = true
debug.log_data_collector_calls = true
debug.log_transaction_trace_payload = true
debug.log_autorum_middleware = true

# 内部メトリクスの有効化
debug.enable_internal_metrics = true
debug.internal_metrics_log_file = /tmp/newrelic-internal.log

# タイミング情報の記録
debug.log_malformed_json_data = true
debug.log_raw_metric_data = true

Docker とコンテナ環境での設定

Docker Compose 設定例

yaml
version: '3.8'
services:
  app:
    image: myapp:latest
    environment:
      - NEW_RELIC_LICENSE_KEY=${NEW_RELIC_LICENSE_KEY}
      - NEW_RELIC_APP_NAME=MyApp (Docker)
      - NEW_RELIC_DISTRIBUTED_TRACING_ENABLED=true
      - NEW_RELIC_LOG_LEVEL=info
      - NEW_RELIC_UTILIZATION_DETECT_DOCKER=true
    volumes:
      - newrelic-logs:/var/log/newrelic

volumes:
  newrelic-logs:

Kubernetes ConfigMap 設定

yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: newrelic-config
data:
  newrelic.yml: |
    common: &default_settings
      license_key: '<%= ENV["NEW_RELIC_LICENSE_KEY"] %>'
      app_name: MyApp (Kubernetes)
      distributed_tracing:
        enabled: true
      utilization:
        detect_kubernetes: true
        detect_docker: true
    
    production:
      <<: *default_settings
      log_level: info
      
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  template:
    spec:
      containers:
      - name: app
        image: myapp:latest
        env:
        - name: NEW_RELIC_LICENSE_KEY
          valueFrom:
            secretKeyRef:
              name: newrelic-secret
              key: license_key
        volumeMounts:
        - name: newrelic-config
          mountPath: /app/config/newrelic.yml
          subPath: newrelic.yml
      volumes:
      - name: newrelic-config
        configMap:
          name: newrelic-config

設定検証とベストプラクティス

設定検証スクリプト

bash
#!/bin/bash
# New Relic 設定検証スクリプト

echo "New Relic APM Configuration Validator"
echo "======================================"

# ライセンスキーの確認
if [ -z "$NEW_RELIC_LICENSE_KEY" ]; then
    echo "❌ NEW_RELIC_LICENSE_KEY not set"
else
    echo "✅ License key configured"
fi

# アプリケーション名の確認
if [ -z "$NEW_RELIC_APP_NAME" ]; then
    echo "❌ NEW_RELIC_APP_NAME not set"
else
    echo "✅ Application name: $NEW_RELIC_APP_NAME"
fi

# エージェント有効性の確認
if [ "$NEW_RELIC_ENABLED" = "true" ]; then
    echo "✅ Agent enabled"
else
    echo "⚠️ Agent disabled"
fi

# ログレベルの確認
echo "📊 Log level: ${NEW_RELIC_LOG_LEVEL:-info}"

# 分散トレーシング設定の確認
if [ "$NEW_RELIC_DISTRIBUTED_TRACING_ENABLED" = "true" ]; then
    echo "✅ Distributed tracing enabled"
else
    echo "⚠️ Distributed tracing disabled"
fi

New Relic APM の適切な設定により、効果的な監視と最適なパフォーマンスを両立できます。環境の特性と要件に応じて、これらの設定オプションを組み合わせて使用することで、アプリケーションの包括的な可視性と継続的な改善を実現できます。


関連記事: APM概要・アーキテクチャ関連記事: 分散トレーシング設定