Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic integration test for OtlpMeterRegistry and the OTel collector #3668

Merged
merged 4 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def VERSIONS = [
'info.ganglia.gmetric4j:gmetric4j:latest.release',
'io.prometheus:simpleclient_common:0.15.+',
'io.prometheus:simpleclient_pushgateway:0.15.+',
'io.rest-assured:rest-assured:latest.release',
'javax.cache:cache-api:latest.release',
'javax.inject:javax.inject:1',
'javax.xml.bind:jaxb-api:2.3.+',
Expand Down
3 changes: 3 additions & 0 deletions implementations/micrometer-registry-otlp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ dependencies {

testImplementation project(':micrometer-test')
testImplementation 'uk.org.webcompere:system-stubs-jupiter:latest.release'
testImplementation 'io.rest-assured:rest-assured'
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.awaitility:awaitility'
}

// new module in 1.9. This should be removed in later branches.
Expand Down
33 changes: 30 additions & 3 deletions implementations/micrometer-registry-otlp/gradle.lockfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.registry.otlp;

import io.micrometer.core.instrument.*;
import io.restassured.response.Response;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.time.Duration;

import static io.restassured.RestAssured.given;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.*;
import static org.testcontainers.containers.BindMode.READ_ONLY;
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariables;

/**
* Integration tests for {@link OtlpMeterRegistry} and the OTel collector
*
* @author Jonatan Ivanov
*/
@Testcontainers
@Tag("docker")
class OTelCollectorIntegrationTest {

private static final String OPENMETRICS_100 = "application/openmetrics-text; version=1.0.0; charset=utf-8";

private static final String CONFIG_FILE_NAME = "collector-config.yml";

@Container
private final GenericContainer<?> container = new GenericContainer("otel/opentelemetry-collector-contrib")
.withCommand("--config=/etc/" + CONFIG_FILE_NAME)
.withClasspathResourceMapping(CONFIG_FILE_NAME, "/etc/" + CONFIG_FILE_NAME, READ_ONLY)
.withExposedPorts(4318, 9090); // HTTP receiver, Prometheus exporter

@Test
void collectorShouldExportMetrics() throws Exception {
MeterRegistry registry = createOtlpMeterRegistryForContainer(container);
Counter.builder("test.counter").register(registry).increment(42);
Gauge.builder("test.gauge", () -> 12).register(registry);
Timer.builder("test.timer").register(registry).record(Duration.ofMillis(123));
DistributionSummary.builder("test.distributionsummary").register(registry).record(24);

await().atMost(Duration.ofSeconds(5))
.pollDelay(Duration.ofMillis(100))
.pollInterval(Duration.ofMillis(100))
.untilAsserted(() -> whenPrometheusScraped().then().body(not(empty())));

// @formatter:off
// tags can vary depending on where you run your tests:
// - IDE: no telemetry_sdk_version tag
// - Gradle: telemetry_sdk_version has the version number
whenPrometheusScraped().then().body(
containsString("{job=\"test\",service_name=\"test\",telemetry_sdk_language=\"java\",telemetry_sdk_name=\"io.micrometer\""),

matchesPattern("(?s)^.*test_counter\\{.+} 42\\n.*$"),
matchesPattern("(?s)^.*test_gauge\\{.+} 12\\n.*$"),

matchesPattern("(?s)^.*test_timer_count\\{.+} 1\\n.*$"),
matchesPattern("(?s)^.*test_timer_sum\\{.+} 123\\n.*$"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I'm looking at this, I don't think this is correct, this should be 123ms not 123s.

Copy link
Member Author

@jonatan-ivanov jonatan-ivanov Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a collector issue, the data that we send looks like this:

metrics {
  name: "test.timer"
  unit: "milliseconds"
  histogram {
    data_points {
      start_time_unix_nano: 1677210838494000000
      time_unix_nano: 1677210839021000000
      count: 1
      sum: 123.0
    }
    aggregation_temporality: AGGREGATION_TEMPORALITY_CUMULATIVE
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I guess we'll need a fix in the collector before we can change the test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like it might be intentional that no conversion is done currently. But then there's no way to know the unit as far as I can tell.
https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/translator/prometheus

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if there are receivers that mandate a unit that is not seconds.
Or two receivers that mandate different units. Or two exporters with different units.
In all of these cases I think not doing the conversion can lead to wrong result.

matchesPattern("(?s)^.*test_timer_bucket\\{.+,le=\"\\+Inf\"} 1\\n.*$"),

matchesPattern("(?s)^.*test_distributionsummary_count\\{.+} 1\\n.*$"),
matchesPattern("(?s)^.*test_distributionsummary_sum\\{.+} 24\\n.*$"),
matchesPattern("(?s)^.*test_distributionsummary_bucket\\{.+,le=\"\\+Inf\"} 1\\n.*$")
);
// @formatter:on
}

private OtlpMeterRegistry createOtlpMeterRegistryForContainer(GenericContainer<?> container) throws Exception {
return withEnvironmentVariables("OTEL_SERVICE_NAME", "test")
.execute(() -> new OtlpMeterRegistry(createOtlpConfigForContainer(container), Clock.SYSTEM));
}

private OtlpConfig createOtlpConfigForContainer(GenericContainer<?> container) {
return new OtlpConfig() {
@Override
public String url() {
return String.format("http://%s:%d/v1/metrics", container.getHost(), container.getMappedPort(4318));
}

@Override
public Duration step() {
return Duration.ofSeconds(1);
}

@Override
public String get(String key) {
return null;
}
};
}

private Response whenPrometheusScraped() {
// @formatter:off
return given()
.port(container.getMappedPort(9090))
.accept(OPENMETRICS_100)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried running the collector locally, I was not able to get it to return OpenMetrics format even when sending this Accept header and configuring the prometheus exporter with enable_open_metrics: true. Debugging this test, it looks like the response is also not OpenMetrics format.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, I did not even notice this. This is another bug in the collector.
If I send 1.0.0 (what I should), it does not work:

Accept: application/openmetrics-text; version=1.0.0; charset=utf-8

If I send 0.0.1 (what I should not), it works:

Accept: application/openmetrics-text; version=0.0.1; charset=utf-8

See details here: open-telemetry/opentelemetry-collector-contrib#18913

Copy link
Member Author

@jonatan-ivanov jonatan-ivanov Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this by requesting (and verifying) 0.0.1 and also included a TODO item.

.when()
.get("/metrics");
// @formatter:on
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Copyright 2023 VMware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
receivers:
otlp:
protocols:
grpc:
http:

exporters:
# https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/prometheusexporter
prometheus:
endpoint: '0.0.0.0:9090'
metric_expiration: 1m
enable_open_metrics: true
resource_to_telemetry_conversion:
enabled: true

service:
pipelines:
metrics:
receivers: [otlp]
exporters: [prometheus]