Updates deps, OC instrumentation, formatting. (#131)

This commit is contained in:
Bogdan Drutu 2019-01-30 13:35:03 -08:00 committed by Ahmet Alp Balkan
parent 02b2018b0e
commit 31df60f050
3 changed files with 138 additions and 126 deletions

View file

@ -10,12 +10,14 @@ buildscript {
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
classpath "gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.7.1"
}
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.github.sherter.google-java-format'
repositories {
mavenCentral()
@ -23,10 +25,10 @@ repositories {
}
group = "adservice"
version = "0.1.0-SNAPSHOT" // CURRENT_OPENCENSUS_VERSION
version = "0.1.0-SNAPSHOT"
def opencensusVersion = "0.17.0" // LATEST_OPENCENSUS_RELEASE_VERSION
def grpcVersion = "1.15.0" // CURRENT_GRPC_VERSION
def opencensusVersion = "0.18.0"
def grpcVersion = "1.17.0"
def jacksonVersion = "2.9.6"
tasks.withType(JavaCompile) {
@ -43,7 +45,9 @@ dependencies {
if (speed) {
compile fileTree(dir: offlineCompile, include: '*.jar')
} else {
compile "com.google.api.grpc:proto-google-common-protos:1.11.0",
compile "com.google.api.grpc:proto-google-common-protos:1.12.0",
"io.opencensus:opencensus-api:${opencensusVersion}",
"io.opencensus:opencensus-contrib-grpc-util:${opencensusVersion}",
"io.opencensus:opencensus-exporter-trace-jaeger:${opencensusVersion}",
"io.opencensus:opencensus-exporter-stats-stackdriver:${opencensusVersion}",
"io.opencensus:opencensus-exporter-trace-stackdriver:${opencensusVersion}",
@ -79,6 +83,10 @@ protobuf {
}
}
googleJavaFormat {
toolVersion '1.7'
}
// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
sourceSets {
main {

View file

@ -16,8 +16,8 @@
package hipstershop;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import hipstershop.Demo.Ad;
import hipstershop.Demo.AdRequest;
@ -26,49 +26,50 @@ import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.health.v1.HealthCheckResponse.ServingStatus;
import io.grpc.stub.StreamObserver;
import io.grpc.services.*;
import io.grpc.stub.StreamObserver;
import io.opencensus.common.Duration;
import io.opencensus.common.Scope;
import io.opencensus.contrib.grpc.metrics.RpcViews;
import io.opencensus.exporter.trace.jaeger.JaegerTraceExporter;
import io.opencensus.exporter.trace.logging.LoggingTraceExporter;
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration;
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;
import io.opencensus.exporter.trace.jaeger.JaegerTraceExporter;
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration;
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter;
import io.opencensus.trace.AttributeValue;
import io.opencensus.trace.Span;
import io.opencensus.trace.SpanBuilder;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.Tracing;
import io.opencensus.trace.samplers.Samplers;
import java.io.IOException;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public final class AdService {
public class AdService {
private static final Logger logger = LogManager.getLogger(AdService.class);
private static final Tracer tracer = Tracing.getTracer();
private int MAX_ADS_TO_SERVE = 2;
private static int MAX_ADS_TO_SERVE = 2;
private Server server;
private HealthStatusManager healthMgr;
static final AdService service = new AdService();
private static final AdService service = new AdService();
private void start() throws IOException {
int port = Integer.parseInt(System.getenv("PORT"));
healthMgr = new HealthStatusManager();
server = ServerBuilder.forPort(port).addService(new AdServiceImpl())
.addService(healthMgr.getHealthService()).build().start();
server =
ServerBuilder.forPort(port)
.addService(new AdServiceImpl())
.addService(healthMgr.getHealthService())
.build()
.start();
logger.info("Ad Service started, listening on " + port);
Runtime.getRuntime()
.addShutdownHook(
@ -91,26 +92,20 @@ public class AdService {
}
}
static class AdServiceImpl extends hipstershop.AdServiceGrpc.AdServiceImplBase {
private static class AdServiceImpl extends hipstershop.AdServiceGrpc.AdServiceImplBase {
/**
* Retrieves ads based on context provided in the request {@code AdRequest}.
*
* @param req the request containing context.
* @param responseObserver the stream observer which gets notified with the value of
* {@code AdResponse}
* @param responseObserver the stream observer which gets notified with the value of {@code
* AdResponse}
*/
@Override
public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
AdService service = AdService.getInstance();
Span parentSpan = tracer.getCurrentSpan();
SpanBuilder spanBuilder =
tracer
.spanBuilderWithExplicitParent("Retrieve Ads", parentSpan)
.setRecordEvents(true)
.setSampler(Samplers.alwaysSample());
try (Scope scope = spanBuilder.startScopedSpan()) {
Span span = tracer.getCurrentSpan();
Span span = tracer.getCurrentSpan();
try {
span.putAttribute("method", AttributeValue.stringAttributeValue("getAds"));
List<Ad> allAds = new ArrayList<>();
logger.info("received ad request (context_words=" + req.getContextKeysList() + ")");
@ -140,29 +135,29 @@ public class AdService {
responseObserver.onCompleted();
} catch (StatusRuntimeException e) {
logger.log(Level.WARN, "GetAds Failed", e.getStatus());
return;
responseObserver.onError(e);
}
}
}
static final ImmutableListMultimap<String, Ad> adsMap = createAdsMap();
private static final ImmutableListMultimap<String, Ad> adsMap = createAdsMap();
Collection<Ad> getAdsByCategory(String category) {
private Collection<Ad> getAdsByCategory(String category) {
return adsMap.get(category);
}
private static final Random random = new Random();
public List<Ad> getRandomAds() {
private List<Ad> getRandomAds() {
List<Ad> ads = new ArrayList<>(MAX_ADS_TO_SERVE);
Collection<Ad> allAds = adsMap.values();
for (int i=0; i<MAX_ADS_TO_SERVE; i++) {
for (int i = 0; i < MAX_ADS_TO_SERVE; i++) {
ads.add(Iterables.get(allAds, random.nextInt(allAds.size())));
}
return ads;
}
public static AdService getInstance() {
private static AdService getInstance() {
return service;
}
@ -173,21 +168,42 @@ public class AdService {
}
}
static ImmutableListMultimap<String, Ad> createAdsMap() {
Ad camera = Ad.newBuilder().setRedirectUrl("/product/2ZYFJ3GM2N")
.setText("Film camera for sale. 50% off.").build();
Ad lens = Ad.newBuilder().setRedirectUrl("/product/66VCHSJNUP")
.setText("Vintage camera lens for sale. 20% off.").build();
Ad recordPlayer = Ad.newBuilder().setRedirectUrl("/product/0PUK6V6EV0")
.setText("Vintage record player for sale. 30% off.").build();
Ad bike = Ad.newBuilder().setRedirectUrl("/product/9SIQT8TOJO")
.setText("City Bike for sale. 10% off.").build();
Ad baristaKit = Ad.newBuilder().setRedirectUrl("/product/1YMWWN1N4O")
.setText("Home Barista kitchen kit for sale. Buy one, get second kit for free").build();
Ad airPlant = Ad.newBuilder().setRedirectUrl("/product/6E92ZMYYFZ")
.setText("Air plants for sale. Buy two, get third one for free").build();
Ad terrarium = Ad.newBuilder().setRedirectUrl("/product/L9ECAV7KIM")
.setText("Terrarium for sale. Buy one, get second one for free").build();
private static ImmutableListMultimap<String, Ad> createAdsMap() {
Ad camera =
Ad.newBuilder()
.setRedirectUrl("/product/2ZYFJ3GM2N")
.setText("Film camera for sale. 50% off.")
.build();
Ad lens =
Ad.newBuilder()
.setRedirectUrl("/product/66VCHSJNUP")
.setText("Vintage camera lens for sale. 20% off.")
.build();
Ad recordPlayer =
Ad.newBuilder()
.setRedirectUrl("/product/0PUK6V6EV0")
.setText("Vintage record player for sale. 30% off.")
.build();
Ad bike =
Ad.newBuilder()
.setRedirectUrl("/product/9SIQT8TOJO")
.setText("City Bike for sale. 10% off.")
.build();
Ad baristaKit =
Ad.newBuilder()
.setRedirectUrl("/product/1YMWWN1N4O")
.setText("Home Barista kitchen kit for sale. Buy one, get second kit for free")
.build();
Ad airPlant =
Ad.newBuilder()
.setRedirectUrl("/product/6E92ZMYYFZ")
.setText("Air plants for sale. Buy two, get third one for free")
.build();
Ad terrarium =
Ad.newBuilder()
.setRedirectUrl("/product/L9ECAV7KIM")
.setText("Terrarium for sale. Buy one, get second one for free")
.build();
return ImmutableListMultimap.<String, Ad>builder()
.putAll("photography", camera, lens)
.putAll("vintage", camera, lens, recordPlayer)
@ -197,7 +213,7 @@ public class AdService {
.build();
}
public static void initStackdriver() {
private static void initStackdriver() {
logger.info("Initialize StackDriver");
long sleepTime = 10; /* seconds */
@ -205,7 +221,7 @@ public class AdService {
boolean statsExporterRegistered = false;
boolean traceExporterRegistered = false;
for (int i=0; i<maxAttempts; i++) {
for (int i = 0; i < maxAttempts; i++) {
try {
if (!traceExporterRegistered) {
StackdriverTraceExporter.createAndRegister(
@ -220,10 +236,12 @@ public class AdService {
statsExporterRegistered = true;
}
} catch (Exception e) {
if (i==(maxAttempts-1)) {
logger.log(Level.WARN, "Failed to register Stackdriver Exporter." +
" Tracing and Stats data will not reported to Stackdriver. Error message: " + e
.toString());
if (i == (maxAttempts - 1)) {
logger.log(
Level.WARN,
"Failed to register Stackdriver Exporter."
+ " Tracing and Stats data will not reported to Stackdriver. Error message: "
+ e.toString());
} else {
logger.info("Attempt to register Stackdriver Exporter in " + sleepTime + " seconds ");
try {
@ -237,7 +255,7 @@ public class AdService {
logger.info("StackDriver initialization complete.");
}
static void initJaeger() {
private static void initJaeger() {
String jaegerAddr = System.getenv("JAEGER_SERVICE_ADDR");
if (jaegerAddr != null && !jaegerAddr.isEmpty()) {
String jaegerUrl = String.format("http://%s/api/traces", jaegerAddr);
@ -251,19 +269,16 @@ public class AdService {
/** Main launches the server from the command line. */
public static void main(String[] args) throws IOException, InterruptedException {
// Add final keyword to pass checkStyle.
// Registers all RPC views.
RpcViews.registerAllViews();
RpcViews.registerAllGrpcViews();
// Registers logging trace exporter.
LoggingTraceExporter.register();
new Thread( new Runnable() {
public void run(){
initStackdriver();
}
}).start();
new Thread(
new Runnable() {
public void run() {
initStackdriver();
}
})
.start();
// Register Jaeger
initJaeger();

View file

@ -16,7 +16,6 @@
package hipstershop;
import hipstershop.Demo.Ad;
import hipstershop.Demo.AdRequest;
import hipstershop.Demo.AdResponse;
@ -26,34 +25,32 @@ import io.grpc.StatusRuntimeException;
import io.opencensus.common.Duration;
import io.opencensus.common.Scope;
import io.opencensus.contrib.grpc.metrics.RpcViews;
import io.opencensus.exporter.trace.logging.LoggingTraceExporter;
import io.opencensus.contrib.grpc.util.StatusConverter;
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration;
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration;
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter;
import io.opencensus.trace.SpanBuilder;
import io.opencensus.trace.Status.CanonicalCode;
import io.opencensus.trace.Span;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.Tracing;
import io.opencensus.trace.samplers.Samplers;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/** A simple client that requests ads from the Ads Service. */
public class AdServiceClient {
private static final Logger logger = LogManager.getLogger(AdServiceClient.class);
private static final Logger logger = LogManager.getLogger(AdServiceClient.class);
private static final Tracer tracer = Tracing.getTracer();
private final ManagedChannel channel;
private final hipstershop.AdServiceGrpc.AdServiceBlockingStub blockingStub;
/** Construct client connecting to Ad Service at {@code host:port}. */
public AdServiceClient(String host, int port) {
private AdServiceClient(String host, int port) {
this(
ManagedChannelBuilder.forAddress(host, port)
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
@ -63,12 +60,12 @@ public class AdServiceClient {
}
/** Construct client for accessing RouteGuide server using the existing channel. */
AdServiceClient(ManagedChannel channel) {
private AdServiceClient(ManagedChannel channel) {
this.channel = channel;
blockingStub = hipstershop.AdServiceGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
private void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
@ -78,28 +75,29 @@ public class AdServiceClient {
AdRequest request = AdRequest.newBuilder().addContextKeys(contextKey).build();
AdResponse response;
SpanBuilder spanBuilder =
tracer.spanBuilder("AdsClient").setRecordEvents(true).setSampler(Samplers.alwaysSample());
try (Scope scope = spanBuilder.startScopedSpan()) {
Span span =
tracer
.spanBuilder("AdsClient")
.setRecordEvents(true)
.setSampler(Samplers.alwaysSample())
.startSpan();
try (Scope scope = tracer.withSpan(span)) {
tracer.getCurrentSpan().addAnnotation("Getting Ads");
response = blockingStub.getAds(request);
tracer.getCurrentSpan().addAnnotation("Received response from Ads Service.");
} catch (StatusRuntimeException e) {
tracer
.getCurrentSpan()
.setStatus(
CanonicalCode.valueOf(e.getStatus().getCode().name())
.toStatus()
.withDescription(e.getMessage()));
logger.log(Level.WARN, "RPC failed: {0}", e.getStatus());
tracer.getCurrentSpan().setStatus(StatusConverter.fromGrpcStatus(e.getStatus()));
logger.log(Level.WARN, "RPC failed: " + e.getStatus());
return;
} finally {
span.end();
}
for(Ad ads: response.getAdsList()) {
for (Ad ads : response.getAdsList()) {
logger.info("Ads: " + ads.getText());
}
}
static int getPortOrDefaultFromArgs(String[] args, int index, int defaultPort) {
private static int getPortOrDefaultFromArgs(String[] args, int index, int defaultPort) {
int portNumber = defaultPort;
if (index < args.length) {
try {
@ -112,8 +110,7 @@ public class AdServiceClient {
return portNumber;
}
static String getStringOrDefaultFromArgs(
private static String getStringOrDefaultFromArgs(
String[] args, int index, @Nullable String defaultString) {
String s = defaultString;
if (index < args.length) {
@ -126,54 +123,46 @@ public class AdServiceClient {
* Ads Service Client main. If provided, the first element of {@code args} is the context key to
* get the ads from the Ads Service
*/
public static void main(String[] args) throws IOException, InterruptedException {
public static void main(String[] args) throws InterruptedException {
// Add final keyword to pass checkStyle.
final String contextKeys = getStringOrDefaultFromArgs(args, 0, "camera");
final String host = getStringOrDefaultFromArgs(args, 1, "localhost");
final int serverPort = getPortOrDefaultFromArgs(args, 2, 9555);
final String cloudProjectId = getStringOrDefaultFromArgs(args, 3, null);
//final int zPagePort = getPortOrDefaultFromArgs(args, 4, 3001);
// Registers all RPC views.
RpcViews.registerAllViews();
// Registers logging trace exporter.
LoggingTraceExporter.register();
RpcViews.registerAllGrpcViews();
// Registers Stackdriver exporters.
if (cloudProjectId != null) {
long sleepTime = 10; /* seconds */
int maxAttempts = 3;
long sleepTime = 10; /* seconds */
int maxAttempts = 3;
for (int i=0; i<maxAttempts; i++) {
try {
StackdriverTraceExporter.createAndRegister(
StackdriverTraceConfiguration.builder().setProjectId(cloudProjectId).build());
StackdriverStatsExporter.createAndRegister(
StackdriverStatsConfiguration.builder()
.setProjectId(cloudProjectId)
.setExportInterval(Duration.create(15, 0))
.build());
} catch (Exception e) {
if (i==(maxAttempts-1)) {
logger.log(Level.WARN, "Failed to register Stackdriver Exporter." +
" Tracing and Stats data will not reported to Stackdriver. Error message: " + e
.toString());
} else {
logger.info("Attempt to register Stackdriver Exporter in " + sleepTime + " seconds");
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(sleepTime));
} catch (Exception se) {
logger.log(Level.WARN, "Exception while sleeping" + e.toString());
}
for (int i = 0; i < maxAttempts; i++) {
try {
StackdriverTraceExporter.createAndRegister(StackdriverTraceConfiguration.builder().build());
StackdriverStatsExporter.createAndRegister(
StackdriverStatsConfiguration.builder()
.setExportInterval(Duration.create(15, 0))
.build());
} catch (Exception e) {
if (i == (maxAttempts - 1)) {
logger.log(
Level.WARN,
"Failed to register Stackdriver Exporter."
+ " Tracing and Stats data will not reported to Stackdriver. Error message: "
+ e.toString());
} else {
logger.info("Attempt to register Stackdriver Exporter in " + sleepTime + " seconds");
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(sleepTime));
} catch (Exception se) {
logger.log(Level.WARN, "Exception while sleeping" + e.toString());
}
}
}
}
// Register Prometheus exporters and export metrics to a Prometheus HTTPServer.
//PrometheusStatsCollector.createAndRegister();
// PrometheusStatsCollector.createAndRegister();
AdServiceClient client = new AdServiceClient(host, serverPort);
try {