Skip to content

Commit

Permalink
chore(examples): Adding Serverless Example
Browse files Browse the repository at this point in the history
  • Loading branch information
xoscar committed Feb 9, 2024
1 parent 3bfdbab commit 260fb06
Show file tree
Hide file tree
Showing 9 changed files with 2,379 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/tracetest-serverless/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TRACETEST_AGENT_ENDPOINT=
7 changes: 7 additions & 0 deletions examples/tracetest-serverless/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
.env
3 changes: 3 additions & 0 deletions examples/tracetest-serverless/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
92 changes: 92 additions & 0 deletions examples/tracetest-serverless/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<!--
title: 'AWS Simple HTTP Endpoint example in NodeJS'
description: 'This template demonstrates how to make a simple HTTP API with Node.js running on AWS Lambda and API Gateway using the Serverless Framework.'
layout: Doc
framework: v3
platform: AWS
language: nodeJS
authorLink: 'https://github.com/serverless'
authorName: 'Serverless, inc.'
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
-->

# Serverless Framework Node HTTP API on AWS

This template demonstrates how to make a simple HTTP API with Node.js running on AWS Lambda and API Gateway using the Serverless Framework.

This template does not include any kind of persistence (database). For more advanced examples, check out the [serverless/examples repository](https://github.com/serverless/examples/) which includes Typescript, Mongo, DynamoDB and other examples.

## Usage

### Deployment

```
$ serverless deploy
```

After deploying, you should see output similar to:

```bash
Deploying aws-node-http-api-project to stage dev (us-east-1)

✔ Service deployed to stack aws-node-http-api-project-dev (152s)

endpoint: GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/
functions:
hello: aws-node-http-api-project-dev-hello (1.9 kB)
```

_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/).

### Invocation

After successful deployment, you can call the created application via HTTP:

```bash
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
```

Which should result in response similar to the following (removed `input` content for brevity):

```json
{
"message": "Go Serverless v2.0! Your function executed successfully!",
"input": {
...
}
}
```

### Local development

You can invoke your function locally by using the following command:

```bash
serverless invoke local --function hello
```

Which should result in response similar to the following:

```
{
"statusCode": 200,
"body": "{\n \"message\": \"Go Serverless v3.0! Your function executed successfully!\",\n \"input\": \"\"\n}"
}
```


Alternatively, it is also possible to emulate API Gateway and Lambda locally by using `serverless-offline` plugin. In order to do that, execute the following command:

```bash
serverless plugin install -n serverless-offline
```

It will add the `serverless-offline` plugin to `devDependencies` in `package.json` file as well as will add it to `plugins` in `serverless.yml`.

After installation, you can start local emulation with:

```
serverless offline
```

To learn more about the capabilities of `serverless-offline`, please refer to its [GitHub repository](https://github.com/dherault/serverless-offline).
29 changes: 29 additions & 0 deletions examples/tracetest-serverless/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const https = require('https');

function getRequest() {
const url = 'https://tracetest.io/';

return new Promise((resolve, reject) => {
const req = https.get(url, (res) => {
resolve(res.statusCode);
});

req.on('error', (err) => {
reject(new Error(err));
});
});
}

exports.handler = async (event) => {
try {
const result = await getRequest();
return {
statusCode: result,
};
} catch (error) {
return {
statusCode: 400,
body: error.message,
};
}
};
31 changes: 31 additions & 0 deletions examples/tracetest-serverless/lambda-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const api = require('@opentelemetry/api');
const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');

api.diag.setLogger(new api.DiagConsoleLogger(), api.DiagLogLevel.ALL);

const { COLLECTOR_ENDPOINT = '' } = process.env;

const provider = new NodeTracerProvider();

const spanProcessor = new BatchSpanProcessor(
new OTLPTraceExporter({
url: COLLECTOR_ENDPOINT,
})
);

provider.addSpanProcessor(spanProcessor);
provider.register();

registerInstrumentations({
instrumentations: [
getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-aws-lambda': {
disableAwsContextPropagation: true,
},
}),
],
});
Loading

0 comments on commit 260fb06

Please sign in to comment.