-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: upstream mocha instrumentation testing plugin from ext-js (#621)
* chore: change test utils files structure to match repo * feat(test-utils): mocha plugin for instrumentation testing * chore(instrumentation-mongodb): use test-utils to run tests * chore: lint and revert dep remove * Update packages/opentelemetry-test-utils/README.md * style: rename registerInstrumentation function * chore: fix lint * fix: wrong import of test-utils package * fix: entry point for test utils package * style(test-utils): fix lint * fix: don't hoist mocha for the plugin to take linked test-utils * ci: also don't hoist ts-mocha * chore: remove test-utils from root package.json
- Loading branch information
Amir Blum
authored
Sep 20, 2021
1 parent
1b7ae9c
commit e7e0e00
Showing
13 changed files
with
304 additions
and
116 deletions.
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
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
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
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 |
---|---|---|
|
@@ -16,3 +16,4 @@ | |
|
||
export * from './resource-assertions'; | ||
export * from './test-utils'; | ||
export * from './instrumentations'; |
56 changes: 56 additions & 0 deletions
56
packages/opentelemetry-test-utils/src/instrumentations/index.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,56 @@ | ||
/* | ||
* 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 { Resource } from '@opentelemetry/resources'; | ||
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; | ||
import { getInstrumentation } from './instrumentation-singelton'; | ||
import { registerInstrumentationTestingProvider } from './otel-default-provider'; | ||
import { resetMemoryExporter } from './otel-provider-api'; | ||
|
||
export * from './instrumentation-singelton'; | ||
export * from './otel-provider-api'; | ||
export * from './otel-default-provider'; | ||
|
||
export const mochaHooks = { | ||
beforeAll(done: Function) { | ||
// since we run mocha executable, process.argv[1] will look like this: | ||
// ${root instrumentation package path}/node_modules/.bin/mocha | ||
// this is not very robust, might need to refactor in the future | ||
let serviceName = 'unknown_instrumentation'; | ||
if (process.env.OTEL_SERVICE_NAME) { | ||
serviceName = process.env.OTEL_SERVICE_NAME; | ||
} else { | ||
try { | ||
serviceName = require(process.argv[1] + '/../../../package.json').name; | ||
} catch { | ||
// could not determine serviceName, continue regardless of this | ||
} | ||
} | ||
const provider = registerInstrumentationTestingProvider({ | ||
resource: new Resource({ | ||
[SemanticResourceAttributes.SERVICE_NAME]: serviceName, | ||
}), | ||
}); | ||
getInstrumentation()?.setTracerProvider(provider); | ||
done(); | ||
}, | ||
|
||
beforeEach(done: Function) { | ||
resetMemoryExporter(); | ||
// reset the config before each test, so that we don't leak state from one test to another | ||
getInstrumentation()?.setConfig({}); | ||
done(); | ||
}, | ||
}; |
42 changes: 42 additions & 0 deletions
42
packages/opentelemetry-test-utils/src/instrumentations/instrumentation-singelton.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,42 @@ | ||
/* | ||
* 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 { InstrumentationBase } from '@opentelemetry/instrumentation'; | ||
|
||
const OTEL_TESTING_INSTRUMENTATION_SINGLETON = Symbol.for( | ||
'opentelemetry.testing.instrumentation_singleton' | ||
); | ||
|
||
type OTelInstrumentationSingeltonGlobal = { | ||
[OTEL_TESTING_INSTRUMENTATION_SINGLETON]?: InstrumentationBase; | ||
}; | ||
const _global = global as OTelInstrumentationSingeltonGlobal; | ||
|
||
export const getInstrumentation = <T extends InstrumentationBase>(): | ||
| T | ||
| undefined => { | ||
return _global[OTEL_TESTING_INSTRUMENTATION_SINGLETON] as T; | ||
}; | ||
|
||
export const registerInstrumentationTesting = <T extends InstrumentationBase>( | ||
instrumentation: T | ||
): T => { | ||
const existing = getInstrumentation<T>(); | ||
if (existing) { | ||
return existing; | ||
} | ||
_global[OTEL_TESTING_INSTRUMENTATION_SINGLETON] = instrumentation; | ||
return instrumentation; | ||
}; |
48 changes: 48 additions & 0 deletions
48
packages/opentelemetry-test-utils/src/instrumentations/otel-default-provider.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,48 @@ | ||
/* | ||
* 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 { JaegerExporter } from '@opentelemetry/exporter-jaeger'; | ||
import { | ||
NodeTracerProvider, | ||
NodeTracerConfig, | ||
} from '@opentelemetry/sdk-trace-node'; | ||
import { | ||
InMemorySpanExporter, | ||
SimpleSpanProcessor, | ||
} from '@opentelemetry/sdk-trace-base'; | ||
import { | ||
getTestMemoryExporter, | ||
setTestMemoryExporter, | ||
} from './otel-provider-api'; | ||
|
||
export const registerInstrumentationTestingProvider = ( | ||
config?: NodeTracerConfig | ||
): NodeTracerProvider => { | ||
const otelTestingProvider = new NodeTracerProvider(config); | ||
|
||
setTestMemoryExporter(new InMemorySpanExporter()); | ||
otelTestingProvider.addSpanProcessor( | ||
new SimpleSpanProcessor(getTestMemoryExporter()!) | ||
); | ||
|
||
if (process.env.OTEL_EXPORTER_JAEGER_AGENT_HOST) { | ||
otelTestingProvider.addSpanProcessor( | ||
new SimpleSpanProcessor(new JaegerExporter()) | ||
); | ||
} | ||
|
||
otelTestingProvider.register(); | ||
return otelTestingProvider; | ||
}; |
44 changes: 44 additions & 0 deletions
44
packages/opentelemetry-test-utils/src/instrumentations/otel-provider-api.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 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 { | ||
InMemorySpanExporter, | ||
ReadableSpan, | ||
} from '@opentelemetry/sdk-trace-base'; | ||
|
||
const OTEL_TESTING_MEMORY_EXPORTER = Symbol.for( | ||
'opentelemetry.testing.memory_exporter' | ||
); | ||
|
||
type OTelProviderApiGlobal = { | ||
[OTEL_TESTING_MEMORY_EXPORTER]?: InMemorySpanExporter; | ||
}; | ||
const _global = global as OTelProviderApiGlobal; | ||
|
||
export const getTestMemoryExporter = (): InMemorySpanExporter | undefined => { | ||
return _global[OTEL_TESTING_MEMORY_EXPORTER]; | ||
}; | ||
|
||
export const setTestMemoryExporter = (memoryExporter: InMemorySpanExporter) => { | ||
_global[OTEL_TESTING_MEMORY_EXPORTER] = memoryExporter; | ||
}; | ||
|
||
export const getTestSpans = (): ReadableSpan[] => { | ||
return getTestMemoryExporter()!.getFinishedSpans(); | ||
}; | ||
|
||
export const resetMemoryExporter = () => { | ||
getTestMemoryExporter()?.reset(); | ||
}; |
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
|
@@ -5,6 +5,6 @@ | |
"outDir": "build" | ||
}, | ||
"include": [ | ||
"*.ts" | ||
"src/**/*.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
Oops, something went wrong.