Skip to content

Commit

Permalink
Break circular dependency when zipkin tracing is enabled (#1049)
Browse files Browse the repository at this point in the history
* Break circular dependency when zipkin tracing is enabled

* Update gcp-tracing/src/test/resources/logback.xml

* Update gcp-tracing/src/test/groovy/io/micronaut/gcp/tracing/zipkin/StackdriverConfigurationSpec.groovy

---------

Co-authored-by: Sergio del Amo <[email protected]>
  • Loading branch information
jeremyg484 and sdelamo authored Feb 16, 2024
1 parent 705b91b commit 0470d3d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.micronaut.core.io.buffer.ByteBuffer;
import io.micronaut.core.type.MutableHeaders;
import io.micronaut.core.util.StringUtils;
import io.micronaut.core.util.SupplierUtil;
import io.micronaut.http.*;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
Expand All @@ -40,6 +41,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
* An implementation of {@link HttpTransportFactory} based upon {@link HttpClient} that can be supplied
Expand All @@ -59,7 +61,7 @@ public class DefaultOAuth2HttpTransportFactory implements HttpTransportFactory {

private static final Logger LOG = LoggerFactory.getLogger(DefaultOAuth2HttpTransportFactory.class);

private final HttpClientHttpTransport transport;
private final Supplier<HttpClientHttpTransport> transport;

/**
* Constructor for {@code DefaultOAuth2HttpTransportFactory}.
Expand All @@ -68,13 +70,14 @@ public class DefaultOAuth2HttpTransportFactory implements HttpTransportFactory {
* @param defaultClientConfiguration the default HTTP client configuration
*/
public DefaultOAuth2HttpTransportFactory(BeanContext beanContext, HttpClientConfiguration defaultClientConfiguration) {
HttpClient httpClient = beanContext.createBean(HttpClient.class, LoadBalancer.empty(), defaultClientConfiguration);
this.transport = new HttpClientHttpTransport(httpClient);
this.transport = SupplierUtil.memoized(() -> new HttpClientHttpTransport(beanContext.createBean(HttpClient.class,
LoadBalancer.empty(),
defaultClientConfiguration)));
}

@Override
public HttpTransport create() {
return this.transport;
return this.transport.get();
}

private static final class HttpClientHttpTransport extends HttpTransport {
Expand Down
2 changes: 2 additions & 0 deletions gcp-tracing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ dependencies {
implementation(libs.brave.opentracing)
testAnnotationProcessor(mn.micronaut.inject.java)
testImplementation(mn.micronaut.inject.java)
testImplementation(mnSerde.micronaut.serde.jackson)
testImplementation(testFixtures(project(":micronaut-gcp-common")))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.micronaut.gcp.tracing.zipkin

import io.micronaut.context.ApplicationContext
import io.micronaut.gcp.credentials.GoogleCredentialsConfiguration
import io.micronaut.gcp.credentials.fixture.ServiceAccountCredentialsTestHelper
import spock.lang.Issue
import spock.lang.Specification
import zipkin2.reporter.Sender

import java.security.PrivateKey

class StackdriverConfigurationSpec extends Specification {

@Issue("https://github.com/micronaut-projects/micronaut-gcp/issues/1045")
void "application starts successfully with stackdriver and zipkin enabled"() {
given:
PrivateKey pk = ServiceAccountCredentialsTestHelper.generatePrivateKey()
File serviceAccountCredentials = ServiceAccountCredentialsTestHelper.writeServiceCredentialsToTempFile(pk)
ApplicationContext ctx = ApplicationContext.run([
"gcp.project-id" : "test-project",
(GoogleCredentialsConfiguration.PREFIX + ".location"): serviceAccountCredentials.getPath(),
"tracing.zipkin.enabled": true
])

when:
Sender sender = ctx.getBean(Sender.class)

then:
noExceptionThrown()
sender

cleanup:
ctx.close()
}
}
13 changes: 13 additions & 0 deletions gcp-tracing/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>

</configuration>

0 comments on commit 0470d3d

Please sign in to comment.