Skip to content

Commit 92a261b

Browse files
authored
[Backport 2.x] - Add Telemetry metrics framework (#10393)
Signed-off-by: Gagan Juneja <gjjuneja@amazon.com>
1 parent 78c55b7 commit 92a261b

File tree

42 files changed

+1488
-78
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1488
-78
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
4747
- Pass parent filter to inner query in nested query ([#10246](https://github.com/opensearch-project/OpenSearch/pull/10246))
4848
- Disable concurrent segment search when terminate_after is used ([#10200](https://github.com/opensearch-project/OpenSearch/pull/10200))
4949
- Enable remote segment upload backpressure by default ([#10356](https://github.com/opensearch-project/OpenSearch/pull/10356))
50+
- [Metrics Framework] Add Metrics framework. ([#10241](https://github.com/opensearch-project/OpenSearch/pull/10241))
5051

5152
### Deprecated
5253

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.telemetry.metrics;
10+
11+
import org.opensearch.common.annotation.ExperimentalApi;
12+
import org.opensearch.telemetry.metrics.tags.Tags;
13+
14+
/**
15+
* Counter adds the value to the existing metric.
16+
* {@opensearch.experimental}
17+
*/
18+
@ExperimentalApi
19+
public interface Counter {
20+
21+
/**
22+
* add value.
23+
* @param value value to be added.
24+
*/
25+
void add(double value);
26+
27+
/**
28+
* add value along with the attributes.
29+
*
30+
* @param value value to be added.
31+
* @param tags attributes/dimensions of the metric.
32+
*/
33+
void add(double value, Tags tags);
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.telemetry.metrics;
10+
11+
import java.io.IOException;
12+
13+
/**
14+
* Default implementation for {@link MetricsRegistry}
15+
*/
16+
class DefaultMetricsRegistry implements MetricsRegistry {
17+
private final MetricsTelemetry metricsTelemetry;
18+
19+
/**
20+
* Constructor
21+
* @param metricsTelemetry metrics telemetry.
22+
*/
23+
public DefaultMetricsRegistry(MetricsTelemetry metricsTelemetry) {
24+
this.metricsTelemetry = metricsTelemetry;
25+
}
26+
27+
@Override
28+
public Counter createCounter(String name, String description, String unit) {
29+
return metricsTelemetry.createCounter(name, description, unit);
30+
}
31+
32+
@Override
33+
public Counter createUpDownCounter(String name, String description, String unit) {
34+
return metricsTelemetry.createUpDownCounter(name, description, unit);
35+
}
36+
37+
@Override
38+
public void close() throws IOException {
39+
metricsTelemetry.close();
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.telemetry.metrics;
10+
11+
import org.opensearch.common.annotation.ExperimentalApi;
12+
13+
import java.io.Closeable;
14+
15+
/**
16+
* MetricsRegistry helps in creating the metric instruments.
17+
* @opensearch.experimental
18+
*/
19+
@ExperimentalApi
20+
public interface MetricsRegistry extends Closeable {
21+
22+
/**
23+
* Creates the counter.
24+
* @param name name of the counter.
25+
* @param description any description about the metric.
26+
* @param unit unit of the metric.
27+
* @return counter.
28+
*/
29+
Counter createCounter(String name, String description, String unit);
30+
31+
/**
32+
* Creates the upDown counter.
33+
* @param name name of the upDown counter.
34+
* @param description any description about the metric.
35+
* @param unit unit of the metric.
36+
* @return counter.
37+
*/
38+
Counter createUpDownCounter(String name, String description, String unit);
39+
}

libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/MetricsTelemetry.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
import org.opensearch.common.annotation.ExperimentalApi;
1212

13+
import java.io.Closeable;
14+
1315
/**
1416
* Interface for metrics telemetry providers
1517
*
1618
* @opensearch.experimental
1719
*/
1820
@ExperimentalApi
19-
public interface MetricsTelemetry {
21+
public interface MetricsTelemetry extends MetricsRegistry, Closeable {
2022

2123
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.telemetry.metrics.noop;
10+
11+
import org.opensearch.common.annotation.InternalApi;
12+
import org.opensearch.telemetry.metrics.Counter;
13+
import org.opensearch.telemetry.metrics.tags.Tags;
14+
15+
/**
16+
* No-op {@link Counter}
17+
* {@opensearch.internal}
18+
*/
19+
@InternalApi
20+
public class NoopCounter implements Counter {
21+
22+
/**
23+
* No-op Counter instance
24+
*/
25+
public final static NoopCounter INSTANCE = new NoopCounter();
26+
27+
private NoopCounter() {}
28+
29+
@Override
30+
public void add(double value) {
31+
32+
}
33+
34+
@Override
35+
public void add(double value, Tags tags) {
36+
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.telemetry.metrics.noop;
10+
11+
import org.opensearch.common.annotation.InternalApi;
12+
import org.opensearch.telemetry.metrics.Counter;
13+
import org.opensearch.telemetry.metrics.MetricsRegistry;
14+
15+
import java.io.IOException;
16+
17+
/**
18+
*No-op {@link MetricsRegistry}
19+
* {@opensearch.internal}
20+
*/
21+
@InternalApi
22+
public class NoopMetricsRegistry implements MetricsRegistry {
23+
24+
/**
25+
* No-op Meter instance
26+
*/
27+
public final static NoopMetricsRegistry INSTANCE = new NoopMetricsRegistry();
28+
29+
private NoopMetricsRegistry() {}
30+
31+
@Override
32+
public Counter createCounter(String name, String description, String unit) {
33+
return NoopCounter.INSTANCE;
34+
}
35+
36+
@Override
37+
public Counter createUpDownCounter(String name, String description, String unit) {
38+
return NoopCounter.INSTANCE;
39+
}
40+
41+
@Override
42+
public void close() throws IOException {
43+
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
/**
10+
* Contains metrics related classes
11+
* {@opensearch.internal}
12+
*/
13+
@InternalApi
14+
package org.opensearch.telemetry.metrics.noop;
15+
16+
import org.opensearch.common.annotation.InternalApi;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.telemetry.metrics.tags;
10+
11+
import org.opensearch.common.annotation.ExperimentalApi;
12+
13+
import java.util.Collections;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
import java.util.Objects;
17+
18+
/**
19+
* Class to create tags for a meter.
20+
*
21+
* @opensearch.experimental
22+
*/
23+
@ExperimentalApi
24+
public class Tags {
25+
private final Map<String, Object> tagsMap;
26+
/**
27+
* Empty value.
28+
*/
29+
public final static Tags EMPTY = new Tags(Collections.emptyMap());
30+
31+
/**
32+
* Factory method.
33+
* @return tags.
34+
*/
35+
public static Tags create() {
36+
return new Tags(new HashMap<>());
37+
}
38+
39+
/**
40+
* Constructor.
41+
*/
42+
private Tags(Map<String, Object> tagsMap) {
43+
this.tagsMap = tagsMap;
44+
}
45+
46+
/**
47+
* Add String attribute.
48+
* @param key key
49+
* @param value value
50+
* @return Same instance.
51+
*/
52+
public Tags addTag(String key, String value) {
53+
Objects.requireNonNull(value, "value cannot be null");
54+
tagsMap.put(key, value);
55+
return this;
56+
}
57+
58+
/**
59+
* Add long attribute.
60+
* @param key key
61+
* @param value value
62+
* @return Same instance.
63+
*/
64+
public Tags addTag(String key, long value) {
65+
tagsMap.put(key, value);
66+
return this;
67+
};
68+
69+
/**
70+
* Add double attribute.
71+
* @param key key
72+
* @param value value
73+
* @return Same instance.
74+
*/
75+
public Tags addTag(String key, double value) {
76+
tagsMap.put(key, value);
77+
return this;
78+
};
79+
80+
/**
81+
* Add boolean attribute.
82+
* @param key key
83+
* @param value value
84+
* @return Same instance.
85+
*/
86+
public Tags addTag(String key, boolean value) {
87+
tagsMap.put(key, value);
88+
return this;
89+
};
90+
91+
/**
92+
* Returns the attribute map.
93+
* @return tags map
94+
*/
95+
public Map<String, ?> getTagsMap() {
96+
return Collections.unmodifiableMap(tagsMap);
97+
}
98+
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
/**
10+
* Contains metrics related classes
11+
* @opensearch.experimental
12+
*/
13+
@ExperimentalApi
14+
package org.opensearch.telemetry.metrics.tags;
15+
16+
import org.opensearch.common.annotation.ExperimentalApi;

libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/TracingTelemetry.java

-5
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,4 @@ public interface TracingTelemetry extends Closeable {
3535
*/
3636
TracingContextPropagator getContextPropagator();
3737

38-
/**
39-
* closes the resource
40-
*/
41-
void close();
42-
4338
}

0 commit comments

Comments
 (0)