From 4d5de7f6edd1f89e4da0ab4f9c0af9d7b8b722bc Mon Sep 17 00:00:00 2001 From: Unitech Date: Wed, 8 Apr 2020 23:14:28 +0200 Subject: [PATCH] instant trace sending + send only slow traces --- CHANGELOG.md | 4 +++- NOTES.md | 45 +++++++++++++++++++++++++++++++++++++++++ src/census/constants.ts | 6 ++++-- src/census/exporter.ts | 9 +++++++-- 4 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 NOTES.md diff --git a/CHANGELOG.md b/CHANGELOG.md index c19cd70e..1e520cb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ## 4.3.4 +- Do not send trace faster than 1000ms +- Instantly send traces to pm2.io (instead of ~60secs timeout) - Allow metrics to be a boolean -- Add some examples +- Add some examples/ - Track changes via CHANGELOG.md diff --git a/NOTES.md b/NOTES.md new file mode 100644 index 00000000..306f7f36 --- /dev/null +++ b/NOTES.md @@ -0,0 +1,45 @@ + + +pm2-io-apm features are in src/features/: + +``` +src/features/ +├── dependencies.ts +├── entrypoint.ts +├── events.ts +├── metrics.ts +├── notify.ts +├── profiling.ts +└── tracing.ts +``` + +## Tracing + +- `./src/census` folder is essentially a dump of https://github.com/census-instrumentation/opencensus-node/tree/master/packages/opencensus-nodejs-base/src/trace with plugins added +- Only traces higher than `MINIMUM_TRACE_DURATION: 1000 * 1000` are sent to transporter (sent in /src/census/exporter.ts:72) + +Trace sent looks like: + +``` +{ + traceId: 'fac7052e9129416185a26d4935229620', + name: '/slow', + id: '66358f0a48be82c5', + parentId: '', + kind: 'SERVER', + timestamp: 1586380086251000, + duration: 2007559, + debug: false, + shared: false, + localEndpoint: { serviceName: 'tototransaction' }, + tags: { + 'http.host': 'localhost', + 'http.method': 'GET', + 'http.path': '/slow', + 'http.route': '/slow', + 'http.user_agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36', + 'http.status_code': '304', + 'result.code': undefined + } +} +``` diff --git a/src/census/constants.ts b/src/census/constants.ts index 87328fe1..c10e227c 100644 --- a/src/census/constants.ts +++ b/src/census/constants.ts @@ -18,10 +18,12 @@ import { resolve } from 'path' /** General purpose constants. */ const constants = { + /* CUSTOM - MINIMUM_DURATION trace in uS to send to backend (avoid *vaportrace*)*/ + MINIMUM_TRACE_DURATION: process.env.NODE_ENV == 'test' ? 0 : 1000 * 1000, /** Default maximum size of a buffer. */ - DEFAULT_BUFFER_SIZE: 100, + DEFAULT_BUFFER_SIZE: 0, /** Default max timeout for a buffer before being flushed */ - DEFAULT_BUFFER_TIMEOUT: 20000, + DEFAULT_BUFFER_TIMEOUT: 1000, /** Default list of target modules to be instrumented */ DEFAULT_INSTRUMENTATION_MODULES: [], /** OpenCensus Scope */ diff --git a/src/census/exporter.ts b/src/census/exporter.ts index cb71f959..c48285df 100644 --- a/src/census/exporter.ts +++ b/src/census/exporter.ts @@ -2,6 +2,8 @@ import { Transport } from '../services/transport' import { ServiceManager } from '../serviceManager' import { TracingConfig } from 'src/features/tracing' import { Exporter, ExporterBuffer, ExporterConfig, RootSpan, Span, SpanKind, Attributes, CanonicalCode } from '@opencensus/core' +import { defaultConfig } from './config/default-config' +import { Constants } from './constants' export interface ZipkinExporterOptions extends ExporterConfig { serviceName: string @@ -49,7 +51,7 @@ export class CustomCensusExporter implements Exporter { constructor (config: TracingConfig) { this.config = config - this.buffer = new ExporterBuffer(this, {}) + this.buffer = new ExporterBuffer(this, defaultConfig) } /** @@ -73,7 +75,10 @@ export class CustomCensusExporter implements Exporter { const isRootClient = span.kind === 'CLIENT' && !span.parentId if (isRootClient && this.config.outbound === false) return - this.transport.send('trace-span', span) + /** CUSTOM - DROP USELESS TRACE **/ + if (span.duration > Constants.MINIMUM_TRACE_DURATION) { + this.transport.send('trace-span', span) + } }) resolve() })