Skip to content

Instantly share code, notes, and snippets.

View endeepak's full-sized avatar

Deepak Narayana Rao endeepak

View GitHub Profile
@endeepak
endeepak / request-tracing-trace-method.js
Last active January 13, 2020 05:20
NodeJS Request Log Tracing : SQS Consumer Wrapper
const cls = require('cls-hooked');
const generate = require('nanoid/generate');
const alphaNumeric = '0123456789abcdefghijklmnopqrstuvwxyz';
const requestTracingNamespace = cls.createNamespace('request-tracing');
//.. middleware: https://gist.github.com/endeepak/4b8a07abfdf07964ea85d9cde44e02b9
const trace = async (tracingId, method) => {
requestTracingNamespace.run(async () => {
@endeepak
endeepak / sns-event-publisher.js
Last active January 13, 2020 05:06
NodeJS Request Log Tracing : SNS Event Publisher
const requestTracing = require('request-tracing'); // Wrapper library to read the tracing information from CLS context
class SNSEventPublisher {
constructor(sns, topicNameToArnMap) {
this.sns = sns;
this.topicNameToArnMap = topicNameToArnMap;
}
async publish(topicName, message) {
const messageAttributes = {};
@endeepak
endeepak / request-promise-wrapper.js
Created January 12, 2020 16:25
NodeJS Request Log Tracing : HTTP Request Wrapper
const request = require('request-promise');
const requestTracing = require('request-tracing'); // Wrapper library to read the tracing information from CLS context
const requestPromiseWrapper = async (body) => {
const requestBody = Object.assign({}, body);
requestBody.headers['X-Tracing-Id'] = requestTracing.getTracingId(); // Same as requestTracingNamespace.get('tracingId');
return request(requestBody);
};
@endeepak
endeepak / logger.js
Last active January 12, 2020 16:12
NodeJS Request Log Tracing : Logger
const { createLogger, transports, format } = require('winston');
const {combine, printf} = format;
const requestTracing = require('request-tracing'); // Wrapper library to read the tracing information from CLS context
const loggerFormat = printf((info) => {
const tracingId = requestTracing.getTracingId(); // Same as requestTracingNamespace.get(tracingIdContextKeyName);
let formatObject = `${info.level || '-'} ${info.timestamp || '-'} ${tracingId || '-'} ${info.message}`;
// ...
return formatObject;
});
@endeepak
endeepak / request-tracing-middleware.js
Last active January 12, 2020 15:59
NodeJS Request Log Tracing: Express Middleware
const cls = require('cls-hooked');
const generate = require('nanoid/generate');
const alphaNumeric = '0123456789abcdefghijklmnopqrstuvwxyz';
const requestTracingNamespace = cls.createNamespace('request-tracing');
const middleware = () => {
return (req, res, next) => {
requestTracingNamespace.bindEmitter(req);
requestTracingNamespace.bindEmitter(res);
@endeepak
endeepak / scheduled-task.js
Last active July 8, 2019 16:17
NodeJS scheduled task skeleton
var schedule = require('node-schedule');
schedule.scheduleJob('30 * * * *', function(){
doTheJob();
});
document.querySelectorAll('h1,h2,h3,h4,strong').forEach(function(header) {
console.log(' '.repeat(header.tagName.replace('STRONG', 'H5').replace('H', '')) + header.textContent)
});
@endeepak
endeepak / influxdb_measurement_series_count_per_measurement.sh
Created January 30, 2017 14:05
Influxdb : Print cardinality / series count per measurement
#!/usr/bin/env bash
if [ "$#" -ne 1 ]
then
echo "Usage: $0 <db_name>"
exit 1
fi
db_name=$1
HttpGet httpGet = new HttpGet("https://en.wikipedia.org/wiki/List_of_least_concern_birds");
HttpResponse httpResponse = client.execute(httpGet);
String responseHtml = IOUtils.toString(httpResponse.getEntity().getContent());
assertThat(responseHtml.length(), greaterThan(16348));
// Motor Code Adopted From: http://www.instructables.com/id/Control-your-motors-with-L293D-and-Arduino/?ALLSTEPS
// Ping Code Adopted From: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home#!single-pin-sketch
#include <NewPing.h>
#define PING_PIN 12 // Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define PING_MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(PING_PIN, PING_PIN, PING_MAX_DISTANCE); // NewPing setup of pin and maximum distance.