Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: create an api package
  • Loading branch information
dyladan committed Jan 22, 2020
commit 357267ebc1da2dadc7fe62029d69e66494950dd7
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "@opentelemetry/types",
"name": "@opentelemetry/api",
"version": "0.3.2",
"description": "TypeScript types for OpenTelemetry",
"description": "Public API for OpenTelemetry",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
"repository": "open-telemetry/opentelemetry-js",
"scripts": {
"test": "nyc ts-mocha -p tsconfig.json test/**/*.ts",
"build": "npm run compile",
"check": "gts check",
"precompile": "tsc --version",
Expand Down Expand Up @@ -42,8 +43,12 @@
"access": "public"
},
"devDependencies": {
"@types/mocha": "^5.2.7",
"gts": "^1.1.0",
"linkinator": "^1.5.0",
"linkinator": "^1.5.0",
"mocha": "^6.1.0",
"nyc": "^14.1.1",
"ts-mocha": "^6.0.0",
"tslint-consistent-codestyle": "^1.15.1",
"tslint-microsoft-contrib": "^6.2.0",
"typedoc": "^0.15.0",
Expand Down
41 changes: 41 additions & 0 deletions packages/opentelemetry-api/src/api/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*!
* Copyright 2020, 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 { MeterRegistry } from '../metrics/MeterRegistry';
import { NOOP_METER_REGISTRY } from '../metrics/NoopMeterRegistry';

export class MetricsAPI {
private static _instance?: MetricsAPI;
private _meterRegistry: MeterRegistry = NOOP_METER_REGISTRY;

private constructor() {}

public static getInstance(): MetricsAPI {
if (!this._instance) {
this._instance = new MetricsAPI();
}

return this._instance;
}

public initGlobalMeterRegistry(registry: MeterRegistry) {
this._meterRegistry = registry;
}

public getMeterRegistry(): MeterRegistry {
return this._meterRegistry;
}
}
41 changes: 41 additions & 0 deletions packages/opentelemetry-api/src/api/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*!
* Copyright 2020, 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 { NOOP_TRACER_REGISTRY } from '../trace/NoopTracerRegistry';
import { TracerRegistry } from '../trace/tracer_registry';

export class TracingAPI {
private static _instance?: TracingAPI;
private _tracerRegistry: TracerRegistry = NOOP_TRACER_REGISTRY;

private constructor() {}

public static getInstance(): TracingAPI {
if (!this._instance) {
this._instance = new TracingAPI();
}

return this._instance;
}

public initGlobalTracerRegistry(registry: TracerRegistry) {
this._tracerRegistry = registry;
}

public getTracerRegistry(): TracerRegistry {
return this._tracerRegistry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { SpanContext, BinaryFormat } from '@opentelemetry/types';
import { SpanContext } from '../../trace/span_context';
import { BinaryFormat } from './BinaryFormat';

/**
* No-op implementations of {@link BinaryFormat}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { HttpTextFormat, SpanContext } from '@opentelemetry/types';
import { SpanContext } from '../../trace/span_context';
import { HttpTextFormat } from './HttpTextFormat';

/**
* No-op implementations of {@link HttpTextFormat}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,19 @@ export * from './trace/tracer';
export * from './trace/tracer_registry';
export * from './trace/trace_flags';
export * from './trace/trace_state';
export * from './trace/NoopSpan';
export * from './trace/NoopTracer';
export * from './trace/NoopTracerRegistry';
export * from './metrics/NoopMeterRegistry';
export * from './metrics/NoopMeter';

import { TracingAPI } from './api/tracing';
export const tracing = TracingAPI.getInstance();

import { MetricsAPI } from './api/metrics';
export const metrics = MetricsAPI.getInstance();

export default {
tracing,
metrics,
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,11 @@
* limitations under the License.
*/

import {
BoundCounter,
DistributedContext,
BoundGauge,
Meter,
Metric,
MetricOptions,
MetricUtils,
BoundMeasure,
SpanContext,
LabelSet,
Labels,
} from '@opentelemetry/types';
import { Meter } from './Meter';
import { MetricOptions, Metric, Labels, LabelSet, MetricUtils } from './Metric';
import { BoundMeasure, BoundCounter, BoundGauge } from './BoundInstrument';
import { DistributedContext } from '../distributed_context/DistributedContext';
import { SpanContext } from '../trace/span_context';

/**
* NoopMeter is a noop implementation of the {@link Meter} interface. It reuses constant
Expand Down Expand Up @@ -165,7 +157,7 @@ export class NoopBoundMeasure implements BoundMeasure {
}
}

export const noopMeter = new NoopMeter();
export const NOOP_METER = new NoopMeter();

export const NOOP_BOUND_GAUGE = new NoopBoundGauge();
export const NOOP_GAUGE_METRIC = new NoopGaugeMetric(NOOP_BOUND_GAUGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
* limitations under the License.
*/

import * as types from '@opentelemetry/types';
import { noopMeter } from './NoopMeter';
import { Meter } from './Meter';
import { MeterRegistry } from './MeterRegistry';
import { NOOP_METER } from './NoopMeter';

/**
* An implementation of the {@link MeterRegistry} which returns an impotent Meter
* for all calls to `getMeter`
*/
export class NoopMeterRegistry implements types.MeterRegistry {
getMeter(_name?: string, _version?: string): types.Meter {
return noopMeter;
export class NoopMeterRegistry implements MeterRegistry {
getMeter(_name?: string, _version?: string): Meter {
return NOOP_METER;
}
}

export const NOOP_METER_REGISTRY = new NoopMeterRegistry();
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,33 @@
* limitations under the License.
*/

import * as types from '@opentelemetry/types';
import { INVALID_SPAN_CONTEXT } from '../trace/spancontext-utils';
import { TimeInput } from '../common/Time';
import { Attributes } from './attributes';
import { Span } from './span';
import { SpanContext } from './span_context';
import { Status } from './status';
import { TraceFlags } from './trace_flags';

export const INVALID_TRACE_ID = '0';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it 0 and not empty for example ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the exact example in the old core package. I think because it needs to satisfy the typings of the TraceContext interface.

export const INVALID_SPAN_ID = '0';
const INVALID_SPAN_CONTEXT: SpanContext = {
traceId: INVALID_TRACE_ID,
spanId: INVALID_SPAN_ID,
traceFlags: TraceFlags.UNSAMPLED,
};

/**
* The NoopSpan is the default {@link Span} that is used when no Span
* implementation is available. All operations are no-op including context
* propagation.
*/
export class NoopSpan implements types.Span {
export class NoopSpan implements Span {
constructor(
private readonly _spanContext: types.SpanContext = INVALID_SPAN_CONTEXT
private readonly _spanContext: SpanContext = INVALID_SPAN_CONTEXT
) {}

// Returns a SpanContext.
context(): types.SpanContext {
context(): SpanContext {
return this._spanContext;
}

Expand All @@ -38,22 +50,22 @@ export class NoopSpan implements types.Span {
}

// By default does nothing
setAttributes(attributes: types.Attributes): this {
setAttributes(attributes: Attributes): this {
return this;
}

// By default does nothing
addEvent(name: string, attributes?: types.Attributes): this {
addEvent(name: string, attributes?: Attributes): this {
return this;
}

// By default does nothing
addLink(spanContext: types.SpanContext, attributes?: types.Attributes): this {
addLink(spanContext: SpanContext, attributes?: Attributes): this {
return this;
}

// By default does nothing
setStatus(status: types.Status): this {
setStatus(status: Status): this {
return this;
}

Expand All @@ -63,7 +75,7 @@ export class NoopSpan implements types.Span {
}

// By default does nothing
end(endTime?: types.TimeInput): void {}
end(endTime?: TimeInput): void {}

// isRecording always returns false for noopSpan.
isRecording(): boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2019, OpenTelemetry Authors
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,15 +14,9 @@
* limitations under the License.
*/

import {
Tracer,
SpanOptions,
Span,
HttpTextFormat,
BinaryFormat,
} from '@opentelemetry/types';
import { NOOP_HTTP_TEXT_FORMAT } from '../context/propagation/NoopHttpTextFormat';
import { BinaryFormat, HttpTextFormat, Span, SpanOptions, Tracer } from '..';
import { NOOP_BINARY_FORMAT } from '../context/propagation/NoopBinaryFormat';
import { NOOP_HTTP_TEXT_FORMAT } from '../context/propagation/NoopHttpTextFormat';
import { NOOP_SPAN } from './NoopSpan';

/**
Expand Down Expand Up @@ -60,4 +54,4 @@ export class NoopTracer implements Tracer {
}
}

export const noopTracer = new NoopTracer();
export const NOOP_TRACER = new NoopTracer();
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
* limitations under the License.
*/

import * as types from '@opentelemetry/types';
import { noopTracer } from './NoopTracer';
import { NOOP_TRACER } from './NoopTracer';
import { Tracer } from './tracer';
import { TracerRegistry } from './tracer_registry';

/**
* An implementation of the {@link TracerRegistry} which returns an impotent Tracer
* for all calls to `getTracer`
*/
export class NoopTracerRegistry implements types.TracerRegistry {
getTracer(_name?: string, _version?: string): types.Tracer {
return noopTracer;
export class NoopTracerRegistry implements TracerRegistry {
getTracer(_name?: string, _version?: string): Tracer {
return NOOP_TRACER;
}
}

export const NOOP_TRACER_REGISTRY = new NoopTracerRegistry();
Loading