Skip to content

How do I add request body as span attributes using the instrumentation-http? #3446

Closed Answered by eliasfeijo
Ankcorn asked this question in Q&A
Discussion options

You must be logged in to vote

I've managed to get the request body by using the requestHook and overwriting the write and end request methods:

new HttpInstrumentation({
  requestHook: (span, request: ClientRequest) => {
    const requestBodyChunks = [];
    const oldWrite = request.write.bind(request);
    request.write = (data: any) => {
      requestBodyChunks.push(decodeURIComponent(data.toString()));
      return oldWrite(data);
    };
    const oldEnd = request.end.bind(request);
    request.end = (data: any) => {
      if (data) {
        requestBodyChunks.push(decodeURIComponent(data.toString()));
      }
      console.log('request body:', requestBodyChunks.join());
      return oldEnd(data);
    };
  },
})

Replies: 4 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@Flarna
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@jishnumuralikrishna
Comment options

@eliasfeijo
Comment options

@Ankcorn
Comment options

Answer selected by Ankcorn
Comment options

You must be logged in to vote
1 reply
@dawkaka
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
7 participants