Skip to content

Commit

Permalink
fixup massageParameters and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
secustor committed Nov 27, 2023
1 parent 9abd6d3 commit c97fa54
Show file tree
Hide file tree
Showing 4 changed files with 24,258 additions and 27,532 deletions.
56 changes: 30 additions & 26 deletions experimental/packages/api-extension/package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
{
"name": "@opentelemetry/api-extension",
"private": false,
"publishConfig": {
"access": "public"
},
"version": "0.41.0",
"description": "api extensions to streamline the user experience",
"main": "build/src/index.js",
"module": "build/esm/index.js",
"esnext": "build/esnext/index.js",
"types": "build/src/index.d.ts",
"main": "build/src/index.js",
"browser": {
"./src/platform/index.ts": "./src/platform/browser/index.ts",
"./build/esm/platform/index.js": "./build/esm/platform/browser/index.js",
"./build/esnext/platform/index.js": "./build/esnext/platform/browser/index.js",
"./build/src/platform/index.js": "./build/src/platform/browser/index.js"
},
"repository": "open-telemetry/opentelemetry-js",
"scripts": {
"prepublishOnly": "npm run compile",
"precompile": "cross-var lerna run version --scope $npm_package_name --include-dependencies",
"compile": "tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"clean": "tsc --build --clean tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts",
"test:browser": "karma start --single-run",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../../",
"codecov:browser": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../../",
"build": "npm run compile",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"tdd": "npm run test -- --watch-extensions ts --watch",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test:browser": "karma start --single-run",
"prewatch": "node ../../../scripts/version-update.js",
"watch": "tsc --build -w tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"peer-api-check": "node ../../../scripts/peer-api-check.js",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../../"
"version": "node ../../../scripts/version-update.js",
"watch": "tsc --build --watch tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"precompile": "cross-var lerna run version --scope $npm_package_name --include-dependencies",
"prewatch": "node ../../../scripts/version-update.js"
},
"keywords": [
"opentelemetry",
"nodejs",
"grpc",
"protobuf",
"otlp",
"browser",
"stats",
"tracing",
"metrics"
"monitoring"
],
"author": "OpenTelemetry Authors",
"license": "Apache-2.0",
Expand All @@ -50,35 +52,37 @@
"build/src/**/*.js",
"build/src/**/*.js.map",
"build/src/**/*.d.ts",
"doc",
"LICENSE",
"README.md"
],
"peerDependencies": {
"@opentelemetry/api": ">=1.3.0 <1.5.0"
"publishConfig": {
"access": "public"
},
"dependencies": {
"@opentelemetry/api": "^1.7.0"
},
"devDependencies": {
"@types/mocha": "10.0.1",
"@types/mocha": "10.0.4",
"@types/node": "20.9.5",
"@types/webpack-env": "1.16.3",
"babel-plugin-istanbul": "6.1.1",
"codecov": "3.8.3",
"cross-var": "1.1.0",
"babel-plugin-istanbul": "6.1.1",
"karma": "6.4.2",
"karma-chrome-launcher": "3.1.0",
"karma-coverage": "2.2.1",
"karma-mocha": "2.0.1",
"karma-spec-reporter": "0.0.36",
"karma-webpack": "4.0.2",
"lerna": "7.1.1",
"lerna": "6.6.2",
"mocha": "10.2.0",
"nyc": "15.1.0",
"ts-loader": "8.4.0",
"ts-mocha": "10.0.0",
"typescript": "4.4.4",
"webpack": "4.46.0"
},
"dependencies": {
"@opentelemetry/api": "^1.0.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/otlp-transformer",
"homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/api-logs",
"sideEffects": false
}
12 changes: 6 additions & 6 deletions experimental/packages/api-extension/src/trace/SugaredTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class SugaredTracer implements Tracer {
): ReturnType<F>;
withActiveSpan<F extends (span: Span) => ReturnType<F>>(
name: string,
arg2: SugaredSpanOptions | F,
arg3?: Context | F,
arg2: F | SugaredSpanOptions,
arg3?: F | Context,
arg4?: F
): ReturnType<F> {
const { opts, ctx, fn } = massageParams(arg2, arg3, arg4);
Expand Down Expand Up @@ -156,17 +156,17 @@ class SugaredTracer implements Tracer {
* @param arg3
*/
function massageParams<F extends (span: Span) => ReturnType<F>>(
arg: SugaredSpanOptions | F,
arg2?: Context | F,
arg: F | SugaredSpanOptions,
arg2?: F | Context,
arg3?: F
) {
let opts: SugaredSpanOptions | undefined;
let ctx: Context | undefined;
let fn: F;

if (arguments.length === 1) {
if (!arg2 && !arg3) {
fn = arg as F;
} else if (arguments.length === 2) {
} else if (!arg3) {
opts = arg as SugaredSpanOptions;
fn = arg2 as F;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 { wrapTracer } from '../../src';
import { NoopTracerProvider } from '@opentelemetry/api/src/trace/NoopTracerProvider';
import * as assert from 'assert';
import { Span } from '@opentelemetry/api/build/src';

describe('SugaredTracer', () => {
const tracerProvider = new NoopTracerProvider();
const tracer = tracerProvider.getTracer();
const sugaredTracer = wrapTracer(tracer);

describe('wrapTracer', () => {
it('should return SugaredTracer', () => {
assert.ok(typeof sugaredTracer === 'object', 'returns object');
assert.ok(
sugaredTracer.constructor.name === 'SugaredTracer',
'returns SugaredTracer'
);
});
});

describe('withActiveSpan', () => {
it('should return result of wrapped function', () => {
const test = function (span: Span) {
return 'test-value';
};
const result = sugaredTracer.withActiveSpan('test', test);

assert.strictEqual(result, 'test-value');
});

it('should rethrow error with default exception handling', () => {
const testError = new Error('test-error');
assert.throws(
sugaredTracer.withActiveSpan('test', span => {
throw testError;
}),
{
message: 'test-error',
}
);
});
});

describe('withSpan', () => {
it('should return result of wrapped function', () => {
const result = sugaredTracer.withSpan('test', span => {
return 'test-value';
});

assert.strictEqual(result, 'test-value');
});
});
});
Loading

0 comments on commit c97fa54

Please sign in to comment.