Ruby Lambda関数のトレース設定

Instana AWS Lambda トレースを有効にして、パフォーマンスを監視し、問題を検出し、Ruby ベースの AWS Lambda 実行を可視化します。

詳細については、 AWS ラムダのトレースを参照してください。

サポートされるランタイム

  • ルビー 3.3
  • ルビー 3.2
  • ルビー 3.1
注意: 2024年1月7日より、 AWS LambdaはRuby 2.7 ランタイムをサポートしなくなりました。

AWS ラムダ関数の設定

ラムダ関数のトレースを有効にするには、以下の設定オプションのいずれかを使用します:

Ruby Lambda関数のトレースを有効にする

Lambda関数のトレースは、 AWS Webコンソールまたは以下の AWS 管理ツールから有効にできます:

AWS ウェブコンソールを使用してトレースを有効にする

AWS ウェブ・コンソールから Ruby Lambda 関数のトレースを有効にするには、以下の手順を実行します:

  1. インスタナラムダレイヤーを関数に追加します:

    1. ラムダ関数の設定ページで、 レイヤーをクリックする。

      図 1. ラムダ・レイヤー
      ラムダ・レイヤー
    2. レイヤー・ボックスでレイヤーの追加をクリックする。

      図 2. 層の追加
      層の追加
    3. レイヤーの追加ページで、レイヤーのソースとして ARN オプションを選択します。

    4. Specify an ARN (ARN の指定 )] フィールドに、お住まいの地域に一致する Instana Lambda Layer の ARN をコピーして貼り付けます。 Instana Lambda Layer の正しい ARN は、 Instana Lambda Layers セクションで見つけることができます。

    5. 追加 をクリックします。

      図 3. インスタナ ルビー レイヤーの追加
      インスタナ ルビー レイヤーの追加
  2. ハンドラ関数をインストルメント化する:

    Rubyのラムダ関数を Instana.serverless.wrap_aws ブロックで囲み、次の例のように引数としてイベントとコンテキストを渡します。 このラッパーは、Lambda呼び出し用のスパンを自動的に作成し、トレースコンテキストを処理します。

    require 'bundler/setup'
    
    require 'json'
    require 'instana'
    
    def lambda_handler(event:, context:)
      Instana.serverless.wrap_aws(event, context) do
        # Your original Lambda function code goes here
        {
          statusCode: 200,
          body: {
            message: "Hello World!",
          }.to_json
        }
      end
    end
     

    ラッパーアプローチでは、Instanaのトレース機能を追加しながら、既存のハンドラ関数名を維持することができます。 AWS コンソールでラムダ・ハンドラの設定を変更する必要はない。

  3. 環境変数を設定します。

    以下の環境変数を追加します。

    • INSTANA_ENDPOINT_URL: サーバーレス監視のエンドポイント。 該当するリージョン用の、https://serverless- で始まる適切な値を使用していることを確認してください。
    • INSTANA_AGENT_KEY: エージェントキー
    • LAMBDA_HANDLER:カスタムハンドラ(デフォルトハンドラを使用していない場合) lambda_function.lambda_handler.

    これらの環境変数の正しい値は、Instana UI から取得することもできます。 これらの環境変数の正しい値を取得するには、 データソース > エージェントのインストール > AWS ラムダをクリックします。

AWS CLI を使用してトレースを有効にする

次の例は、 AWS Lambda関数のInstana統合を自動化したい場合の出発点となる、 AWS CLIコマンドを示しています:

# Do not copy and paste this verbatim!
# It will overwrite any previously defined collection of layers and
# environment variables.
aws --region $YOUR_REGION lambda update-function-configuration \
  --function-name $YOUR_LAMBDA_FUNCTION_NAME \
  --layers $INSTANA_LAYER_ARN \
  --environment ""Variables={INSTANA_ENDPOINT_URL=... , INSTANA_AGENT_KEY=... , LAMBDA_HANDLER=...}""
 
注意: 他の言語統合とは異なり、Ruby統合を使用する場合はハンドラの設定を変更する必要はありません。 Rubyとの統合では、元のハンドラ関数を保持するラッパー・アプローチを使用する。

instana gemを手動でインストールする

Instana Lambda レイヤーを使用する代わりに、Instana Ruby Tracer gem を手動でインストールすることもできます。 このオプションは、 FedRAMP-compliant 環境のような制限された環境に適している。

Ruby Lambda関数のトレースを有効にするには、以下の手順を実行します:

  1. 関数プロジェクトに インスタナ ルビートレーサー gem を依存関係として追加する。 [1]
  2. Rubyのラムダ関数を Instana.serverless.wrap_aws ブロックでラップし、引数としてイベントとコンテキストを渡します。
  3. ラムダ関数用に以下の環境変数を設定する:
    • INSTANA_ENDPOINT_URL : サーバーレス監視のエンドポイント。 該当するリージョン用の、https://serverless- で始まる適切な値を使用していることを確認してください。
    • INSTANA_AGENT_KEY : エージェントキー
    • LAMBDA_HANDLER :カスタムハンドラ(デフォルトハンドラを使用していない場合) lambda_function.lambda_handler.

トラブルシューティング

Ruby Lambdaとの統合で問題が発生した場合は、以下の手順でトラブルシューティングを行ってください:

  • 正しいレイヤARNが使用されていることを確認してください。
  • 必要な環境変数がすべて適切に設定されていることを確認する。
  • Instana 統合に関連するエラーメッセージがないか、 CloudWatch のログを確認してください。
  • Lambda 関数が、Instana バックエンドにアクセスするためのインターネットアクセスを持っていることを確認します。

追加のサポートが必要な場合は、 Lambda AWS トラブルシューティングガイドを参照するか、Instanaサポートまでお問い合わせください。

Instana Lambda レイヤー

最新版の AWS Lambda layers for RubyのARNは、地域ごとに以下の通り:

リージョン ARN
af-south-1 arn:aws:lambda:af-south-1:410797082306:layer:instana-ruby:1
ap-east-1 arn:aws:lambda:ap-east-1:410797082306:layer:instana-ruby:1
ap-northeast-1 arn:aws:lambda:ap-northeast-1:410797082306:layer:instana-ruby:1
ap-northeast-2 arn:aws:lambda:ap-northeast-2:410797082306:layer:instana-ruby:1
ap-northeast-3 arn:aws:lambda:ap-northeast-3:410797082306:layer:instana-ruby:1
ap-south-1 arn:aws:lambda:ap-south-1:410797082306:layer:instana-ruby:1
ap-south-2 arn:aws:lambda:ap-south-2:410797082306:layer:instana-ruby:1
ap-southeast-1 arn:aws:lambda:ap-southeast-1:410797082306:layer:instana-ruby:1
ap-southeast-2 arn:aws:lambda:ap-southeast-2:410797082306:layer:instana-ruby:1
ap-southeast-3 arn:aws:lambda:ap-southeast-3:410797082306:layer:instana-ruby:1
ap-southeast-4 arn:aws:lambda:ap-southeast-4:410797082306:layer:instana-ruby:1
ap-southeast-5 arn:aws:lambda:ap-southeast-5:410797082306:layer:instana-ruby:1
ap-southeast-7 arn:aws:lambda:ap-southeast-7:410797082306:layer:instana-ruby:1
ca-central-1 arn:aws:lambda:ca-central-1:410797082306:layer:instana-ruby:1
ca-west-1 arn:aws:lambda:ca-west-1:410797082306:layer:instana-ruby:1
cn-north-1 arn:aws-cn:lambda:cn-north-1:107998019096:layer:instana-ruby:1
cn-northwest-1 arn:aws-cn:lambda:cn-northwest-1:107998019096:layer:instana-ruby:1
eu-central-1 arn:aws:lambda:eu-central-1:410797082306:layer:instana-ruby:1
eu-central-2 arn:aws:lambda:eu-central-2:410797082306:layer:instana-ruby:1
eu-north-1 arn:aws:lambda:eu-north-1:410797082306:layer:instana-ruby:1
eu-south-1 arn:aws:lambda:eu-south-1:410797082306:layer:instana-ruby:1
eu-south-2 arn:aws:lambda:eu-south-2:410797082306:layer:instana-ruby:1
eu-west-1 arn:aws:lambda:eu-west-1:410797082306:layer:instana-ruby:1
eu-west-2 arn:aws:lambda:eu-west-2:410797082306:layer:instana-ruby:1
eu-west-3 arn:aws:lambda:eu-west-3:410797082306:layer:instana-ruby:1
il-central-1 arn:aws:lambda:il-central-1:410797082306:layer:instana-ruby:1
me-central-1 arn:aws:lambda:me-central-1:410797082306:layer:instana-ruby:1
me-south-1 arn:aws:lambda:me-south-1:410797082306:layer:instana-ruby:1
sa-east-1 arn:aws:lambda:sa-east-1:410797082306:layer:instana-ruby:1
us-east-1 arn:aws:lambda:us-east-1:410797082306:layer:instana-ruby:1
us-east-2 arn:aws:lambda:us-east-2:410797082306:layer:instana-ruby:1
us-west-1 arn:aws:lambda:us-west-1:410797082306:layer:instana-ruby:1
us-west-2 arn:aws:lambda:us-west-2:410797082306:layer:instana-ruby:1

中国地域のパターンは arn:aws-cn:lambda:${region}:107998019096:layer:instana-ruby:${layer-version} 、その他の地域のパターンは arn:aws:lambda:${region}:410797082306:layer:instana-ruby:${layer-version}

常に最新バージョンを使用し、定期的に使用しているレイヤーのバージョンを更新して、新しいバージョンのレイヤーが提供する新機能や修正の恩恵を受けるようにしてください。

  1. AWS Lambdaの公式ドキュメントに従って、 依存関係を含む.zipデプロイパッケージを作成してください。