From 48e3faac96c8d6d05e526d34ab1d944f091838a2 Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Thu, 7 Nov 2019 13:32:35 -0800 Subject: [PATCH 1/8] feat: direct calling of metric instruments --- packages/opentelemetry-metrics/src/LabelSet.ts | 9 --------- packages/opentelemetry-metrics/src/Metric.ts | 18 ++++++++++++++++++ .../opentelemetry-metrics/test/Meter.test.ts | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/packages/opentelemetry-metrics/src/LabelSet.ts b/packages/opentelemetry-metrics/src/LabelSet.ts index c17534c2e7..fb038384bf 100644 --- a/packages/opentelemetry-metrics/src/LabelSet.ts +++ b/packages/opentelemetry-metrics/src/LabelSet.ts @@ -28,12 +28,3 @@ export class LabelSet implements types.LabelSet { this.labels = labels; } } - -/** - * Type guard to remove nulls from arrays - * - * @param value value to be checked for null equality - */ -export function notNull(value: T | null): value is T { - return value !== null; -} diff --git a/packages/opentelemetry-metrics/src/Metric.ts b/packages/opentelemetry-metrics/src/Metric.ts index f3b50f2884..28e1eaf7f1 100644 --- a/packages/opentelemetry-metrics/src/Metric.ts +++ b/packages/opentelemetry-metrics/src/Metric.ts @@ -145,6 +145,15 @@ export class CounterMetric extends Metric { this._onUpdate ); } + + /** + * Adds the given value to the current value. Values cannot be negative. + * @param value the value to add. + * @param labelSet the canonicalized LabelSet used to associate with this metric's handle. + */ + add(value: number, labelSet: types.LabelSet) { + this.getHandle(labelSet).add(value); + } } /** This is a SDK implementation of Gauge Metric. */ @@ -172,4 +181,13 @@ export class GaugeMetric extends Metric { this._onUpdate ); } + + /** + * Sets the given value. Values can be negative. + * @param value the new value. + * @param labelSet the canonicalized LabelSet used to associate with this metric's handle. + */ + set(value: number, labelSet: types.LabelSet) { + this.getHandle(labelSet).set(value); + } } diff --git a/packages/opentelemetry-metrics/test/Meter.test.ts b/packages/opentelemetry-metrics/test/Meter.test.ts index 06fd599abc..3e678d7ce4 100644 --- a/packages/opentelemetry-metrics/test/Meter.test.ts +++ b/packages/opentelemetry-metrics/test/Meter.test.ts @@ -76,6 +76,14 @@ describe('Meter', () => { assert.ok(counter instanceof Metric); }); + it('should be able to call add() directly on counter', () => { + const counter = meter.createCounter('name') as CounterMetric; + counter.add(10, labelSet); + assert.strictEqual(counter.getHandle(labelSet)['_data'], 10); + counter.add(10, labelSet); + assert.strictEqual(counter.getHandle(labelSet)['_data'], 20); + }); + describe('.getHandle()', () => { it('should create a counter handle', () => { const counter = meter.createCounter('name') as CounterMetric; @@ -229,6 +237,14 @@ describe('Meter', () => { assert.ok(gauge instanceof Metric); }); + it('should be able to call set() directly on gauge', () => { + const gauge = meter.createGauge('name') as GaugeMetric; + gauge.set(10, labelSet); + assert.strictEqual(gauge.getHandle(labelSet)['_data'], 10); + gauge.set(250, labelSet); + assert.strictEqual(gauge.getHandle(labelSet)['_data'], 250); + }); + describe('.getHandle()', () => { it('should create a gauge handle', () => { const gauge = meter.createGauge('name') as GaugeMetric; From 1758fd05c7bb575793438654613015983ff5b748 Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Fri, 8 Nov 2019 14:31:49 -0800 Subject: [PATCH 2/8] fix: add interface in types->metric --- .../src/metrics/NoopMeter.ts | 31 ++++++++++++++----- packages/opentelemetry-metrics/src/Metric.ts | 6 ++-- .../opentelemetry-types/src/metrics/Metric.ts | 17 ++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/packages/opentelemetry-core/src/metrics/NoopMeter.ts b/packages/opentelemetry-core/src/metrics/NoopMeter.ts index 9f354cbaa0..415ba3aabe 100644 --- a/packages/opentelemetry-core/src/metrics/NoopMeter.ts +++ b/packages/opentelemetry-core/src/metrics/NoopMeter.ts @@ -21,6 +21,7 @@ import { Meter, Metric, MetricOptions, + MetricUtils, MeasureHandle, SpanContext, LabelSet, @@ -110,6 +111,26 @@ export class NoopMetric implements Metric { } } +export class NoopCounterMetric extends NoopMetric + implements Pick { + add(value: number, labelSet: LabelSet) { + this.getHandle(labelSet).add(value); + } +} + +export class NoopGaugeMetric extends NoopMetric + implements Pick { + set(value: number, labelSet: LabelSet) { + this.getHandle(labelSet).set(value); + } +} + +export class NoopMeasureMetric extends NoopMetric { + record(value: number, labelSet: LabelSet) { + this.getHandle(labelSet).record(value); + } +} + export class NoopCounterHandle implements CounterHandle { add(value: number): void { return; @@ -133,16 +154,12 @@ export class NoopMeasureHandle implements MeasureHandle { } export const NOOP_GAUGE_HANDLE = new NoopGaugeHandle(); -export const NOOP_GAUGE_METRIC = new NoopMetric(NOOP_GAUGE_HANDLE); +export const NOOP_GAUGE_METRIC = new NoopGaugeMetric(NOOP_GAUGE_HANDLE); export const NOOP_COUNTER_HANDLE = new NoopCounterHandle(); -export const NOOP_COUNTER_METRIC = new NoopMetric( - NOOP_COUNTER_HANDLE -); +export const NOOP_COUNTER_METRIC = new NoopCounterMetric(NOOP_COUNTER_HANDLE); export const NOOP_MEASURE_HANDLE = new NoopMeasureHandle(); -export const NOOP_MEASURE_METRIC = new NoopMetric( - NOOP_MEASURE_HANDLE -); +export const NOOP_MEASURE_METRIC = new NoopMeasureMetric(NOOP_MEASURE_HANDLE); export const NOOP_LABEL_SET = {} as LabelSet; diff --git a/packages/opentelemetry-metrics/src/Metric.ts b/packages/opentelemetry-metrics/src/Metric.ts index 28e1eaf7f1..e6bc268a80 100644 --- a/packages/opentelemetry-metrics/src/Metric.ts +++ b/packages/opentelemetry-metrics/src/Metric.ts @@ -121,7 +121,8 @@ export abstract class Metric implements types.Metric { } /** This is a SDK implementation of Counter Metric. */ -export class CounterMetric extends Metric { +export class CounterMetric extends Metric + implements Pick { constructor( name: string, options: MetricOptions, @@ -157,7 +158,8 @@ export class CounterMetric extends Metric { } /** This is a SDK implementation of Gauge Metric. */ -export class GaugeMetric extends Metric { +export class GaugeMetric extends Metric + implements Pick { constructor( name: string, options: MetricOptions, diff --git a/packages/opentelemetry-types/src/metrics/Metric.ts b/packages/opentelemetry-types/src/metrics/Metric.ts index 0c011d3811..9106b71585 100644 --- a/packages/opentelemetry-types/src/metrics/Metric.ts +++ b/packages/opentelemetry-types/src/metrics/Metric.ts @@ -99,6 +99,23 @@ export interface Metric { setCallback(fn: () => void): void; } +export interface MetricUtils { + /** + * Adds the given value to the current value. Values cannot be negative. + */ + add(value: number, labelSet: LabelSet): void + /** + * Sets the given value. Values can be negative. + */; + + set(value: number, labelSet: LabelSet): void + /** + * Records the given value to this measure. + */; + + record(value: number, labelSet: LabelSet): void; +} + /** * key-value pairs passed by the user. */ From ced8cdf1ef27b711680640768a5ff691c9430b13 Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Fri, 8 Nov 2019 14:46:14 -0800 Subject: [PATCH 3/8] fix: linting --- packages/opentelemetry-types/src/metrics/Metric.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/opentelemetry-types/src/metrics/Metric.ts b/packages/opentelemetry-types/src/metrics/Metric.ts index 9106b71585..bfa3833296 100644 --- a/packages/opentelemetry-types/src/metrics/Metric.ts +++ b/packages/opentelemetry-types/src/metrics/Metric.ts @@ -103,15 +103,21 @@ export interface MetricUtils { /** * Adds the given value to the current value. Values cannot be negative. */ - add(value: number, labelSet: LabelSet): void + add( + value: number, + labelSet: LabelSet + ): void; /** * Sets the given value. Values can be negative. - */; + */ - set(value: number, labelSet: LabelSet): void + set( + value: number, + labelSet: LabelSet + ): void; /** * Records the given value to this measure. - */; + */ record(value: number, labelSet: LabelSet): void; } From 48349c6b5abf41424daf07670c649d1ad7320323 Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Fri, 8 Nov 2019 14:59:52 -0800 Subject: [PATCH 4/8] fix: linting & docs --- packages/opentelemetry-types/src/metrics/Metric.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/opentelemetry-types/src/metrics/Metric.ts b/packages/opentelemetry-types/src/metrics/Metric.ts index bfa3833296..d54e49178b 100644 --- a/packages/opentelemetry-types/src/metrics/Metric.ts +++ b/packages/opentelemetry-types/src/metrics/Metric.ts @@ -103,18 +103,12 @@ export interface MetricUtils { /** * Adds the given value to the current value. Values cannot be negative. */ - add( - value: number, - labelSet: LabelSet - ): void; + add(value: number, labelSet: LabelSet): void; /** * Sets the given value. Values can be negative. */ - set( - value: number, - labelSet: LabelSet - ): void; + set(value: number, labelSet: LabelSet): void; /** * Records the given value to this measure. */ From 5442f7da7ed3c86e16e56f2fe996935b9d12df21 Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Fri, 8 Nov 2019 16:44:55 -0800 Subject: [PATCH 5/8] fix: add overloads for record in measure metric --- .../src/metrics/NoopMeter.ts | 18 ++++++++++++++--- .../opentelemetry-types/src/metrics/Metric.ts | 20 +++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/packages/opentelemetry-core/src/metrics/NoopMeter.ts b/packages/opentelemetry-core/src/metrics/NoopMeter.ts index 415ba3aabe..6f2cb99507 100644 --- a/packages/opentelemetry-core/src/metrics/NoopMeter.ts +++ b/packages/opentelemetry-core/src/metrics/NoopMeter.ts @@ -125,9 +125,21 @@ export class NoopGaugeMetric extends NoopMetric } } -export class NoopMeasureMetric extends NoopMetric { - record(value: number, labelSet: LabelSet) { - this.getHandle(labelSet).record(value); +export class NoopMeasureMetric extends NoopMetric + implements Pick { + record( + value: number, + labelSet: LabelSet, + distContext?: DistributedContext, + spanContext?: SpanContext + ) { + if (typeof distContext === 'undefined') { + this.getHandle(labelSet).record(value); + } else if (typeof spanContext === 'undefined') { + this.getHandle(labelSet).record(value, distContext); + } else { + this.getHandle(labelSet).record(value, distContext, spanContext); + } } } diff --git a/packages/opentelemetry-types/src/metrics/Metric.ts b/packages/opentelemetry-types/src/metrics/Metric.ts index d54e49178b..a1e18e4fad 100644 --- a/packages/opentelemetry-types/src/metrics/Metric.ts +++ b/packages/opentelemetry-types/src/metrics/Metric.ts @@ -14,6 +14,9 @@ * limitations under the License. */ +import { DistributedContext } from '../distributed_context/DistributedContext'; +import { SpanContext } from '../trace/span_context'; + /** * Options needed for metric creation */ @@ -104,16 +107,29 @@ export interface MetricUtils { * Adds the given value to the current value. Values cannot be negative. */ add(value: number, labelSet: LabelSet): void; + /** * Sets the given value. Values can be negative. */ - set(value: number, labelSet: LabelSet): void; + /** * Records the given value to this measure. */ - record(value: number, labelSet: LabelSet): void; + + record( + value: number, + labelSet: LabelSet, + distContext: DistributedContext + ): void; + + record( + value: number, + labelSet: LabelSet, + distContext: DistributedContext, + spanContext: SpanContext + ): void; } /** From c6ccc14526e453292abd957d82020ed6a57583f0 Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Mon, 11 Nov 2019 10:04:27 -0800 Subject: [PATCH 6/8] fix: linting --- packages/opentelemetry-plugin-document-load/src/documentLoad.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-plugin-document-load/src/documentLoad.ts b/packages/opentelemetry-plugin-document-load/src/documentLoad.ts index 9770decd46..d3b4f68166 100644 --- a/packages/opentelemetry-plugin-document-load/src/documentLoad.ts +++ b/packages/opentelemetry-plugin-document-load/src/documentLoad.ts @@ -186,7 +186,7 @@ export class DocumentLoad extends BasePlugin { }); } else { // // fallback to previous version - const perf: (typeof otperformance) & PerformanceLegacy = otperformance; + const perf: typeof otperformance & PerformanceLegacy = otperformance; const performanceTiming = perf.timing; if (performanceTiming) { const keys = Object.values(PTN); From 1445c074baa7a600efb6a34c8e71755a5f42d2bb Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Mon, 11 Nov 2019 10:52:30 -0800 Subject: [PATCH 7/8] fix: linting --- packages/opentelemetry-plugin-grpc/src/grpc.ts | 9 +++------ .../opentelemetry-plugin-pg/src/pg.ts | 7 +++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/opentelemetry-plugin-grpc/src/grpc.ts b/packages/opentelemetry-plugin-grpc/src/grpc.ts index 9c44c65abd..531bd80bbf 100644 --- a/packages/opentelemetry-plugin-grpc/src/grpc.ts +++ b/packages/opentelemetry-plugin-grpc/src/grpc.ts @@ -354,12 +354,9 @@ export class GrpcPlugin extends BasePlugin { parent: currentSpan || undefined, }) .setAttribute(AttributeNames.COMPONENT, GrpcPlugin.component); - return plugin._makeGrpcClientRemoteCall( - original, - args, - this, - plugin - )(span); + return plugin._makeGrpcClientRemoteCall(original, args, this, plugin)( + span + ); }; }; } diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts index a6841ae33a..e41ceee08a 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts @@ -97,10 +97,9 @@ export class PostgresPlugin extends BasePlugin { const parentSpan = plugin._tracer.getCurrentSpan(); if (typeof args[args.length - 1] === 'function') { // Patch ParameterQuery callback - args[args.length - 1] = utils.patchCallback( - span, - args[args.length - 1] as PostgresCallback - ); + args[args.length - 1] = utils.patchCallback(span, args[ + args.length - 1 + ] as PostgresCallback); // If a parent span exists, bind the callback if (parentSpan) { args[args.length - 1] = plugin._tracer.bind( From 9a1996419d5eae1f3e67945c0b299637e7adfb7d Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Tue, 12 Nov 2019 10:19:50 -0800 Subject: [PATCH 8/8] fix: linting --- packages/opentelemetry-plugin-grpc/src/grpc.ts | 9 ++++++--- .../opentelemetry-plugin-pg/src/pg.ts | 7 ++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/opentelemetry-plugin-grpc/src/grpc.ts b/packages/opentelemetry-plugin-grpc/src/grpc.ts index 531bd80bbf..9c44c65abd 100644 --- a/packages/opentelemetry-plugin-grpc/src/grpc.ts +++ b/packages/opentelemetry-plugin-grpc/src/grpc.ts @@ -354,9 +354,12 @@ export class GrpcPlugin extends BasePlugin { parent: currentSpan || undefined, }) .setAttribute(AttributeNames.COMPONENT, GrpcPlugin.component); - return plugin._makeGrpcClientRemoteCall(original, args, this, plugin)( - span - ); + return plugin._makeGrpcClientRemoteCall( + original, + args, + this, + plugin + )(span); }; }; } diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts index e41ceee08a..a6841ae33a 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/src/pg.ts @@ -97,9 +97,10 @@ export class PostgresPlugin extends BasePlugin { const parentSpan = plugin._tracer.getCurrentSpan(); if (typeof args[args.length - 1] === 'function') { // Patch ParameterQuery callback - args[args.length - 1] = utils.patchCallback(span, args[ - args.length - 1 - ] as PostgresCallback); + args[args.length - 1] = utils.patchCallback( + span, + args[args.length - 1] as PostgresCallback + ); // If a parent span exists, bind the callback if (parentSpan) { args[args.length - 1] = plugin._tracer.bind(