Skip to content

Commit

Permalink
instant trace sending + send only slow traces
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed Apr 8, 2020
1 parent 68c97ec commit 4d5de7f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -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
}
}
```
6 changes: 4 additions & 2 deletions src/census/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
9 changes: 7 additions & 2 deletions src/census/exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

/**
Expand All @@ -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()
})
Expand Down

0 comments on commit 4d5de7f

Please sign in to comment.