-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (106 loc) · 2.99 KB
/
Copy pathindex.js
File metadata and controls
121 lines (106 loc) · 2.99 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const { trace } = require("@opentelemetry/api");
const pino = require("pino");
const GeocodingService = require("./geocoding");
const pinoOptions = {
// Set the global log level to the lowest level
// We adjust the level per transport
level: "trace",
hooks: {},
serializers: {
// Handle error properties as Error and serialize them correctly
err: pino.stdSerializers.err,
error: pino.stdSerializers.err,
validationErrors: pino.stdSerializers.err,
},
// Automatic trace context injection
mixin() {
const span = trace.getActiveSpan();
if (!span) return {};
const spanContext = span.spanContext();
return {
trace_id: spanContext.traceId,
span_id: spanContext.spanId,
trace_flags: spanContext.traceFlags,
};
},
};
const otlpTransport = pino.transport({
target: "pino-opentelemetry-transport",
options: {
loggerName: "pino-js-logger",
serviceVersion: "1.0.0",
},
});
const streams = [
{
level: "debug",
stream: otlpTransport,
},
];
const logger = pino(pinoOptions, pino.multistream(streams));
const geocodingService = new GeocodingService(logger);
exports.handler = async (event, context) => {
try {
logger.info("Lambda function invoked", {
requestId: context.awsRequestId,
functionName: context.functionName,
functionVersion: context.functionVersion,
});
logger.info("Processing event", { event });
// Extract zip code from event
const zipCode =
event.zipCode ||
event.body?.zipCode ||
event.queryStringParameters?.zipCode;
if (!zipCode) {
const error = new Error("Missing required parameter: zipCode");
logger.error("Missing zipCode parameter", { event });
return {
statusCode: 400,
body: JSON.stringify({
error: "Missing required parameter: zipCode",
usage:
"Include zipCode in event body, query parameters, or root level",
timestamp: new Date().toISOString(),
requestId: context.awsRequestId,
}),
};
}
// Get coordinates for the zip code
const coordinates =
await geocodingService.getCoordinatesFromZipCode(zipCode);
const response = {
statusCode: 200,
body: JSON.stringify({
zipCode,
coordinates,
timestamp: new Date().toISOString(),
requestId: context.awsRequestId,
}),
};
logger.info("Lambda function completed successfully", {
zipCode,
coordinates,
requestId: context.awsRequestId,
});
return response;
} catch (error) {
logger.error("Lambda function failed", {
error: error.message,
stack: error.stack,
requestId: context.awsRequestId,
});
return {
statusCode: 500,
body: JSON.stringify({
error: "Internal server error",
message: error.message,
timestamp: new Date().toISOString(),
requestId: context.awsRequestId,
}),
};
} finally {
logger.flush();
otlpTransport.flushSync();
}
};