Skip to content

Commit

Permalink
Merge pull request #59 from honeycombio/ssharma-add-logs
Browse files Browse the repository at this point in the history
update adservice for logs
  • Loading branch information
sgsharma authored May 3, 2024
2 parents f130455 + b02353b commit b248210
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 61 deletions.
19 changes: 15 additions & 4 deletions kubernetes-manifests/adservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@ spec:
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: HONEYCOMB_API_ENDPOINT
- name: HONEYCOMB_API_KEY
valueFrom:
secretKeyRef:
name: honeycomb
key: api-key
- name: OTEL_EXPORTER_OTLP_TRACES_PROTOCOL
value: grpc
- name: OTEL_EXPORTER_OTLP_LOGS_PROTOCOL
value: grpc
- name: OTEL_TRACES_EXPORTER
value: otlp
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://opentelemetry-collector:4317
- name: HONEYCOMB_METRICS_DATASET
value: microservices-metrics
- name: SERVICE_NAME
- name: OTEL_LOGS_EXPORTER
value: otlp
- name: OTEL_SERVICE_NAME
value: adservice
- name: OTEL_RESOURCE_ATTRIBUTES
value: ip=$(POD_IP)
Expand Down
3 changes: 2 additions & 1 deletion src/adservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ RUN apt-get -y update && apt-get install -qqy \
&& rm -rf /var/lib/apt/lists/*

RUN mkdir -p /opt/otel && \
wget -q -O opt/otel/javaagent.jar https://github.com/honeycombio/honeycomb-opentelemetry-java/releases/download/v0.10.0/honeycomb-opentelemetry-javaagent-0.10.0.jar
wget -q -O /opt/otel/javaagent.jar https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar

WORKDIR /app
COPY --from=builder /app .

Expand Down
16 changes: 8 additions & 8 deletions src/adservice/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ description = 'Ad Service'
group = "adservice"
version = "0.1.0-SNAPSHOT"

def opentelemetryVersion = "1.9.1"
def grpcVersion = "1.43.2"
def jacksonVersion = "2.13.1"
def protocVersion = "3.19.2"
Expand All @@ -34,16 +33,17 @@ dependencies {
} else {
compileOnly group: "javax.annotation", name: "javax.annotation-api", version: "1.3.2"

compile "io.opentelemetry:opentelemetry-api:${opentelemetryVersion}",
"io.opentelemetry:opentelemetry-sdk:${opentelemetryVersion}",
"io.opentelemetry:opentelemetry-extension-annotations:${opentelemetryVersion}"

compile "io.opentelemetry:opentelemetry-api:1.37.0",
"io.opentelemetry:opentelemetry-sdk:1.37.0",
"io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:2.3.0"
implementation "com.google.api.grpc:proto-google-common-protos:1.17.0",
"io.grpc:grpc-protobuf:${grpcVersion}",
"io.grpc:grpc-stub:${grpcVersion}",
"io.grpc:grpc-netty:${grpcVersion}",
"io.grpc:grpc-services:${grpcVersion}",
"org.apache.logging.log4j:log4j-core:2.17.1"
"org.apache.logging.log4j:log4j-core:2.17.1",
"io.opentelemetry:opentelemetry-exporter-otlp:1.37.0"

runtimeOnly "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}",
"com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}",
Expand Down Expand Up @@ -95,15 +95,15 @@ task adService(type: CreateStartScripts) {
applicationName = 'AdService'
outputDir = new File(project.buildDir, 'tmp')
classpath = startScripts.classpath
defaultJvmOpts = ["-javaagent:/opt/otel/javaagent.jar"]//, "-Dotel.javaagent.debug=true"]
defaultJvmOpts = ["-javaagent:/opt/otel/javaagent.jar", "-Dotel.javaagent.debug=true"]
}

task adServiceClient(type: CreateStartScripts) {
mainClassName = 'msdemo.AdServiceClient'
applicationName = 'AdServiceClient'
outputDir = new File(project.buildDir, 'tmp')
classpath = startScripts.classpath
defaultJvmOpts = ["-javaagent:/opt/otel/javaagent.jar"]//, "-Dotel.javaagent.debug=true"]
defaultJvmOpts = ["-javaagent:/opt/otel/javaagent.jar", "-Dotel.javaagent.debug=true"]
}

applicationDistribution.into('bin') {
Expand Down
90 changes: 42 additions & 48 deletions src/adservice/src/main/java/msdemo/AdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.extension.annotations.WithSpan;
import io.opentelemetry.instrumentation.annotations.WithSpan;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -31,7 +31,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public final class AdService {

private static final Logger logger = LogManager.getLogger(AdService.class);
Expand All @@ -49,19 +48,19 @@ private void start() throws IOException {
healthMgr = new HealthStatusManager();

logger.info("Building server on " + port);
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(
new Thread(
() -> {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
// Use stderr here since the logger may have been reset by its JVM shutdown
// hook.
System.err.println(
"*** shutting down gRPC ads server since JVM is shutting down");
AdService.this.stop();
Expand All @@ -73,7 +72,7 @@ private void start() throws IOException {

private static float getRandomWaitTime(int max, int buckets) {
float num = 0;
float val = max / (float)buckets;
float val = max / (float) buckets;
for (int i = 0; i < buckets; i++) {
num += Math.random() * val;
}
Expand Down Expand Up @@ -112,12 +111,13 @@ private static class AdServiceImpl extends msdemo.AdServiceGrpc.AdServiceImplBas
* 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
* @param responseObserver the stream observer which gets notified with the
* value of {@code
* AdResponse}
*/
@Override
// Wrap function in an OpenTelemetry span
@WithSpan //results in a span name of AdServiceImpl.getAds
@WithSpan // results in a span name of AdServiceImpl.getAds
public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
AdService service = AdService.getInstance();

Expand Down Expand Up @@ -195,7 +195,8 @@ private static AdService getInstance() {
}

/**
* Await termination on the main thread since the grpc library uses daemon threads.
* Await termination on the main thread since the grpc library uses daemon
* threads.
*/
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
Expand All @@ -204,41 +205,34 @@ private void blockUntilShutdown() throws InterruptedException {
}

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();
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)
Expand Down

0 comments on commit b248210

Please sign in to comment.