Skip to content

Commit

Permalink
feat: add WIP metrics SDK (#2601)
Browse files Browse the repository at this point in the history
Co-authored-by: Georg Pirklbauer <georg.pirklbauer@dynatrace.com>
  • Loading branch information
dyladan and pirgeo authored Nov 15, 2021
1 parent 7d67bf3 commit 6b77b67
Show file tree
Hide file tree
Showing 16 changed files with 548 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
},
{
"path": "../opentelemetry-exporter-trace-otlp-http"
},
{
"path": "../opentelemetry-sdk-metrics-base"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
},
{
"path": "../opentelemetry-exporter-trace-otlp-http/tsconfig.esm.json"
},
{
"path": "../opentelemetry-sdk-metrics-base/tsconfig.esm.json"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
},
{
"path": "../opentelemetry-exporter-trace-otlp-http"
},
{
"path": "../opentelemetry-sdk-metrics-base"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
},
{
"path": "../opentelemetry-exporter-trace-otlp-proto"
},
{
"path": "../opentelemetry-sdk-metrics-base"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"references": [
{
"path": "../opentelemetry-api-metrics"
},
{
"path": "../opentelemetry-sdk-metrics-base"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opentelemetry/sdk-metrics-base-wip",
"version": "0.26.0",
"version": "0.27.0",
"private": true,
"description": "Work in progress OpenTelemetry metrics SDK",
"main": "build/src/index.js",
Expand Down Expand Up @@ -64,7 +64,7 @@
"@opentelemetry/api": "^1.0.0"
},
"dependencies": {
"@opentelemetry/api-metrics": "0.26.0",
"@opentelemetry/api-metrics": "0.27.0",
"@opentelemetry/core": "1.0.0",
"@opentelemetry/resources": "1.0.0",
"lodash.merge": "^4.6.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 { Measurement } from './Measurement';

// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#aggregation

export interface Aggregator {
aggregate(measurement: Measurement): void;
}

// TODO define actual aggregator classes
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 * as api from '@opentelemetry/api';
import * as metrics from '@opentelemetry/api-metrics';
import { Meter } from './Meter';

// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument

export enum InstrumentType {
COUNTER = 'COUNTER',
HISTOGRAM = 'HISTOGRAM',
UP_DOWN_COUNTER = 'UP_DOWN_COUNTER',
OBSERVABLE_COUNTER = 'OBSERVABLE_COUNTER',
OBSERVABLE_GAUGE = 'OBSERVABLE_GAUGE',
OBSERVABLE_UP_DOWN_COUNTER = 'OBSERVABLE_UP_DOWN_COUNTER',
}

export class SyncInstrument {
constructor(private _meter: Meter, private _name: string) { }

getName(): string {
return this._name;
}


aggregate(value: number, attributes: metrics.Attributes = {}, ctx: api.Context = api.context.active()) {
this._meter.aggregate(this, {
value,
attributes,
context: ctx,
});
}
}

export class UpDownCounter extends SyncInstrument implements metrics.Counter {
add(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void {
this.aggregate(value, attributes, ctx);
}
}

export class Counter extends SyncInstrument implements metrics.Counter {
add(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void {
if (value < 0) {
api.diag.warn(`negative value provided to counter ${this.getName()}: ${value}`);
return;
}

this.aggregate(value, attributes, ctx);
}
}

export class Histogram extends SyncInstrument implements metrics.Histogram {
record(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void {
this.aggregate(value, attributes, ctx);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 * as api from '@opentelemetry/api'
import { Attributes } from '@opentelemetry/api-metrics'

// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#measurement

export type Measurement = {
value: number;
// TODO use common attributes
attributes: Attributes
context?: api.Context;
}
65 changes: 65 additions & 0 deletions experimental/packages/opentelemetry-sdk-metrics-base/src/Meter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 * as metrics from '@opentelemetry/api-metrics';
import { InstrumentationLibrary } from '@opentelemetry/core';
import { Counter, Histogram, UpDownCounter } from './Instruments';
import { Measurement } from './Measurement';
import { MeterProvider } from './MeterProvider';

// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#meter

export class Meter implements metrics.Meter {
// instrumentation library required by spec to be on meter
// spec requires provider config changes to apply to previously created meters, achieved by holding a reference to the provider
constructor(private _provider: MeterProvider, private _instrumentationLibrary: InstrumentationLibrary, private _schemaUrl?: string) { }

/** this exists just to prevent ts errors from unused variables and may be removed */
getSchemaUrl(): string | undefined {
return this._schemaUrl;
}

/** this exists just to prevent ts errors from unused variables and may be removed */
getInstrumentationLibrary(): InstrumentationLibrary {
return this._instrumentationLibrary;
}

createHistogram(_name: string, _options?: metrics.MetricOptions): Histogram {
return new Histogram(this, _name);
}

createCounter(_name: string, _options?: metrics.MetricOptions): metrics.Counter {
return new Counter(this, _name);
}

createUpDownCounter(_name: string, _options?: metrics.MetricOptions): metrics.UpDownCounter {
return new UpDownCounter(this, _name);
}

createObservableGauge(_name: string, _options?: metrics.MetricOptions, _callback?: (observableResult: metrics.ObservableResult) => void): metrics.ObservableBase {
throw new Error('Method not implemented.');
}
createObservableCounter(_name: string, _options?: metrics.MetricOptions, _callback?: (observableResult: metrics.ObservableResult) => void): metrics.ObservableBase {
throw new Error('Method not implemented.');
}
createObservableUpDownCounter(_name: string, _options?: metrics.MetricOptions, _callback?: (observableResult: metrics.ObservableResult) => void): metrics.ObservableBase {
throw new Error('Method not implemented.');
}

public aggregate(metric: unknown, measurement: Measurement) {
this._provider.aggregate(this, metric, measurement);
}
}
Loading

0 comments on commit 6b77b67

Please sign in to comment.