Skip to content

Commit

Permalink
changed method of restricting number of segments
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-pytel committed May 12, 2021
1 parent 4bf24c4 commit 06db610
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
7 changes: 0 additions & 7 deletions src/agent/Buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
*/

import { createLogger } from '../logging';
import config from '../config/AgentConfig';

const logger = createLogger(__filename);

export default class Buffer<T> {
private readonly maxSize: number;
private readonly buffer: T[];

constructor() {
this.maxSize = config.maxBufferSize;
this.buffer = [];
}

Expand All @@ -36,10 +33,6 @@ export default class Buffer<T> {
}

put(element: T): boolean {
if (this.length > this.maxSize) {
logger.warn('Drop the data because of the buffer is oversize');
return false;
}
this.buffer.push(element);

return true;
Expand Down
2 changes: 2 additions & 0 deletions src/agent/protocol/grpc/clients/TraceReportClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export default class TraceReportClient implements Client {

start() {
const reportFunction = () => {
emitter.emit('segments-sent'); // reset limiter in SpanContext

try {
if (this.buffer.length === 0) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/lib/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Segment from '../trace/context/Segment';

declare interface SkyWalkingEventEmitter {
on(event: 'segment-finished', listener: (segment: Segment) => void): this;
on(event: 'segments-sent', listener: () => void): this;
}

class SkyWalkingEventEmitter extends EventEmitter {
Expand Down
10 changes: 9 additions & 1 deletion src/trace/context/ContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
*
*/

import config from '../../config/AgentConfig';
import Context from '../../trace/context/Context';
import Span from '../../trace/span/Span';
import SpanContext from '../../trace/context/SpanContext';
import DummyContext from '../../trace/context/DummyContext';

import async_hooks from 'async_hooks';

Expand Down Expand Up @@ -81,7 +83,13 @@ class ContextManager {
get current(): Context {
const asyncState = this.asyncState;

return !asyncState.spans.length ? new SpanContext() : asyncState.spans[asyncState.spans.length - 1].context;
if (asyncState.spans.length)
return asyncState.spans[asyncState.spans.length - 1].context;

if (SpanContext.nActiveSegments < config.maxBufferSize)
return new SpanContext();

return new DummyContext();
}

get spans(): Span[] {
Expand Down
8 changes: 7 additions & 1 deletion src/trace/context/SpanContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ import { emitter } from '../../lib/EventEmitter';

const logger = createLogger(__filename);

emitter.on('segments-sent', () => {
SpanContext.nActiveSegments = 0; // reset limiter
});

export default class SpanContext implements Context {
static nActiveSegments = 0; // counter to allow only config.maxBufferSize active (non-dummy) segments per reporting frame
spanId = 0;
nSpans = 0;
finished = false;
Expand Down Expand Up @@ -166,7 +171,8 @@ export default class SpanContext implements Context {
nSpans: this.nSpans,
});

this.nSpans += 1;
if (!this.nSpans++)
SpanContext.nActiveSegments += 1;

if (ContextManager.spans.indexOf(span) === -1)
ContextManager.spans.push(span);
Expand Down

0 comments on commit 06db610

Please sign in to comment.