Skip to content

Commit

Permalink
Add Telemetry metrics framework (#10241)
Browse files Browse the repository at this point in the history
Signed-off-by: Gagan Juneja <gjjuneja@amazon.com>
  • Loading branch information
Gaganjuneja authored Oct 5, 2023
1 parent 3dd9fc1 commit 211578c
Show file tree
Hide file tree
Showing 42 changed files with 1,489 additions and 79 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add instrumentation in Inbound Handler. ([#100143](https://github.com/opensearch-project/OpenSearch/pull/10143))
- Enable remote segment upload backpressure by default ([#10356](https://github.com/opensearch-project/OpenSearch/pull/10356))
- [Remote Store] Add support to reload repository metadata inplace ([#9569](https://github.com/opensearch-project/OpenSearch/pull/9569))
- [Metrics Framework] Add Metrics framework. ([#10241](https://github.com/opensearch-project/OpenSearch/pull/10241))

### Deprecated

Expand All @@ -147,4 +148,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Security

[Unreleased 3.0]: https://github.com/opensearch-project/OpenSearch/compare/2.x...HEAD
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.11...2.x
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.11...2.x
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 org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.telemetry.metrics.tags.Tags;

/**
* Counter adds the value to the existing metric.
* {@opensearch.experimental}
*/
@ExperimentalApi
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 tags attributes/dimensions of the metric.
*/
void add(double value, Tags tags);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.IOException;

/**
* Default implementation for {@link MetricsRegistry}
*/
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 {
metricsTelemetry.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.common.annotation.ExperimentalApi;

import java.io.Closeable;

/**
* MetricsRegistry helps in creating the metric instruments.
* @opensearch.experimental
*/
@ExperimentalApi
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,14 @@

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 MetricsRegistry, Closeable {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.common.annotation.InternalApi;
import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.metrics.tags.Tags;

/**
* No-op {@link Counter}
* {@opensearch.internal}
*/
@InternalApi
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, Tags tags) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.common.annotation.InternalApi;
import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.metrics.MetricsRegistry;

import java.io.IOException;

/**
*No-op {@link MetricsRegistry}
* {@opensearch.internal}
*/
@InternalApi
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,16 @@
/*
* 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
* {@opensearch.internal}
*/
@InternalApi
package org.opensearch.telemetry.metrics.noop;

import org.opensearch.common.annotation.InternalApi;
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.tags;

import org.opensearch.common.annotation.ExperimentalApi;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Class to create tags for a meter.
*
* @opensearch.experimental
*/
@ExperimentalApi
public class Tags {
private final Map<String, Object> tagsMap;
/**
* Empty value.
*/
public final static Tags EMPTY = new Tags(Collections.emptyMap());

/**
* Factory method.
* @return tags.
*/
public static Tags create() {
return new Tags(new HashMap<>());
}

/**
* Constructor.
*/
private Tags(Map<String, Object> tagsMap) {
this.tagsMap = tagsMap;
}

/**
* Add String attribute.
* @param key key
* @param value value
* @return Same instance.
*/
public Tags addTag(String key, String value) {
Objects.requireNonNull(value, "value cannot be null");
tagsMap.put(key, value);
return this;
}

/**
* Add long attribute.
* @param key key
* @param value value
* @return Same instance.
*/
public Tags addTag(String key, long value) {
tagsMap.put(key, value);
return this;
};

/**
* Add double attribute.
* @param key key
* @param value value
* @return Same instance.
*/
public Tags addTag(String key, double value) {
tagsMap.put(key, value);
return this;
};

/**
* Add boolean attribute.
* @param key key
* @param value value
* @return Same instance.
*/
public Tags addTag(String key, boolean value) {
tagsMap.put(key, value);
return this;
};

/**
* Returns the attribute map.
* @return tags map
*/
public Map<String, ?> getTagsMap() {
return Collections.unmodifiableMap(tagsMap);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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
* @opensearch.experimental
*/
@ExperimentalApi
package org.opensearch.telemetry.metrics.tags;

import org.opensearch.common.annotation.ExperimentalApi;
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,4 @@ public interface TracingTelemetry extends Closeable {
*/
TracingContextPropagator getContextPropagator();

/**
* closes the resource
*/
void close();

}
Loading

0 comments on commit 211578c

Please sign in to comment.