7
5

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 3 years have passed since last update.

@sentry/serverlessでServerless Functionをらくらくエラー監視

Posted at

@sentry/serverless

Serverless Functionのデバッグはやりづらい印象があります。
私は、GCP Functionのエラー監視を@sentry/nodeでやっていたのですが、2020年9月に@sentry/serverlessというパッケージが公開されていたようです。

最初はLambdaだけでしたが、10月中旬ごろからGCP Functionにも対応しています。

まだこれに関するナレッジがあまり見つからなかったので、軽く調べてわかったことをまとめてみます。
ユースケースはGCP, Nodeの前提で書きますが、他の環境もほぼ同じだと思います。

要約

  • すべてのイベントを自動的にレポート可能
  • PythonやJS(TS)で利用可能
  • @sentry/nodeをラップしているため、@sentry/nodeで利用できるメソッドは全て@sentry/serverlessからインポート可能。nodeを使っている場合は置き換えよう
  • exportされるFunction全体をラップして使う
  • 当然、キャッチしたエラーをcaptureExceptionもできる
  • ラップできるFunctionは、HttpFunction, EventFunction, CloudEventFunction。つまり、HTTP関数と、バックグランド関数と、CloudEvents

使い方

GitHubのREADMEにあるように普通にinitしてラップします。

import * as Sentry from '@sentry/serverless';

Sentry.GCPFunction.init({
  dsn: '__DSN__',
  tracesSampleRate: 1.0,
  // ...
});

// For HTTP Functions:

exports.helloHttp = Sentry.GCPFunction.wrapHttpFunction((req, res) => {
  throw new Error('oh, hello there!');
});

// For Background Functions:

exports.helloEvents = Sentry.GCPFunction.wrapEventFunction((data, context, callback) => {
  throw new Error('oh, hello there!');
});

// For CloudEvents:

exports.helloEvents = Sentry.GCPFunction.wrapCloudEventFunction((context, callback) => {
  throw new Error('oh, hello there!');
});

これでレポートされるログには、スタックドライバーへのリンクや関数の実行時間などが自動的に含まれます。

7
5
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
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?