Azure AppInsights with Nodejs adding operation id to response

I needed to add an additional response header to my requests to aid in tracking errors.

Here is my solution. A middleware that will add the AppInsights Operation ID to the response. The Operation ID is unique to each request so searching for it is simple.

import { Request, Response, NextFunction } from 'express';
import appInsights from '../../lib/appInsights';

export default (req: Request, res: Response, next: NextFunction) => {          
    const { operation } = appInsights.getCorrelationContext();    
    
    res.setHeader('X-Operation-ID', operation.id);
    
    next();
};

Development questions…

I’ve been working on compiling a set of questions we should ask at the outset of each new feature. In my experience, teams often become laser-focused on coding and implementing new features without considering the broader context. I believe it’s crucial to be proactive and demonstrate the value of our work to the company, as ultimately, the bottom line is what matters most.

For every new feature, let’s ensure we ask these questions. The responses we gather can then inform the creation of tickets and specifications, ensuring that we’re aligned with both technical requirements and organizational objectives.

AspectQuestions
Top Level User Story– What is the main objective or goal of this feature/change?
Security Concerns– Are there any potential security vulnerabilities or risks associated with this work?
Organizational Policies and Processes– Are there specific policies or processes that need to be adhered to during development?
Value Addition– How does this work enhance the application/product?
– What additional benefits or improvements does it bring?
Justification– Why is this work necessary or important?
– What problem or need does it address?
Proving Worth– How can we demonstrate the impact or value of this work?
– What criteria or metrics can be used to measure its success?
Monitoring– What metrics or indicators can be used to track the performance or usage of this feature/change?
– How will we monitor its effectiveness over time?
Technical Requirements– Are there any specific technical constraints or dependencies that need to be considered?
– What technologies or frameworks should be utilized for this work?
User Experience (UX)– How will this work impact the user experience?
– Are there any usability considerations to be aware of?
Testing and Quality Assurance– What testing strategies will be employed to ensure the quality of the implementation?
– Are there any specific test cases or scenarios that need to be addressed?
Scalability and Performance– How will this work scale as the application grows?
– Are there any performance considerations or benchmarks to meet?
Documentation– What documentation needs to be created or updated as part of this work?
– How will knowledge transfer be facilitated for other team members?
Deployment and Rollout– What is the deployment plan for this work?
– Are there any rollout or release strategies to consider?
Feedback and Iteration– How will feedback be collected and incorporated into future iterations?
– What mechanisms are in place for continuous improvement?
Collaboration and Communication– How will communication be maintained between team members and stakeholders throughout the process?
– Are there any collaboration tools or platforms to be used?
Risk Management– What potential risks or challenges could arise during implementation, and how will they be mitigated?
– Is there a contingency plan in place for unexpected issues?

Replacing ObjectId with a string in JSON. Using RegEx

Problem: I have a data dump of a MongoDb query in a JSON file. I need to replace the ObjectID(“12345677abc”) with “12345677abc”.

Using Visual Studio Code’s find and replace

Find:

ObjectId\("([0-9a-fA-F]{24})"\)

Replace with: “$1”

Turns this

"_id" : ObjectId("5e3b1890e032d225a091d43f"),
"userId" : ObjectId("65ed1c2c-922c-4c82-b5bc-7324f69eea10"),

To this

"_id" : "5e3b1890e032d225a091d43f",
"userId" : "65ed1c2c-922c-4c82-b5bc-7324f69eea10",


Bonus:

ISODate\("([^"]+)"\)

Mastering New Software with Ease: The Three “Software Whisperer” Principles

Introduction:
Have you ever wondered how some people effortlessly grasp new software applications with minimal training? I was intrigued by this ability and decided to explore the secrets behind their success. Through my journey, I discovered three powerful principles that transformed my approach to learning new software. In this blog post, I’ll share my experience applying the three “Software Whisperer” principles that have made me a more adept learner in the digital landscape.

Embracing the Intuitive Explorer:
The first principle I adopted was the fusion of intuition and curiosity as a powerful learning tool. I began trusting my instincts and letting my inquisitiveness guide me as I delved into new applications. Embracing various approaches and adapting my mindset based on my discoveries proved highly effective. With each encounter, my intuition strengthened, enabling me to navigate new software environments effortlessly.

Uncovering the Hidden Threads:
Identifying similarities with applications I’d used before became my focus. I familiarized myself with common user interface patterns and leveraged transferable skills from past experiences, which significantly accelerated my learning. Embracing trial and error allowed me to learn from mistakes and refine my approach. By unveiling the hidden threads connecting interface patterns, transferable skills, and trial and error experiences, I quickly mastered new software.

Becoming an Analytical Visionary:
Applying analytical thinking, problem-solving skills, and visual thinking also played a crucial role in my software learning journey. I broke down complex tasks into manageable steps and analyzed the underlying logic of the software to comprehend its workings. Creating mental images or visual representations of information within the application aided in grasping complex concepts and systems. The combination of these three aspects empowered me to conquer challenges and navigate new software with ease.

Conclusion:
By applying the three “Software Whisperer” principles—Embracing the Intuitive Explorer, Uncovering the Hidden Threads, and Becoming an Analytical Visionary—I have significantly improved my ability to understand and use new software applications with little to no prior training. While patience and persistence are essential when venturing into unfamiliar digital terrain, the rewards are well worth it. I now conquer software challenges effortlessly, akin to a true Software Whisperer. I encourage you to try these principles for yourself and witness the difference they can make in your software learning journey. Trust me, they have been a game-changer for me, and I believe they can do the same for you too!

Time to come clean, I used ChatGPT to create this! I copied the original text (see link below) pasted into ChatGPT and experimented with different prompts.

Reference: https://www.linkedin.com/pulse/unlock-your-inner-software-whisperer-3-simple-vishvakanth-alexander%3FtrackingId=u0nGp05rTpawtiS5CF9NAQ%253D%253D/?trackingId=u0nGp05rTpawtiS5CF9NAQ%3D%3D

With a “Ranting Donald Trump” prompt:

Then, we’ve got the Hidden Threads. You won’t believe what I uncovered. I looked at other applications, and I saw similarities, big similarities! It’s like they were all connected, like a beautiful web, a tremendous web!

Postman and Faker

I am a huge fan of Postman when developing API’s. I dont think i use even 20% of its abilities.

Today I found out that the Postman Pre-request scripts also have access to Faker.

Here is an example dynamic request body for creating a user.

const randomInt = Math.floor((Math.random()*100 +1));

const firstName = pm.variables.replaceIn("{{$randomFirstName}}");
const lastName = pm.variables.replaceIn("{{$randomLastName}}");
const domain = pm.variables.replaceIn("{{$randomCompanyName}}").replace(/[, ]+/g, ".").trim();


const body = {
firstName,
lastName,
email: ${firstName}.${lastName}@${domain}.c
om

}

pm.environment.set('body', JSON.stringify(body))

Here is Postman’s list of Faker options

https://learning.postman.com/docs/writing-scripts/script-references/variables-list/

Another cURL: This time validating TLS versions

I had to demonstrate that our hosting of a single page app did not accept TLS version 1.1,

This is the proof

curl -o /dev/null -s -w "%{http_code}\n" https://koala-moon.com --insecure --tls-max 1.1
Returns: 0000

curl -o /dev/null -s -w "%{http_code}\n" https://koala-moon.com --insecure --tls-max 1.2
Returns: 200

Easy eh?

API development: Lazy tip # 25

TL;DR;

Use cURL and pipe to json_pp “| json_pp

So for the longest time when working on API’s my process would look like this:

  • Write code in Visual Studio Code
  • Change to Postman and click send (try again as Nodemon has not finished restarting)
  • Check result
  • Change back to Visual Studio Code
  • Repeat

But now its

  • Write code in Visual Studio Code
  • In the Visual Studio Code terminal press ‘up arrow’ then ‘return’
  • Check result
  • Repeat

This has speeded up my development and reduced frustration.

How?
In the terminal I make use of cURL and append the command with | json_pp

For example

curl --location --request POST 'localhost:61100/my-service/report/progress' \
--header 'Authorization: <token> \
--header 'Content-Type: application/json' \
--data-raw '{"ids":["5d765d10a13c116f3ce44d04","62bc1433eb87da610a436a48","62bc7a1147f2e0f3854aa268","62bc2dd747f2e04f724a9e26"]}' | json_pp

Top tip:

Use postman on the first iteration, then use it to export cURL command

Fire and Forget

Yesterday was spent diagnosing what was reported as CORS issue in a Node service used for logging that was being called by front end apps.

CORS turned out to be a red herring, it ended up being the wrong format for a new database connection string, less than gracefully handling errors (it would just die and return 500), but what concerned me was the frontend request (sending a packet to a remote logging service) was not necessary to the successful sequence of editing and saving an object (plus it was sent prior to the event being successful) and would halt following requests.

The frontend fix was a “fire and forget” call to send a logging packet to the Node service, while not ideal if the service was down it would not effect frontend users experience.

const logEvent = (str) => {
  const send = async (str) => {
    // send the request, it might throw an error
    throw new Error(`thrown....${str}`)
  }

  send(str).catch(() => {
    // If you don't catch the error you can/will end up with a non handled exception error.
    console.log('It did not break, log error with service like Azure App Insights')
  })
}

console.log(
  logEvent("do nothing")
)

NPM update

So, for a long time now our projects CI/CD pipelines have an audit step and will prevent merging if there and high risk issues. Nothing complicated just

npm audit

Then a developer would need to sit down and follow their own approach to updating NPM packages. Mine was to install all the patch versions – test, minor versions and test. Then I’d take a stab at any major versions after reading any release notes.

See: https://docs.npmjs.com/about-semantic-versioning

It works, but it is a pain.

Then I found NPM GUI

https://www.npmjs.com/package/npm-gui

After installing it globally, I run the following command

npm-gui

This opens a browser tab and will first show all the global packages and versions. Navigate to your directory that has the package.json file, a nice table of installed node packages is displayed with required, current, compatible and latest versions. Clicking the versions will install them.

I still follow the same work flow of patches, minor and major versions but it takes me far less time and cognitive load to update projects.

It might not be perfect, but the rest of my team have started using this and its helped overcome the dread of package maintenance.