Skip to content

Commit

Permalink
enforce static imports (#12009)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger authored Aug 21, 2024
1 parent 9ca4ec4 commit 749fafa
Show file tree
Hide file tree
Showing 34 changed files with 202 additions and 210 deletions.
12 changes: 12 additions & 0 deletions buildscripts/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@
-->

<module name="TreeWalker">
<module name="RegexpSinglelineJava">
<property name="format"
value="(?&lt;!import static io.opentelemetry.sdk.testing.assertj.)OpenTelemetryAssertions\."/>
<property name="message"
value="Please statically import methods from OpenTelemetryAssertions"/>
</module>
<module name="RegexpSinglelineJava">
<property name="format"
value="(?&lt;!import static org.assertj.core.api.|OpenTelemetry)Assertions.assertThat"/>
<property name="message"
value="Please statically import methods from Assertions (OpenTelemetryAssertions extends Assertions)"/>
</module>
<module name="OuterTypeFilename"/>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
Expand Down
5 changes: 5 additions & 0 deletions docs/contributing/style-guideline.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ rough guideline of what are commonly accepted static imports:
- Singleton instances (especially where clearly named and hopefully immutable)
- `tracer()` methods that expose tracer singleton instances

Some of these are enforced by checkstyle rules:

- look for `RegexpSinglelineJava` in `checkstyle.xml`
- use `@SuppressWarnings("checkstyle:RegexpSinglelineJava")` to suppress the checkstyle warning

## Ordering of class contents

The following order is preferred:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package io.opentelemetry.instrumentation.api.incubator.semconv.messaging;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
Expand All @@ -16,7 +16,6 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import io.opentelemetry.semconv.ServerAttributes;
import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes;
Expand Down Expand Up @@ -76,7 +75,7 @@ void collectsMetrics() {
assertThat(metricReader.collectAllMetrics())
.satisfiesExactlyInAnyOrder(
metric ->
OpenTelemetryAssertions.assertThat(metric)
assertThat(metric)
.hasName("messaging.publish.duration")
.hasUnit("s")
.hasDescription("Measures the duration of publish operation.")
Expand Down Expand Up @@ -112,7 +111,7 @@ void collectsMetrics() {
assertThat(metricReader.collectAllMetrics())
.satisfiesExactlyInAnyOrder(
metric ->
OpenTelemetryAssertions.assertThat(metric)
assertThat(metric)
.hasName("messaging.publish.duration")
.hasHistogramSatisfying(
histogram ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.instrumentation.api.internal;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.MeterProvider;
Expand All @@ -13,7 +15,6 @@
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import java.lang.reflect.Proxy;
import java.util.concurrent.atomic.AtomicBoolean;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand All @@ -29,7 +30,7 @@ void noWarning() {
"test metrics", meter -> null, (s, doubleHistogramBuilder) -> warning.set(true));
operationMetrics.create(testing.getOpenTelemetry().getMeter("test"));

Assertions.assertThat(warning).isFalse();
assertThat(warning).isFalse();
}

@Test
Expand All @@ -40,7 +41,7 @@ void noWarningWithNoopMetrics() {
"test metrics", meter -> null, (s, doubleHistogramBuilder) -> warning.set(true));
operationMetrics.create(MeterProvider.noop().get("test"));

Assertions.assertThat(warning).isFalse();
assertThat(warning).isFalse();
}

@Test
Expand All @@ -65,7 +66,7 @@ void warning() {
});
operationMetrics.create(meter);

Assertions.assertThat(warning).isTrue();
assertThat(warning).isTrue();
}

private static DoubleHistogramBuilder proxyDoubleHistogramBuilder(Meter meter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
Expand Down Expand Up @@ -183,8 +182,7 @@ public List<Consumer<SpanDataAssert>> errorPageSpanAssertions(
if (endpoint == NOT_FOUND) {
spanAssertions.add(
span ->
span.satisfies(
spanData -> Assertions.assertThat(spanData.getName()).endsWith(".sendError"))
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendError"))
.hasKind(SpanKind.INTERNAL)
.hasAttributesSatisfying(Attributes::isEmpty));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.instrumentation.grpc.v1_6;

import static org.assertj.core.api.Assertions.assertThat;

import example.GreeterGrpc;
import example.Helloworld;
import io.grpc.BindableService;
Expand All @@ -23,7 +25,6 @@
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -124,7 +125,7 @@ public void sayHello(
"parent",
() -> client.sayHello(Helloworld.Request.newBuilder().setName("test").build()));

OpenTelemetryAssertions.assertThat(response.getMessage()).isEqualTo("Hello test");
assertThat(response.getMessage()).isEqualTo("Hello test");

testing()
.waitAndAssertTraces(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.util.ThrowingRunnable;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.semconv.NetworkAttributes;
import io.opentelemetry.semconv.ServerAttributes;
Expand Down Expand Up @@ -1643,7 +1642,7 @@ public void sayHello(
"parent",
() -> client.sayHello(Helloworld.Request.newBuilder().setName("test").build()));

OpenTelemetryAssertions.assertThat(response.getMessage()).isEqualTo("Hello test");
assertThat(response.getMessage()).isEqualTo("Hello test");

testing()
.waitAndAssertTraces(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
package io.opentelemetry.instrumentation.lettuce.v5_1;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static org.assertj.core.api.Assertions.assertThat;

import io.lettuce.core.api.sync.RedisCommands;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import io.opentelemetry.semconv.NetworkAttributes;
import io.opentelemetry.semconv.ServerAttributes;
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
Expand Down Expand Up @@ -90,7 +90,7 @@ void testAuthCommand() throws Exception {
equalTo(ServerAttributes.SERVER_ADDRESS, host),
equalTo(ServerAttributes.SERVER_PORT, port),
equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"),
OpenTelemetryAssertions.satisfies(
satisfies(
DbIncubatingAttributes.DB_STATEMENT,
stringAssert ->
stringAssert.startsWith("CLIENT SETINFO lib-ver")))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.lettuce.v5_1;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
Expand All @@ -20,7 +21,6 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.test.utils.PortUtils;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import io.opentelemetry.semconv.NetworkAttributes;
import io.opentelemetry.semconv.ServerAttributes;
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
Expand Down Expand Up @@ -396,7 +396,7 @@ void testDebugSegfaultCommandWithNoArgumentProducesNoSpan() {
equalTo(ServerAttributes.SERVER_ADDRESS, host),
equalTo(ServerAttributes.SERVER_PORT, containerConnection.port),
equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"),
OpenTelemetryAssertions.satisfies(
satisfies(
DbIncubatingAttributes.DB_STATEMENT,
stringAssert ->
stringAssert.startsWith("CLIENT SETINFO lib-ver")))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
package io.opentelemetry.instrumentation.log4j.appender.v2_17;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import java.util.List;
import org.apache.logging.log4j.message.StringMapMessage;
import org.apache.logging.log4j.message.StructuredDataMessage;
Expand Down Expand Up @@ -45,7 +44,7 @@ void twoLogs() {

List<LogRecordData> logDataList = logRecordExporter.getFinishedLogRecordItems();
assertThat(logDataList).hasSize(1);
OpenTelemetryAssertions.assertThat(logDataList.get(0))
assertThat(logDataList.get(0))
.hasResource(resource)
.hasInstrumentationScope(instrumentationScopeInfo)
.hasBody("log message 1");
Expand All @@ -69,7 +68,7 @@ void twoLogsStringMapMessage() {

List<LogRecordData> logDataList = logRecordExporter.getFinishedLogRecordItems();
assertThat(logDataList).hasSize(1);
OpenTelemetryAssertions.assertThat(logDataList.get(0))
assertThat(logDataList.get(0))
.hasResource(resource)
.hasInstrumentationScope(instrumentationScopeInfo)
.hasAttributesSatisfyingExactly(
Expand All @@ -94,7 +93,7 @@ void twoLogsStructuredDataMessage() {

List<LogRecordData> logDataList = logRecordExporter.getFinishedLogRecordItems();
assertThat(logDataList).hasSize(1);
OpenTelemetryAssertions.assertThat(logDataList.get(0))
assertThat(logDataList.get(0))
.hasResource(resource)
.hasInstrumentationScope(instrumentationScopeInfo)
.hasBody("a message")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

package io.opentelemetry.instrumentation.logback.appender.v1_0;

import static org.assertj.core.api.Assertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.spi.ContextAware;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import java.net.URL;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -65,7 +64,7 @@ void twoLogs() {
List<LogRecordData> logDataList = logRecordExporter.getFinishedLogRecordItems();
assertThat(logDataList).hasSize(1);
LogRecordData logData = logDataList.get(0);
OpenTelemetryAssertions.assertThat(logData)
assertThat(logData)
.hasResource(resource)
.hasInstrumentationScope(instrumentationScopeInfo)
.hasBody("log message 1")
Expand Down
Loading

0 comments on commit 749fafa

Please sign in to comment.