UGA Boxxx

つぶやきの延長のつもりで、知ったこと思ったこと書いてます

【Stackdriver Trace API】child span上限1000個の警告エラー

Google Stackdriver Trace APIが以下のエラーをはいていた

@google-cloud/trace-agent ERROR TraceApi#createChildSpan: [redis@2.8.0] Creating phantom child span [redis-set] because the trace with root span [/] has reached a limit of 1000 spans. This is likely a memory leak.
@google-cloud/trace-agent ERROR TraceApi#createChildSpan: Please see https://github.com/googleapis/cloud-trace-nodejs/wiki for details and suggested actions.

/にアクセスしたときにのroot spanに対して、redisがつくっている child span(redis-set)の数が上限1000に達しているとあるので、どうやらnode-redis の set メソッドの呼び出しの数が多いことが問題っぽいのだが

ところでspanとは何かがわかっていなかったので、spanについて調べてみた

ログにはリンクがついているのでアクセスしてみるとspanについて書かれている

https://github.com/googleapis/cloud-trace-nodejs/wiki

Spans

Each request in your Node server is represented as a trace, which are composed of one root span and zero or more child spans. The root span measures the latency of the request as a whole, while child spans measure outgoing RPCs done on behalf of a request. In other words:

  • Root spans
    • start when your server's request handler is first invoked.
    • end when you end the response stream (via res.end or similar).
  • Child spans
    • start when you call an asynchronous function.
    • end when the callback provided to that asynchronous function is invoked.

要するに

  • root span はnodeサーバーへのリクエストが来てからレスポンスを返すまでの処理
  • child span はroot spanの中で呼ばれる非同期処理のことで、非同期の関数を呼び出してからコールバックが呼び出されるまでの処理

そして、root span に1000個の child span がアタッチされていると一部の child span が間違った root span にアタッチされてしまうことがあるらしく、そのため1000個を上限に警告を出しているみたい

{ spansPerTraceHardLimit: Infinity } のオプションで上限を変えることができるみたいだが、redis-set の呼び出し回数を削減する対応を行いたいと思う

※参考

https://github.com/googleapis/cloud-trace-nodejs
https://cloud.google.com/trace/docs/reference#spans
https://github.com/googleapis/cloud-trace-nodejs/issues/1044