Skip to content

Commit

Permalink
Add Telemetry metrics framework
Browse files Browse the repository at this point in the history
Signed-off-by: Gagan Juneja <[email protected]>
  • Loading branch information
Gagan Juneja committed Sep 26, 2023
1 parent a8969cb commit cacdc02
Show file tree
Hide file tree
Showing 37 changed files with 1,301 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics;

import org.opensearch.telemetry.tracing.attributes.Attributes;

/**
* Counter adds the value to the existing metric.
*/
public interface Counter {

/**
* add value.
* @param value value to be added.
*/
void add(double value);

/**
* add value along with the attributes.
* @param value value to be added.
* @param attributes attributes/dimensions of the metric.
*/
void add(double value, Attributes attributes);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics;

import java.io.Closeable;
import java.io.IOException;

/**
* Default implementation for {@link MetricsRegistry}
*/
public class DefaultMetricsRegistry implements MetricsRegistry {
private final MetricsTelemetry metricsTelemetry;

/**
* Constructor
* @param metricsTelemetry metrics telemetry.
*/
public DefaultMetricsRegistry(MetricsTelemetry metricsTelemetry) {
this.metricsTelemetry = metricsTelemetry;
}

@Override
public Counter createCounter(String name, String description, String unit) {
return metricsTelemetry.createCounter(name, description, unit);
}

@Override
public Counter createUpDownCounter(String name, String description, String unit) {
return metricsTelemetry.createUpDownCounter(name, description, unit);
}

@Override
public void close() throws IOException {
((Closeable) metricsTelemetry).close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics;

import java.io.Closeable;

/**
* MetricsRegistry helps in creating the metric instruments.
*/
public interface MetricsRegistry extends Closeable {

/**
* Creates the counter.
* @param name name of the counter.
* @param description any description about the metric.
* @param unit unit of the metric.
* @return counter.
*/
Counter createCounter(String name, String description, String unit);

/**
* Creates the upDown counter.
* @param name name of the upDown counter.
* @param description any description about the metric.
* @param unit unit of the metric.
* @return counter.
*/
Counter createUpDownCounter(String name, String description, String unit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,31 @@

import org.opensearch.common.annotation.ExperimentalApi;

import java.io.Closeable;

/**
* Interface for metrics telemetry providers
*
* @opensearch.experimental
*/
@ExperimentalApi
public interface MetricsTelemetry {
public interface MetricsTelemetry extends Closeable {

/**
* Creates the counter.
* @param name name of the counter.
* @param description any description about the metric.
* @param unit unit of the metric.
* @return counter
*/
Counter createCounter(String name, String description, String unit);

/**
* Creates the upDown counter.
* @param name name of the upDown counter.
* @param description any description about the metric.
* @param unit unit of the metric.
* @return upDownCounter.
*/
Counter createUpDownCounter(String name, String description, String unit);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics.noop;

import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.tracing.attributes.Attributes;

/**
* No-op {@link Counter}
*/
public class NoopCounter implements Counter {

/**
* No-op Counter instance
*/
public final static NoopCounter INSTANCE = new NoopCounter();

private NoopCounter() {}

@Override
public void add(double value) {

}

@Override
public void add(double value, Attributes attributes) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics.noop;

import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.metrics.MetricsRegistry;

import java.io.IOException;

/**
*No-op {@link MetricsRegistry}
*/
public class NoopMetricsRegistry implements MetricsRegistry {

/**
* No-op Meter instance
*/
public final static NoopMetricsRegistry INSTANCE = new NoopMetricsRegistry();

private NoopMetricsRegistry() {}

@Override
public Counter createCounter(String name, String description, String unit) {
return NoopCounter.INSTANCE;
}

@Override
public Counter createUpDownCounter(String name, String description, String unit) {
return NoopCounter.INSTANCE;
}

@Override
public void close() throws IOException {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/**
* Contains metrics related classes
*/
package org.opensearch.telemetry.metrics.noop;
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics;

import org.opensearch.test.OpenSearchTestCase;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class DefaultMetricsRegistryTests extends OpenSearchTestCase {

private MetricsTelemetry metricsTelemetry;
private DefaultMetricsRegistry defaultMeterRegistry;

@Override
public void setUp() throws Exception {
super.setUp();
metricsTelemetry = mock(MetricsTelemetry.class);
defaultMeterRegistry = new DefaultMetricsRegistry(metricsTelemetry);
}

public void testCounter() {
Counter mockCounter = mock(Counter.class);
when(defaultMeterRegistry.createCounter(any(String.class), any(String.class), any(String.class))).thenReturn(mockCounter);
Counter counter = defaultMeterRegistry.createCounter(
"org.opensearch.telemetry.metrics.DefaultMeterRegistryTests.testCounter",
"test counter",
"1"
);
assertSame(mockCounter, counter);
}

public void testUpDownCounter() {
Counter mockCounter = mock(Counter.class);
when(defaultMeterRegistry.createUpDownCounter(any(String.class), any(String.class), any(String.class))).thenReturn(mockCounter);
Counter counter = defaultMeterRegistry.createUpDownCounter(
"org.opensearch.telemetry.metrics.DefaultMeterRegistryTests.testUpDownCounter",
"test up-down counter",
"1"
);
assertSame(mockCounter, counter);
}

}
22 changes: 3 additions & 19 deletions plugins/telemetry-otel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
runtimeOnly "com.squareup.okhttp3:okhttp:4.11.0"
runtimeOnly "com.squareup.okio:okio-jvm:3.5.0"
runtimeOnly "io.opentelemetry:opentelemetry-exporter-sender-okhttp:${versions.opentelemetry}"
api "io.opentelemetry:opentelemetry-extension-incubator:${versions.opentelemetry}-alpha"
testImplementation "io.opentelemetry:opentelemetry-sdk-testing:${versions.opentelemetry}"
}

Expand Down Expand Up @@ -80,29 +81,12 @@ thirdPartyAudit {
'io.opentelemetry.api.events.EventEmitter',
'io.opentelemetry.api.events.EventEmitterBuilder',
'io.opentelemetry.api.events.EventEmitterProvider',
'io.opentelemetry.extension.incubator.metrics.ExtendedDoubleHistogramBuilder',
'io.opentelemetry.extension.incubator.metrics.ExtendedLongHistogramBuilder',
'io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties',
'io.opentelemetry.sdk.autoconfigure.spi.logs.ConfigurableLogRecordExporterProvider',
'io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider',
'io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider',
'io.opentelemetry.extension.incubator.metrics.DoubleCounterAdviceConfigurer',
'io.opentelemetry.extension.incubator.metrics.DoubleGauge',
'io.opentelemetry.extension.incubator.metrics.DoubleGaugeAdviceConfigurer',
'io.opentelemetry.extension.incubator.metrics.DoubleHistogramAdviceConfigurer',
'io.opentelemetry.extension.incubator.metrics.DoubleUpDownCounterAdviceConfigurer',
'io.opentelemetry.extension.incubator.metrics.ExtendedDoubleCounterBuilder',
'io.opentelemetry.extension.incubator.metrics.ExtendedDoubleGaugeBuilder',
'io.opentelemetry.extension.incubator.metrics.ExtendedDoubleUpDownCounterBuilder',
'io.opentelemetry.extension.incubator.metrics.ExtendedLongCounterBuilder',
'io.opentelemetry.extension.incubator.metrics.ExtendedLongGaugeBuilder',
'io.opentelemetry.extension.incubator.metrics.ExtendedLongUpDownCounterBuilder',
'io.opentelemetry.extension.incubator.metrics.LongCounterAdviceConfigurer',
'io.opentelemetry.extension.incubator.metrics.LongGauge',
'io.opentelemetry.extension.incubator.metrics.LongGaugeAdviceConfigurer',
'io.opentelemetry.extension.incubator.metrics.LongHistogramAdviceConfigurer',
'io.opentelemetry.extension.incubator.metrics.LongUpDownCounterAdviceConfigurer',
'kotlin.io.path.PathsKt'
'kotlin.io.path.PathsKt',
'io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider'
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bfcea9bd71f97dd4e8a4f92c15ba5659fb07ff05
Loading

0 comments on commit cacdc02

Please sign in to comment.