Skip to content

Instantly share code, notes, and snippets.

$ npm ls
trace-pin-sms@1.0.0 /Users/kelvinjin/src/experiments/trace-pin-sms
├─┬ @google-cloud/trace-agent@4.2.2
│ ├─┬ @google-cloud/common@2.2.3
│ │ ├── @google-cloud/projectify@1.0.1
│ │ ├── @google-cloud/promisify@1.0.2
│ │ ├── arrify@2.0.1
│ │ ├─┬ duplexify@3.7.1
│ │ │ ├─┬ end-of-stream@1.4.4
│ │ │ │ └─┬ once@1.4.0
const tracer = require('@google-cloud/trace-agent').start();
const express = require('express');
const app = express();
// Middleware that just adds a label to each trace.
app.use((req, res, next) => {
tracer.getCurrentRootSpan().addLabel('key', 'value');
next();
@kjin
kjin / trace-1074.js
Last active September 10, 2019 00:04
const propagation = require('@opencensus/propagation-b3');
const tracer = require('@google-cloud/trace-agent').start({
rootSpanNameOverride: ((path) => `http://helloworld${path}`),
ignoreUrls: ['/metrics', '/api_version'],
samplingRate: 0.1,
propagation: new propagation.B3Format(),
disableUntracedModulesWarning: true
});
const tracer = require('@google-cloud/trace-agent').start({
// Settings recommended for Cloud Functions.
samplingRate: 0,
bufferSize: 1
});
app.get('/', (req, res) => {
tracer.runInRootSpan({
name: 'outer', // Your function name here
traceContext: tracer.propagation.extract(key => req.headers[key]),
// Plugin for superagent@5
module.exports = [
{
file: '',
patch: (superagent, tracer) => {
const UnpatchedRequest = superagent.Request;
superagent.Request = Object.assign(function Request(...args) {
const result = UnpatchedRequest.apply(this, args);
// Request#end sends the actual request.
// It's called within a custom thenable's `then` implementation which,
@kjin
kjin / index.js
Created April 26, 2019 17:45
spansPerTraceSoftLimit example
const trace = require('@google-cloud/trace-agent');
trace.start({
spansPerTraceSoftLimit: 1000,
// ... other options
});
@kjin
kjin / stackdriver-logging-winston-default-meta.js
Last active April 9, 2019 18:02
Exports an object to set as Winston `defaultMeta` (do not use Object.assign to merge with other default metadata first)
let stackdriverLoggingWinston = null;
try {
stackdriverLoggingWinston = require('@google-cloud/logging-winston');
} catch (e) {}
const agent = global._google_trace_agent;
const enableThunkTimestamp = !!stackdriverLoggingWinston;
const enableThunkAgent = !!(stackdriverLoggingWinston
&& stackdriverLoggingWinston.LOGGING_TRACE_KEY
const ah = require('async_hooks');
const express = require('express');
const { createLogger } = require('winston');
const TransportStream = require('winston-transport');
// Activate async hooks.
let requestIdHighWater = 0;
const requestIdTable = {};
ah.createHook({
init: (child, resource, parent) => {
const { default: axios } = require('axios');
const num = Number(process.argv[2]);
for (let i = 0; i < num; i++) {
setTimeout(() => axios.get('http://localhost:3000'), Math.floor(Math.random() * 200));
}
// Client for https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_server/main.go.
// 'helloworld.proto' is https://github.com/grpc/grpc-go/blob/master/examples/helloworld/helloworld/helloworld.proto.
const tracer = require('@google-cloud/trace-agent').start({ bufferSize: 1, samplingRate: 0 });
const grpc = require('grpc');
const bluebird = require('bluebird');
const protoFile = __dirname + '/helloworld.proto';
const proto = grpc.load(protoFile).helloworld;