-
Notifications
You must be signed in to change notification settings - Fork 833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add InMemorySpanExporter, MultiSpanProcessor #234
Merged
mayurkale22
merged 2 commits into
open-telemetry:master
from
mayurkale22:multispanprocessor
Sep 9, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/opentelemetry-basic-tracer/src/MultiSpanProcessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Span } from '@opentelemetry/types'; | ||
import { SpanProcessor } from './SpanProcessor'; | ||
|
||
/** | ||
* Implementation of the {@link SpanProcessor} that simply forwards all | ||
* received events to a list of {@link SpanProcessor}s. | ||
*/ | ||
export class MultiSpanProcessor implements SpanProcessor { | ||
constructor(private readonly _spanProcessors: SpanProcessor[]) {} | ||
|
||
onStart(span: Span): void { | ||
for (const spanProcessor of this._spanProcessors) { | ||
spanProcessor.onStart(span); | ||
} | ||
} | ||
|
||
onEnd(span: Span): void { | ||
for (const spanProcessor of this._spanProcessors) { | ||
spanProcessor.onEnd(span); | ||
} | ||
} | ||
|
||
shutdown(): void { | ||
for (const spanProcessor of this._spanProcessors) { | ||
spanProcessor.shutdown(); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/opentelemetry-basic-tracer/src/NoopSpanProcessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Span } from '@opentelemetry/types'; | ||
import { SpanProcessor } from './SpanProcessor'; | ||
|
||
/** No-op implementation of SpanProcessor */ | ||
export class NoopSpanProcessor implements SpanProcessor { | ||
onStart(span: Span): void {} | ||
onEnd(span: Span): void {} | ||
shutdown(): void {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/opentelemetry-basic-tracer/src/export/InMemorySpanExporter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { SpanExporter } from './SpanExporter'; | ||
import { ReadableSpan } from './ReadableSpan'; | ||
import { ExportResult } from './ExportResult'; | ||
|
||
/** | ||
* This class can be used for testing purposes. It stores the exported spans | ||
* in a list in memory that can be retrieve using the `getFinishedSpans()` | ||
* method. | ||
*/ | ||
export class InMemorySpanExporter implements SpanExporter { | ||
private _finishedSpans: ReadableSpan[] = []; | ||
private _stopped = false; | ||
|
||
export( | ||
spans: ReadableSpan[], | ||
resultCallback: (result: ExportResult) => void | ||
): void { | ||
if (this._stopped) return resultCallback(ExportResult.FailedNonRetryable); | ||
this._finishedSpans.push(...spans); | ||
return resultCallback(ExportResult.Success); | ||
} | ||
|
||
shutdown(): void { | ||
this._stopped = true; | ||
this._finishedSpans = []; | ||
} | ||
|
||
reset() { | ||
this._finishedSpans = []; | ||
} | ||
|
||
getFinishedSpans(): ReadableSpan[] { | ||
return this._finishedSpans; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/opentelemetry-basic-tracer/test/MultiSpanProcessor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import { MultiSpanProcessor } from '../src/MultiSpanProcessor'; | ||
import { SpanProcessor, Span, BasicTracer } from '../src'; | ||
import { NoopScopeManager } from '@opentelemetry/scope-base'; | ||
|
||
class TestProcessor implements SpanProcessor { | ||
spans: Span[] = []; | ||
onStart(span: Span): void {} | ||
onEnd(span: Span): void { | ||
this.spans.push(span); | ||
} | ||
shutdown(): void { | ||
this.spans = []; | ||
} | ||
} | ||
|
||
describe('MultiSpanProcessor', () => { | ||
const tracer = new BasicTracer({ | ||
scopeManager: new NoopScopeManager(), | ||
}); | ||
const span = tracer.startSpan('one'); | ||
|
||
it('should handle empty span processor', () => { | ||
const multiSpanProcessor = new MultiSpanProcessor([]); | ||
multiSpanProcessor.onStart(span); | ||
multiSpanProcessor.onEnd(span); | ||
multiSpanProcessor.shutdown(); | ||
}); | ||
|
||
it('should handle one span processor', () => { | ||
const processor1 = new TestProcessor(); | ||
const multiSpanProcessor = new MultiSpanProcessor([processor1]); | ||
multiSpanProcessor.onStart(span); | ||
assert.strictEqual(processor1.spans.length, 0); | ||
multiSpanProcessor.onEnd(span); | ||
assert.strictEqual(processor1.spans.length, 1); | ||
}); | ||
|
||
it('should handle two span processor', () => { | ||
const processor1 = new TestProcessor(); | ||
const processor2 = new TestProcessor(); | ||
const multiSpanProcessor = new MultiSpanProcessor([processor1, processor2]); | ||
multiSpanProcessor.onStart(span); | ||
assert.strictEqual(processor1.spans.length, 0); | ||
assert.strictEqual(processor1.spans.length, processor2.spans.length); | ||
multiSpanProcessor.onEnd(span); | ||
assert.strictEqual(processor1.spans.length, 1); | ||
assert.strictEqual(processor1.spans.length, processor2.spans.length); | ||
|
||
multiSpanProcessor.shutdown(); | ||
assert.strictEqual(processor1.spans.length, 0); | ||
assert.strictEqual(processor1.spans.length, processor2.spans.length); | ||
}); | ||
}); |
90 changes: 90 additions & 0 deletions
90
packages/opentelemetry-basic-tracer/test/export/InMemorySpanExporter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import { | ||
InMemorySpanExporter, | ||
SimpleSpanProcessor, | ||
BasicTracer, | ||
ExportResult, | ||
} from '../../src'; | ||
import { NoopScopeManager } from '@opentelemetry/scope-base'; | ||
|
||
describe('InMemorySpanExporter', () => { | ||
const memoryExporter = new InMemorySpanExporter(); | ||
const tracer = new BasicTracer({ | ||
scopeManager: new NoopScopeManager(), | ||
}); | ||
tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); | ||
|
||
afterEach(() => { | ||
// reset spans in memory. | ||
memoryExporter.reset(); | ||
}); | ||
|
||
it('should get finished spans', () => { | ||
const root = tracer.startSpan('root'); | ||
const child = tracer.startSpan('child', { parent: root }); | ||
const grandChild = tracer.startSpan('grand-child', { parent: child }); | ||
|
||
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); | ||
grandChild.end(); | ||
assert.strictEqual(memoryExporter.getFinishedSpans().length, 1); | ||
child.end(); | ||
assert.strictEqual(memoryExporter.getFinishedSpans().length, 2); | ||
root.end(); | ||
assert.strictEqual(memoryExporter.getFinishedSpans().length, 3); | ||
|
||
const [span1, span2, span3] = memoryExporter.getFinishedSpans(); | ||
assert.strictEqual(span1.name, 'grand-child'); | ||
assert.strictEqual(span2.name, 'child'); | ||
assert.strictEqual(span3.name, 'root'); | ||
assert.strictEqual(span1.spanContext.traceId, span2.spanContext.traceId); | ||
assert.strictEqual(span2.spanContext.traceId, span3.spanContext.traceId); | ||
assert.strictEqual(span1.parentSpanId, span2.spanContext.spanId); | ||
assert.strictEqual(span2.parentSpanId, span3.spanContext.spanId); | ||
}); | ||
|
||
it('should shutdown the exorter', () => { | ||
const root = tracer.startSpan('root'); | ||
tracer.startSpan('child', { parent: root }).end(); | ||
root.end(); | ||
assert.strictEqual(memoryExporter.getFinishedSpans().length, 2); | ||
memoryExporter.shutdown(); | ||
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); | ||
|
||
// after shutdown no new spans are accepted | ||
tracer.startSpan('child1', { parent: root }).end(); | ||
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); | ||
}); | ||
|
||
it('should return the success result', () => { | ||
const exorter = new InMemorySpanExporter(); | ||
exorter.export([], (result: ExportResult) => { | ||
assert.strictEqual(result, ExportResult.Success); | ||
}); | ||
}); | ||
|
||
it('should return the FailedNonRetryable result after shutdown', () => { | ||
const exorter = new InMemorySpanExporter(); | ||
exorter.shutdown(); | ||
|
||
// after shutdown export should fail | ||
exorter.export([], (result: ExportResult) => { | ||
assert.strictEqual(result, ExportResult.FailedNonRetryable); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stopped = false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reset
is just to clean the collected spans. If we make itstopped = false
, it won't export/collect any spans afterreset
.