-
Notifications
You must be signed in to change notification settings - Fork 4
/
post.js
73 lines (62 loc) · 1.89 KB
/
post.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
const uuid = require("uuid");
const AWS = require("aws-sdk");
const logger = require("logger");
const otel = require("@opentelemetry/api");
module.exports.handler = async (event) => {
logger.info("The event", event);
// Get the current active span
const activeSpan = otel.trace.getSpan(otel.context.active());
try {
const body = JSON.parse(event.body);
const post = {
id: uuid.v4(),
title: body.title,
content: body.content,
created: (new Date()).toISOString(),
};
// Add the post to the active span
activeSpan.setAttribute("post.id", post.id);
await writeToDb(post);
// Add an event to the span, signaling a post was created
activeSpan.addEvent("POST_CREATED", { "post.id": post.id });
return {
statusCode: 200,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message: 'Post created', post }),
};
} catch (error) {
logger.info("There was an error handling a request", { event, error });
activeSpan.recordException(error);
// Set the active span status to error
activeSpan.setStatus({ code: otel.SpanStatusCode.ERROR });
return {
statusCode: 500,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message: "Fatal Error" }),
}
}
};
async function writeToDb(post) {
const dynamoDb = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10', region: process.env.REGION });
const tableName = process.env.PROJECTION_TABLE;
const params = {
TableName: tableName,
Item: {
partitionKey: `post#${post.id}`,
data: post,
type: "post",
},
ConditionExpression: 'attribute_not_exists(partitionKey)'
};
try {
await dynamoDb.put(params).promise();
} catch (err) {
logger.error("There was an error writing to the database", { post, error });
throw err;
}
}