From a8083e84b23227828745da80fd5fe512357dd34b Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 30 Aug 2021 13:26:37 -0400 Subject: [PATCH] fix: remove all circular dependencies (#119) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: remove all circular dependencies * chore(deps): pin dpdm Co-authored-by: Gerhard Stöbich * chore: better name for dpdm script Co-authored-by: Gerhard Stöbich --- .github/workflows/lint.yaml | 7 +++++-- package.json | 2 ++ src/baggage/utils.ts | 4 +++- src/index.ts | 9 ++++++--- src/internal/global-utils.ts | 5 ++--- src/trace/NonRecordingSpan.ts | 2 +- src/trace/NoopTracer.ts | 4 +++- src/trace/ProxyTracer.ts | 7 +++++-- src/trace/invalid-span-constants.ts | 26 ++++++++++++++++++++++++++ src/trace/spancontext-utils.ts | 9 +-------- test/trace/spancontext-utils.test.ts | 10 +++++----- 11 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 src/trace/invalid-span-constants.ts diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 7e47e051..6c92af18 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -27,11 +27,14 @@ jobs: - name: Install dependencies run: npm install + - name: Build 🔧 + run: npm run compile + - name: Lint run: npm run lint - - name: Build 🔧 - run: npm run compile + - name: Check for Circular Dependencies + run: npm run cycle-check - name: Generate Documentation 📜 run: npm run docs diff --git a/package.json b/package.json index eab82db2..d8432156 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "lint": "eslint src test --ext .ts", "test:browser": "nyc karma start --single-run", "test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts", + "cycle-check": "dpdm --exit-code circular:1 src/index.ts", "version": "node scripts/version-update.js", "prewatch": "npm run version", "watch": "tsc --build --watch" @@ -63,6 +64,7 @@ "@typescript-eslint/eslint-plugin": "4.28.1", "@typescript-eslint/parser": "4.28.1", "codecov": "3.8.2", + "dpdm": "3.7.1", "eslint": "7.30.0", "eslint-plugin-header": "3.1.1", "eslint-plugin-import": "2.23.4", diff --git a/src/baggage/utils.ts b/src/baggage/utils.ts index 2b378b7a..80b87890 100644 --- a/src/baggage/utils.ts +++ b/src/baggage/utils.ts @@ -14,11 +14,13 @@ * limitations under the License. */ -import { diag } from '..'; +import { DiagAPI } from '../api/diag'; import { BaggageImpl } from './internal/baggage-impl'; import { baggageEntryMetadataSymbol } from './internal/symbol'; import { Baggage, BaggageEntry, BaggageEntryMetadata } from './types'; +const diag = DiagAPI.instance(); + /** * Create a new Baggage with optional entries * diff --git a/src/index.ts b/src/index.ts index 78c26a80..f8961931 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,14 +37,17 @@ export * from './trace/tracer_provider'; export * from './trace/tracer'; export { - INVALID_SPANID, - INVALID_TRACEID, - INVALID_SPAN_CONTEXT, isSpanContextValid, isValidTraceId, isValidSpanId, } from './trace/spancontext-utils'; +export { + INVALID_SPANID, + INVALID_TRACEID, + INVALID_SPAN_CONTEXT, +} from './trace/invalid-span-constants'; + export * from './context/context'; export * from './context/types'; diff --git a/src/internal/global-utils.ts b/src/internal/global-utils.ts index 7f0d2d7a..5306aa50 100644 --- a/src/internal/global-utils.ts +++ b/src/internal/global-utils.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import type { DiagAPI } from '../api/diag'; import { ContextManager } from '../context/types'; import { DiagLogger } from '../diag'; import { _globalThis } from '../platform'; @@ -33,7 +32,7 @@ const _global = _globalThis as OTelGlobal; export function registerGlobal( type: Type, instance: OTelGlobalAPI[Type], - diag: DiagAPI, + diag: DiagLogger, allowOverride = false ): boolean { const api = (_global[GLOBAL_OPENTELEMETRY_API_KEY] = _global[ @@ -78,7 +77,7 @@ export function getGlobal( return _global[GLOBAL_OPENTELEMETRY_API_KEY]?.[type]; } -export function unregisterGlobal(type: keyof OTelGlobalAPI, diag: DiagAPI) { +export function unregisterGlobal(type: keyof OTelGlobalAPI, diag: DiagLogger) { diag.debug( `@opentelemetry/api: Unregistering a global for ${type} v${VERSION}.` ); diff --git a/src/trace/NonRecordingSpan.ts b/src/trace/NonRecordingSpan.ts index bd5e321e..a9e5bcaf 100644 --- a/src/trace/NonRecordingSpan.ts +++ b/src/trace/NonRecordingSpan.ts @@ -17,10 +17,10 @@ import { Exception } from '../common/Exception'; import { TimeInput } from '../common/Time'; import { SpanAttributes } from './attributes'; +import { INVALID_SPAN_CONTEXT } from './invalid-span-constants'; import { Span } from './span'; import { SpanContext } from './span_context'; import { SpanStatus } from './status'; -import { INVALID_SPAN_CONTEXT } from './spancontext-utils'; /** * The NonRecordingSpan is the default {@link Span} that is used when no Span diff --git a/src/trace/NoopTracer.ts b/src/trace/NoopTracer.ts index 238553e6..d1db93e9 100644 --- a/src/trace/NoopTracer.ts +++ b/src/trace/NoopTracer.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { context } from '../'; +import { ContextAPI } from '../api/context'; import { Context } from '../context/types'; import { getSpanContext, setSpan } from '../trace/context-utils'; import { NonRecordingSpan } from './NonRecordingSpan'; @@ -24,6 +24,8 @@ import { SpanOptions } from './SpanOptions'; import { SpanContext } from './span_context'; import { Tracer } from './tracer'; +const context = ContextAPI.getInstance(); + /** * No-op implementations of {@link Tracer}. */ diff --git a/src/trace/ProxyTracer.ts b/src/trace/ProxyTracer.ts index 1ee97d8a..758bf204 100644 --- a/src/trace/ProxyTracer.ts +++ b/src/trace/ProxyTracer.ts @@ -16,7 +16,6 @@ import { Context } from '../context/types'; import { NoopTracer } from './NoopTracer'; -import { ProxyTracerProvider } from './ProxyTracerProvider'; import { Span } from './span'; import { SpanOptions } from './SpanOptions'; import { Tracer } from './tracer'; @@ -31,7 +30,7 @@ export class ProxyTracer implements Tracer { private _delegate?: Tracer; constructor( - private _provider: ProxyTracerProvider, + private _provider: TracerDelegator, public readonly name: string, public readonly version?: string ) {} @@ -69,3 +68,7 @@ export class ProxyTracer implements Tracer { return this._delegate; } } + +export interface TracerDelegator { + getDelegateTracer(name: string, version?: string): Tracer | undefined; +} diff --git a/src/trace/invalid-span-constants.ts b/src/trace/invalid-span-constants.ts new file mode 100644 index 00000000..882b4fe5 --- /dev/null +++ b/src/trace/invalid-span-constants.ts @@ -0,0 +1,26 @@ +/* + * 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 { SpanContext } from './span_context'; +import { TraceFlags } from './trace_flags'; + +export const INVALID_SPANID = '0000000000000000'; +export const INVALID_TRACEID = '00000000000000000000000000000000'; +export const INVALID_SPAN_CONTEXT: SpanContext = { + traceId: INVALID_TRACEID, + spanId: INVALID_SPANID, + traceFlags: TraceFlags.NONE, +}; diff --git a/src/trace/spancontext-utils.ts b/src/trace/spancontext-utils.ts index 266471fd..6bd54903 100644 --- a/src/trace/spancontext-utils.ts +++ b/src/trace/spancontext-utils.ts @@ -13,20 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { INVALID_SPANID, INVALID_TRACEID } from './invalid-span-constants'; import { NonRecordingSpan } from './NonRecordingSpan'; import { Span } from './span'; import { SpanContext } from './span_context'; -import { TraceFlags } from './trace_flags'; const VALID_TRACEID_REGEX = /^([0-9a-f]{32})$/i; const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i; -export const INVALID_SPANID = '0000000000000000'; -export const INVALID_TRACEID = '00000000000000000000000000000000'; -export const INVALID_SPAN_CONTEXT: SpanContext = { - traceId: INVALID_TRACEID, - spanId: INVALID_SPANID, - traceFlags: TraceFlags.NONE, -}; export function isValidTraceId(traceId: string): boolean { return VALID_TRACEID_REGEX.test(traceId) && traceId !== INVALID_TRACEID; diff --git a/test/trace/spancontext-utils.test.ts b/test/trace/spancontext-utils.test.ts index 81dd3b1f..09f5712b 100644 --- a/test/trace/spancontext-utils.test.ts +++ b/test/trace/spancontext-utils.test.ts @@ -16,7 +16,7 @@ import * as assert from 'assert'; import * as context from '../../src/trace/spancontext-utils'; -import { TraceFlags } from '../../src'; +import { INVALID_SPANID, INVALID_TRACEID, TraceFlags } from '../../src'; describe('spancontext-utils', () => { it('should return true for valid spancontext', () => { @@ -30,7 +30,7 @@ describe('spancontext-utils', () => { it('should return false when traceId is invalid', () => { const spanContext = { - traceId: context.INVALID_TRACEID, + traceId: INVALID_TRACEID, spanId: '6e0c63257de34c92', traceFlags: TraceFlags.NONE, }; @@ -40,7 +40,7 @@ describe('spancontext-utils', () => { it('should return false when spanId is invalid', () => { const spanContext = { traceId: 'd4cda95b652f4a1592b449d5929fda1b', - spanId: context.INVALID_SPANID, + spanId: INVALID_SPANID, traceFlags: TraceFlags.NONE, }; assert.ok(!context.isSpanContextValid(spanContext)); @@ -48,8 +48,8 @@ describe('spancontext-utils', () => { it('should return false when traceId & spanId is invalid', () => { const spanContext = { - traceId: context.INVALID_TRACEID, - spanId: context.INVALID_SPANID, + traceId: INVALID_TRACEID, + spanId: INVALID_SPANID, traceFlags: TraceFlags.NONE, }; assert.ok(!context.isSpanContextValid(spanContext));