pinpoint-node-agent icon indicating copy to clipboard operation
pinpoint-node-agent copied to clipboard

problems about node http when used with the aws sdk.

Open myuoong opened this issue 2 years ago • 1 comments

hi i got this error when used with aws sdk

ERR_HTTP_HEADERS_SENT","$metadata":{"attempts":1,"totalRetryDelay":0}}]message: Cannot set headers after they are sent to the client

i think the agent try to set header after http send to client

here i tried test code:

import 'pinpoint-node-agent';
import * as fs from 'fs';
import express from 'express';
import { S3Client, PutObjectCommand, CreateBucketCommand } from "@aws-sdk/client-s3";

// Set the AWS Region.
const REGION = "ap-northeast-2"; //e.g. "us-east-1"
// Create an Amazon S3 service client object.
const s3Client = new S3Client({ region: REGION });

const file = await fs.readFileSync(`./test.pdf`);

// Set the parameters
const params = {
    Bucket: "", // The name of the bucket. For example, 'sample_bucket_101'.
    Key: "", // The name of the object. For example, 'sample_upload.txt'.
    Body: Buffer.from(file), // The content of the object. For example, 'Hello world!".
    ContentType: 'application/pdf',
    ACL: 'private',
};

const run = async () => {
    // Create an object and upload it to the Amazon S3 bucket.
    for (let i=0; i<10; i++) {
        try {
            const results = await s3Client.send(new PutObjectCommand({...params, Key: `path/file-name${i}.pdf`}));
            console.log(
                "Successfully created " +
                params.Key +
                " and uploaded it to " +
                params.Bucket +
                "/" +
                params.Key +
                "\nresults:" + JSON.stringify(results)
            );
        } catch (err) {
            console.log("Error"+i, err);
        }
    }
};

const app = express()
const port = 3000

app.get('/', (req, res) => {
    run().then( () => {
        res.send('Hello World!')
    }).catch(e => {
        res.send(e)
    });
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})
//package.json
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@aws-sdk/client-s3": "^3.105.0",
    "express": "^4.18.1",
    "pinpoint-node-agent": "^0.8.3"
  },
  "type": "module"
}
PINPOINT_APPLICATION_NAME=TEST PINPOINT_AGENT_ID=TEST_AGENT node index.js

curl localhost:3000

Error9 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:371:5)
    at ClientRequest.setHeader (node:_http_outgoing:576:11)
    at Function.setHeader (/home/node/app/test/node_modules/pinpoint-node-agent/lib/instrumentation/request-header-utils.js:93:15)
    at Function.write (/home/node/app/test/node_modules/pinpoint-node-agent/lib/instrumentation/request-header-utils.js:79:12)
    at /home/node/app/test/node_modules/pinpoint-node-agent/lib/instrumentation/http-shared.js:119:26
    at /home/node/app/test/node_modules/@aws-sdk/node-http-handler/dist-cjs/node-http-handler.js:70:25
    at new Promise (<anonymous>)
    at NodeHttpHandler.handle (/home/node/app/test/node_modules/@aws-sdk/node-http-handler/dist-cjs/node-http-handler.js:49:16)
    at /home/node/app/test/node_modules/@aws-sdk/client-s3/dist-cjs/commands/PutObjectCommand.js:39:58
    at /home/node/app/test/node_modules/@aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:5:32 {
  code: 'ERR_HTTP_HEADERS_SENT',
  '$metadata': { attempts: 1, totalRetryDelay: 0 }
}

myuoong avatar Jun 08 '22 06:06 myuoong

Hello, myuoong

Thank you for your reports. It's a Pinpoint Node agent issue.

feelform avatar Jun 16 '22 07:06 feelform