Skip to content

Commit

Permalink
eureka: allows authentication (#107)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
codefromthecrypt authored Jan 22, 2024
1 parent 1094f88 commit 04a31f5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
9 changes: 9 additions & 0 deletions armeria/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ requests. Both services run as a normal Java application.

* brave.example.Frontend and Backend : HTTP controllers with a trace configuration hook
* brave.example.HttpTracingFactory : Configures the tracing subsystem

### Service Discovery with Eureka

This supports discovery of zipkin via [Eureka](https://github.com/Netflix/eureka).
To do so, set `EUREKA_SERVICE_URL` to a possibly authenticated v2 endpoint.

Here are some examples:
* Eureka is on localhost: `EUREKA_SERVICE_URL=http://localhost:8761/eureka/v2`
* Eureka is authenticated: `EUREKA_SERVICE_URL=http://username:password@localhost:8761/eureka/v2`
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package brave.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import brave.Tracing;
import brave.baggage.BaggageField;
import brave.baggage.BaggagePropagation;
Expand All @@ -15,10 +13,15 @@
import brave.propagation.Propagation;
import com.linecorp.armeria.client.WebClient;
import com.linecorp.armeria.client.eureka.EurekaEndpointGroup;
import com.linecorp.armeria.client.eureka.EurekaEndpointGroupBuilder;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.auth.BasicToken;
import com.linecorp.armeria.common.brave.RequestContextCurrentTraceContext;
import com.linecorp.armeria.common.logging.RequestLogLevelMapper;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import zipkin2.reporter.BytesMessageSender;
import zipkin2.reporter.brave.AsyncZipkinSpanHandler;

Expand Down Expand Up @@ -67,21 +70,45 @@ static Propagation.Factory propagationFactory() {

/** Configuration for how to send spans to Zipkin */
static BytesMessageSender sender() {
String postPath = "/api/v2/spans";
String postZipkinSpans = "/api/v2/spans";
String eurekaUri = System.getenv("EUREKA_SERVICE_URL");
if (eurekaUri != null && !eurekaUri.isEmpty()) {
EurekaEndpointGroup zipkin =
EurekaEndpointGroup.builder(eurekaUri).appName("zipkin").build();
LOGGER.info("Using eureka to discover zipkin: {}", eurekaUri);
URI serviceUrl = URI.create(eurekaUri);
BasicToken auth = null;
if (serviceUrl.getUserInfo() != null) {
LOGGER.info("Using eureka authentication");
String[] ui = serviceUrl.getUserInfo().split(":");
if (ui.length == 2) {
auth = BasicToken.ofBasic(ui[0], ui[1]);
}
serviceUrl = stripBaseUrl(serviceUrl);
}

EurekaEndpointGroupBuilder eb = EurekaEndpointGroup.builder(serviceUrl).appName("zipkin");
if (auth != null) eb.auth(auth);
EurekaEndpointGroup zipkin = eb.build();
LOGGER.info("Using eureka to discover zipkin: {}", serviceUrl);
Runtime.getRuntime().addShutdownHook(new Thread(zipkin::close));
return new WebClientSender(WebClient.of(SessionProtocol.H2C, zipkin, postPath));
return new WebClientSender(WebClient.of(SessionProtocol.H2C, zipkin, postZipkinSpans));
}
String zipkinUri =
System.getProperty("zipkin.baseUrl", "http://127.0.0.1:9411") + postPath;
System.getProperty("zipkin.baseUrl", "http://127.0.0.1:9411") + postZipkinSpans;
LOGGER.info("Using zipkin URI: {}", zipkinUri);
return new WebClientSender(WebClient.of(zipkinUri));
}

// Strip the credentials and any invalid query or fragment from the URI:
// The Eureka API doesn't define any global query params or fragment.
// See https://github.com/Netflix/eureka/wiki/Eureka-REST-operations
static URI stripBaseUrl(URI baseUrl) {
try {
return new URI(baseUrl.getScheme(), null, baseUrl.getHost(), baseUrl.getPort(),
baseUrl.getPath(), null, null);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}

/** Configuration for how to buffer spans into messages for Zipkin */
static AsyncZipkinSpanHandler spanHandler(BytesMessageSender sender) {
final AsyncZipkinSpanHandler spanHandler = AsyncZipkinSpanHandler.create(sender);
Expand Down

0 comments on commit 04a31f5

Please sign in to comment.