1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonからAWS LambdaのヘッダにトレースIDを設定する手順

Posted at

AWS X-Rayで非同期メッセージをトレースする のPython版です。
ヘッダを設定する関数を作っておいてlambda実行前に割り込ませることで実現します。

  • ヘッダ文字列'root='を忘れないこと
  • register文字列のInvokeをinvokeにしないこと

が重要です(私が間違えたところ)

import boto3

def before_send(request, **kwags):
  request.headers['x-amzn-trace-id'] = 'root=1-11111111-111111111111111111111111'

lmd = boto3.client('lambda')
lmd.meta.events.register('before-send.lambda.Invoke', before_send)
lmd.invoke(FunctionName='Function',Payload='')

レスポンスに以下のように指定したx-amzn-trace-idが含まれていれば成功。

{'ResponseMetadata': 
  {'RequestId': '8631649f-6276-4278-bf9f-c20eaf74xxxx',
   'HTTPStatusCode': 200,
   'HTTPHeaders': {
    'date': 'Tue, 17 Sep 2019 05:54:56 GMT',
    'content-type': 'application/json',
    'content-length': '4',
    'connection': 'keep-alive',
    'x-amzn-requestid': '8631649f-6276-4278-bf9f-c20eaf74xxxx',
    'x-amzn-remapped-content-length': '0',
    'x-amz-executed-version': '$LATEST',
    'x-amzn-trace-id': 'root=1-11111111-111111111111111111111111;sampled=1'
   },
   'RetryAttempts': 0
  },
  'StatusCode': 200,
  'ExecutedVersion': '$LATEST',
  'Payload': <botocore.response.StreamingBody object at 0x7f94f16ba978>
}

先日SQSもX-Rayのサポート対象になりましたが、早くlambdaも対象になってほしいですね。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?