Request Contexts for Serverless Functions

Request Contexts for Serverless Functions

This library is based on the ‘async_hooks’ lib and will create a request scoped context, which can store or generate a correlation ID, as well as other meta data we may need about the request.

I’m currently a software engineer working on a serverless, event-driven, multi-tenanted, and distributed system built with TypeScript and hosted with Google Cloud Platform. The project began quite recently and had a major emphasis on throughput of work. Now however, we’ve hit a critical point in our product road map, and have begun to work on system stabilisation as the primary focus, before pressing on with new features.

To that end, the first and arguably most important thing missing from the system, was having correlated logs. As one might imagine, having a large and growing system without some form of log correlation, only leads to headaches and late night debugging sessions. So I decided to take on the daunting task of retrofitting log correlation across the systems.

For all of the HTTP endpoints, which use Express under the hood, this wasn’t a problem and simply intercepting all incoming requests with a middleware component was a doddle. The real problems started when looking at Firebase’s onCall and PubSub functions.

The Problem

The first major issue that I ran into, was that there obviously wasn’t any middleware component for onCall functions, nor were there any for PubSubfunctions. I certainly wasn’t going to settle for passing the correlation ID around in function parameters, as this is messy and unmaintainable in small systems, let alone a large and growing system. So I began to investigate how to create a request scoped context and quite quickly identified Node’s ‘async_hooks’ functionality as a possible saviour.

The ‘async_hooks’ library essentially allows you to track asynchronous tasks with a builtin API. Let’s take a look at how we can create a context, in which we can store a correlation ID, meta data about the request or anything else we may need to be accessible from within the scope of one of the above named functions.

software-development javascript typescript serverless programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

5 Core Criteria for Selecting Software Development Company -

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

Wrestling Betting Software Development | WWE Betting Software Developers

Wrestling betting software development solutions from expert Wrestling betting app and software development company delivers ultimate betting experience. Chat with us.

Developer Career Path: To Become a Team Lead or Stay a Developer?

For a developer, becoming a team leader can be a trap or open up opportunities for creating software. Two years ago, when I was a developer, ... by Oleg Sklyarov, Fullstack Developer at Skyeng company