-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from 2 commits
f7ecfd9
eb73b55
1b462cb
08d8a61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.*$"), | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 0.0.1 (what I should not), it works:
See details here: open-telemetry/opentelemetry-collector-contrib#18913 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created an issue to track this: open-telemetry/opentelemetry-collector-contrib#18903
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.