|
12 | 12 | import org.elasticsearch.action.admin.indices.stats.IndexStats; |
13 | 13 | import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; |
14 | 14 | import org.elasticsearch.action.admin.indices.stats.ShardStats; |
| 15 | +import org.elasticsearch.cluster.metadata.ProjectId; |
15 | 16 | import org.elasticsearch.common.settings.Settings; |
16 | 17 | import org.elasticsearch.common.unit.ByteSizeValue; |
17 | 18 | import org.elasticsearch.common.util.CollectionUtils; |
| 19 | +import org.elasticsearch.common.util.Maps; |
18 | 20 | import org.elasticsearch.core.TimeValue; |
| 21 | +import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus; |
19 | 22 | import org.elasticsearch.plugins.Plugin; |
20 | 23 | import org.elasticsearch.plugins.PluginsService; |
21 | 24 | import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; |
| 25 | +import org.elasticsearch.snapshots.SnapshotState; |
| 26 | +import org.elasticsearch.telemetry.InstrumentType; |
22 | 27 | import org.elasticsearch.telemetry.Measurement; |
| 28 | +import org.elasticsearch.telemetry.RecordingMeterRegistry; |
23 | 29 | import org.elasticsearch.telemetry.TestTelemetryPlugin; |
24 | 30 | import org.elasticsearch.test.ESIntegTestCase; |
25 | 31 | import org.elasticsearch.threadpool.ThreadPool; |
26 | 32 | import org.hamcrest.Matcher; |
27 | 33 |
|
28 | 34 | import java.util.Collection; |
29 | 35 | import java.util.List; |
| 36 | +import java.util.Map; |
30 | 37 | import java.util.stream.Stream; |
31 | 38 | import java.util.stream.StreamSupport; |
32 | 39 |
|
33 | 40 | import static org.elasticsearch.threadpool.ThreadPool.ESTIMATED_TIME_INTERVAL_SETTING; |
34 | 41 | import static org.hamcrest.Matchers.allOf; |
| 42 | +import static org.hamcrest.Matchers.empty; |
35 | 43 | import static org.hamcrest.Matchers.equalTo; |
36 | 44 | import static org.hamcrest.Matchers.everyItem; |
37 | 45 | import static org.hamcrest.Matchers.greaterThan; |
| 46 | +import static org.hamcrest.Matchers.hasEntry; |
38 | 47 | import static org.hamcrest.Matchers.hasItem; |
39 | 48 | import static org.hamcrest.Matchers.hasSize; |
40 | 49 | import static org.hamcrest.Matchers.lessThan; |
| 50 | +import static org.hamcrest.Matchers.not; |
41 | 51 |
|
42 | 52 | @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST) |
43 | 53 | public class SnapshotMetricsIT extends AbstractSnapshotIntegTestCase { |
@@ -170,6 +180,65 @@ public void testSnapshotAPMMetrics() throws Exception { |
170 | 180 |
|
171 | 181 | // assert we throttled on restore |
172 | 182 | assertThat(getTotalClusterLongCounterValue(SnapshotMetrics.SNAPSHOT_RESTORE_THROTTLE_DURATION), greaterThan(0L)); |
| 183 | + |
| 184 | + // assert appropriate attributes are present |
| 185 | + final Map<String, Object> expectedAttrs = Map.of( |
| 186 | + "project_id", |
| 187 | + ProjectId.DEFAULT.id(), |
| 188 | + "repo_name", |
| 189 | + repositoryName, |
| 190 | + "repo_type", |
| 191 | + "mock" |
| 192 | + ); |
| 193 | + final Map<String, Object> expectedAttrsWithShardStage = Maps.copyMapWithAddedEntry( |
| 194 | + expectedAttrs, |
| 195 | + "stage", |
| 196 | + IndexShardSnapshotStatus.Stage.DONE.name() |
| 197 | + ); |
| 198 | + final Map<String, Object> expectedAttrsWithSnapshotState = Maps.copyMapWithAddedEntry( |
| 199 | + expectedAttrs, |
| 200 | + "state", |
| 201 | + SnapshotState.SUCCESS.name() |
| 202 | + ); |
| 203 | + assertMetricsHaveAttributes(InstrumentType.LONG_COUNTER, SnapshotMetrics.SNAPSHOTS_STARTED, expectedAttrs); |
| 204 | + assertMetricsHaveAttributes(InstrumentType.LONG_GAUGE, SnapshotMetrics.SNAPSHOTS_IN_PROGRESS, expectedAttrs); |
| 205 | + assertMetricsHaveAttributes(InstrumentType.LONG_COUNTER, SnapshotMetrics.SNAPSHOTS_COMPLETED, expectedAttrsWithSnapshotState); |
| 206 | + assertMetricsHaveAttributes(InstrumentType.DOUBLE_HISTOGRAM, SnapshotMetrics.SNAPSHOT_DURATION, expectedAttrsWithSnapshotState); |
| 207 | + |
| 208 | + assertMetricsHaveAttributes(InstrumentType.LONG_COUNTER, SnapshotMetrics.SNAPSHOT_SHARDS_STARTED, expectedAttrs); |
| 209 | + assertMetricsHaveAttributes(InstrumentType.LONG_GAUGE, SnapshotMetrics.SNAPSHOT_SHARDS_IN_PROGRESS, expectedAttrs); |
| 210 | + assertMetricsHaveAttributes(InstrumentType.LONG_COUNTER, SnapshotMetrics.SNAPSHOT_SHARDS_COMPLETED, expectedAttrsWithShardStage); |
| 211 | + assertMetricsHaveAttributes(InstrumentType.DOUBLE_HISTOGRAM, SnapshotMetrics.SNAPSHOT_SHARDS_DURATION, expectedAttrsWithShardStage); |
| 212 | + |
| 213 | + assertMetricsHaveAttributes(InstrumentType.LONG_COUNTER, SnapshotMetrics.SNAPSHOT_RESTORE_THROTTLE_DURATION, expectedAttrs); |
| 214 | + assertMetricsHaveAttributes(InstrumentType.LONG_COUNTER, SnapshotMetrics.SNAPSHOT_CREATE_THROTTLE_DURATION, expectedAttrs); |
| 215 | + assertMetricsHaveAttributes(InstrumentType.LONG_COUNTER, SnapshotMetrics.SNAPSHOT_UPLOAD_READ_DURATION, expectedAttrs); |
| 216 | + assertMetricsHaveAttributes(InstrumentType.LONG_COUNTER, SnapshotMetrics.SNAPSHOT_UPLOAD_DURATION, expectedAttrs); |
| 217 | + assertMetricsHaveAttributes(InstrumentType.LONG_COUNTER, SnapshotMetrics.SNAPSHOT_BYTES_UPLOADED, expectedAttrs); |
| 218 | + assertMetricsHaveAttributes(InstrumentType.LONG_COUNTER, SnapshotMetrics.SNAPSHOT_BLOBS_UPLOADED, expectedAttrs); |
| 219 | + } |
| 220 | + |
| 221 | + private static void assertMetricsHaveAttributes( |
| 222 | + InstrumentType instrumentType, |
| 223 | + String metricName, |
| 224 | + Map<String, Object> expectedAttributes |
| 225 | + ) { |
| 226 | + final List<Measurement> clusterMeasurements = getClusterMeasurements(instrumentType, metricName); |
| 227 | + assertThat(clusterMeasurements, not(empty())); |
| 228 | + clusterMeasurements.forEach(recordingMetric -> { |
| 229 | + for (Map.Entry<String, Object> entry : expectedAttributes.entrySet()) { |
| 230 | + assertThat(recordingMetric.attributes(), hasEntry(entry.getKey(), entry.getValue())); |
| 231 | + } |
| 232 | + }); |
| 233 | + } |
| 234 | + |
| 235 | + private static List<Measurement> getClusterMeasurements(InstrumentType instrumentType, String metricName) { |
| 236 | + return allTestTelemetryPlugins().flatMap( |
| 237 | + testTelemetryPlugin -> ((RecordingMeterRegistry) testTelemetryPlugin.getTelemetryProvider(Settings.EMPTY).getMeterRegistry()) |
| 238 | + .getRecorder() |
| 239 | + .getMeasurements(instrumentType, metricName) |
| 240 | + .stream() |
| 241 | + ).toList(); |
173 | 242 | } |
174 | 243 |
|
175 | 244 | private static void assertDoubleHistogramMetrics(String metricName, Matcher<? super List<Double>> matcher) { |
|
0 commit comments