-
Notifications
You must be signed in to change notification settings - Fork 807
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: introduces ability to suppress tracing via context #1344
Changes from 16 commits
207dfdc
f5d351c
98acb74
c2ac8b4
4d7e5ea
9a351ed
df7e85d
d588d4f
91d1add
9eb2d5e
d183bd8
4ff9591
6bb2eb6
49f53e4
74a4000
2866f51
6c81a98
cc0249b
f125085
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright The 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 { | ||
SUPPRESS_INSTRUMENTATION_KEY, | ||
suppressInstrumentation, | ||
unsuppressInstrumentation, | ||
isInstrumentationSuppressed, | ||
} from '../../src/context/context'; | ||
import { Context } from '@opentelemetry/api'; | ||
|
||
describe('Context Helpers', () => { | ||
describe('suppressInstrumentation', () => { | ||
it('should set suppress to true', () => { | ||
const expectedValue = true; | ||
const context = suppressInstrumentation(Context.ROOT_CONTEXT); | ||
|
||
const value = context.getValue(SUPPRESS_INSTRUMENTATION_KEY); | ||
const boolValue = value as boolean; | ||
|
||
assert.equal(boolValue, expectedValue); | ||
}); | ||
}); | ||
|
||
describe('unsuppressInstrumentation', () => { | ||
it('should set suppress to false', () => { | ||
const expectedValue = false; | ||
const context = unsuppressInstrumentation(Context.ROOT_CONTEXT); | ||
|
||
const value = context.getValue(SUPPRESS_INSTRUMENTATION_KEY); | ||
const boolValue = value as boolean; | ||
|
||
assert.equal(boolValue, expectedValue); | ||
}); | ||
}); | ||
|
||
describe('isInstrumentationSuppressed', () => { | ||
it('should get value as bool', () => { | ||
const expectedValue = true; | ||
const context = Context.ROOT_CONTEXT.setValue( | ||
SUPPRESS_INSTRUMENTATION_KEY, | ||
expectedValue | ||
); | ||
|
||
const value = isInstrumentationSuppressed(context); | ||
|
||
assert.equal(value, expectedValue); | ||
}); | ||
|
||
describe('when suppress instrumentation set to null', () => { | ||
const context = Context.ROOT_CONTEXT.setValue( | ||
SUPPRESS_INSTRUMENTATION_KEY, | ||
null | ||
); | ||
|
||
it('should return false', () => { | ||
const value = isInstrumentationSuppressed(context); | ||
|
||
assert.equal(value, false); | ||
}); | ||
}); | ||
|
||
describe('when suppress instrumentation set to undefined', () => { | ||
const context = Context.ROOT_CONTEXT.setValue( | ||
SUPPRESS_INSTRUMENTATION_KEY, | ||
undefined | ||
); | ||
|
||
it('should return false', () => { | ||
const value = isInstrumentationSuppressed(context); | ||
|
||
assert.equal(value, false); | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,20 +20,25 @@ import { ExportResult } from '@opentelemetry/core'; | |
|
||
/** | ||
* 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()` | ||
* in a list in memory that can be retrieved using the `getFinishedSpans()` | ||
* method. | ||
*/ | ||
export class InMemorySpanExporter implements SpanExporter { | ||
private _finishedSpans: ReadableSpan[] = []; | ||
private _stopped = false; | ||
/** | ||
* Indicates if the exporter has been "shutdown." | ||
* When false, exported spans will not be stored in-memory. | ||
*/ | ||
protected _stopped = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add some doc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @obecny what sort of doc are you looking for? do you want a comment on why this is protected? that seems like something that would quickly get out of date with future usages, outside of the obvious fact it is now available to classes extending the InMemorySpanExporter. or are you looking for something else? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If something is protected its meaning should be documented so that when someone subclasses this they know what the property represents. It could be argued it is obvious in this case, but it is good practice in general. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. i don't see that done for any other protected members (non-function) in the code base and somewhat debate the value here but am happy to go along with the standards y'all are looking for. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @michaelgoin I agree we haven't been great at being consistent about this, and in many cases the value is questionable anyways. |
||
|
||
export( | ||
spans: ReadableSpan[], | ||
resultCallback: (result: ExportResult) => void | ||
): void { | ||
if (this._stopped) return resultCallback(ExportResult.FAILED_NOT_RETRYABLE); | ||
this._finishedSpans.push(...spans); | ||
return resultCallback(ExportResult.SUCCESS); | ||
|
||
setTimeout(() => resultCallback(ExportResult.SUCCESS), 0); | ||
} | ||
|
||
shutdown(): void { | ||
|
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.
I would remove the extraneous cast and assignment, but this is minor.
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.
I was definitely giving that the side-eye while I was restructuring. Thanks for the extra push. :)
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.
It's probably minor, but since this function is likely to be called a lot, I think the performance benefit alone of skipping the assignment is worth it since you don't really lose anything here.