Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add WIP metrics SDK #2601

Merged
merged 15 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
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
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,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 * as api from '@opentelemetry/api';
import * as metrics from '@opentelemetry/api-metrics';
import { Meter } from './Meter';

export enum InstrumentType {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
COUNTER = 'COUNTER',
HISTOGRAM = 'HISTOGRAM',
UP_DOWN_COUNTER = 'UP_DOWN_COUNTER',
OBSERVABLE_COUNTER = 'OBSERVABLE_COUNTER',
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
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,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 * as api from '@opentelemetry/api'
import { Attributes } from '@opentelemetry/api-metrics'

export type Measurement = {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
value: number;
// TODO use common attributes
attributes: Attributes
context?: api.Context;
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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';

export class Meter implements metrics.Meter {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
// 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