|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.action.otlp; |
| 9 | + |
| 10 | +import io.opentelemetry.api.common.Attributes; |
| 11 | +import io.opentelemetry.exporter.internal.FailedExportException; |
| 12 | +import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter; |
| 13 | +import io.opentelemetry.sdk.common.Clock; |
| 14 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 15 | +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; |
| 16 | +import io.opentelemetry.sdk.metrics.SdkMeterProvider; |
| 17 | +import io.opentelemetry.sdk.metrics.data.MetricData; |
| 18 | +import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; |
| 19 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableDoublePointData; |
| 20 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableGaugeData; |
| 21 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData; |
| 22 | +import io.opentelemetry.sdk.resources.Resource; |
| 23 | + |
| 24 | +import org.elasticsearch.client.Request; |
| 25 | +import org.elasticsearch.common.settings.SecureString; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 28 | +import org.elasticsearch.rest.RestStatus; |
| 29 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 30 | +import org.elasticsearch.test.cluster.local.distribution.DistributionType; |
| 31 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.ClassRule; |
| 34 | + |
| 35 | +import java.time.Duration; |
| 36 | +import java.util.List; |
| 37 | +import java.util.concurrent.Executors; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | + |
| 40 | +import static io.opentelemetry.api.common.AttributeKey.stringKey; |
| 41 | +import static org.hamcrest.Matchers.equalTo; |
| 42 | +import static org.hamcrest.Matchers.is; |
| 43 | + |
| 44 | +public class OTLPMetricsIndexingRestIT extends ESRestTestCase { |
| 45 | + |
| 46 | + private static final String USER = "test_admin"; |
| 47 | + private static final String PASS = "x-pack-test-password"; |
| 48 | + private static final Resource TEST_RESOURCE = Resource.create(Attributes.of(stringKey("service.name"), "elasticsearch")); |
| 49 | + private static final InstrumentationScopeInfo TEST_SCOPE = InstrumentationScopeInfo.create("io.opentelemetry.example.metrics"); |
| 50 | + private OtlpHttpMetricExporter exporter; |
| 51 | + private SdkMeterProvider meterProvider; |
| 52 | + |
| 53 | + @ClassRule |
| 54 | + public static ElasticsearchCluster cluster = ElasticsearchCluster.local() |
| 55 | + .distribution(DistributionType.DEFAULT) |
| 56 | + .user(USER, PASS, "superuser", false) |
| 57 | + .setting("xpack.security.autoconfiguration.enabled", "false") |
| 58 | + .setting("xpack.license.self_generated.type", "trial") |
| 59 | + .build(); |
| 60 | + |
| 61 | + @Override |
| 62 | + protected String getTestRestCluster() { |
| 63 | + return cluster.getHttpAddresses(); |
| 64 | + } |
| 65 | + |
| 66 | + protected Settings restClientSettings() { |
| 67 | + String token = basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())); |
| 68 | + return Settings.builder().put(super.restClientSettings()).put(ThreadContext.PREFIX + ".Authorization", token).build(); |
| 69 | + } |
| 70 | + |
| 71 | + @Before |
| 72 | + public void beforeTest() throws Exception { |
| 73 | + exporter = OtlpHttpMetricExporter.builder() |
| 74 | + .setEndpoint(getClusterHosts().getFirst().toURI() + "/_otlp/v1/metrics") |
| 75 | + .addHeader("Authorization", basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray()))) |
| 76 | + .build(); |
| 77 | + meterProvider = SdkMeterProvider.builder() |
| 78 | + .registerMetricReader( |
| 79 | + PeriodicMetricReader.builder(exporter) |
| 80 | + .setExecutor(Executors.newScheduledThreadPool(0)) |
| 81 | + .setInterval(Duration.ofNanos(Long.MAX_VALUE)) |
| 82 | + .build() |
| 83 | + ) |
| 84 | + .build(); |
| 85 | + assertBusy(() -> assertOK(client().performRequest(new Request("GET", "_index_template/metrics-otel@template")))); |
| 86 | + boolean otlpEndpointEnabled = false; |
| 87 | + try { |
| 88 | + otlpEndpointEnabled = RestStatus.isSuccessful( |
| 89 | + client().performRequest(new Request("POST", "/_otlp/v1/metrics")).getStatusLine().getStatusCode() |
| 90 | + ); |
| 91 | + } catch (Exception ignore) {} |
| 92 | + assumeTrue("Requires otlp_metrics feature flag to be enabled", otlpEndpointEnabled); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void tearDown() throws Exception { |
| 97 | + meterProvider.close(); |
| 98 | + super.tearDown(); |
| 99 | + } |
| 100 | + |
| 101 | + public void testIngestMetricDataViaMetricExporter() throws Exception { |
| 102 | + MetricData jvmMemoryMetricData = createDoubleGauge( |
| 103 | + TEST_RESOURCE, |
| 104 | + Attributes.empty(), |
| 105 | + "jvm.memory.total", |
| 106 | + Runtime.getRuntime().totalMemory(), |
| 107 | + "By", |
| 108 | + Clock.getDefault().now() |
| 109 | + ); |
| 110 | + |
| 111 | + FailedExportException.HttpExportException exception = assertThrows( |
| 112 | + FailedExportException.HttpExportException.class, |
| 113 | + () -> export(List.of(jvmMemoryMetricData)) |
| 114 | + ); |
| 115 | + assertThat(exception.getResponse().statusCode(), equalTo(RestStatus.NOT_IMPLEMENTED.getStatus())); |
| 116 | + } |
| 117 | + |
| 118 | + private void export(List<MetricData> metrics) throws Exception { |
| 119 | + CompletableResultCode result = exporter.export(metrics).join(10, TimeUnit.SECONDS); |
| 120 | + Throwable failure = result.getFailureThrowable(); |
| 121 | + if (failure instanceof Exception e) { |
| 122 | + throw e; |
| 123 | + } else if (failure != null) { |
| 124 | + throw new RuntimeException("Failed to export metrics", failure); |
| 125 | + } |
| 126 | + assertThat(result.isSuccess(), is(true)); |
| 127 | + assertOK(client().performRequest(new Request("GET", "_refresh/metrics-*"))); |
| 128 | + } |
| 129 | + |
| 130 | + private static MetricData createDoubleGauge( |
| 131 | + Resource resource, |
| 132 | + Attributes attributes, |
| 133 | + String name, |
| 134 | + double value, |
| 135 | + String unit, |
| 136 | + long timeEpochNanos |
| 137 | + ) { |
| 138 | + return ImmutableMetricData.createDoubleGauge( |
| 139 | + resource, |
| 140 | + TEST_SCOPE, |
| 141 | + name, |
| 142 | + "Your description could be here.", |
| 143 | + unit, |
| 144 | + ImmutableGaugeData.create(List.of(ImmutableDoublePointData.create(timeEpochNanos, timeEpochNanos, attributes, value))) |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
0 commit comments