Skip to content

Commit 25c747d

Browse files
authored
Bump to latest version and update Auto jar (#117)
* Bump otel versions and get things compiling. * Run spotless. * Disable spring example from build to make progress. * Add example autoinstrumentation docker image w/ our extension to the agent. * Fixes #107. Update client version to one that works w/ GKE metadata server. * Update readme for new auto library. * Update to otel 1.5.0 API/SDK and autoconf. * Add an example of using the autoconfiguration module in java to export traces + metrics. * Fixes from review.
1 parent a526e9a commit 25c747d

File tree

13 files changed

+399
-28
lines changed

13 files changed

+399
-28
lines changed

build.gradle

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ subprojects {
105105
autoServiceVersion = '1.0-rc7'
106106
autoValueVersion = '1.7.4'
107107
slf4jVersion = '1.7.30'
108-
googleCloudVersion = '1.3.0'
109-
cloudMonitoringVersion = '2.1.0'
110-
openTelemetryVersion = '1.4.1'
108+
googleCloudVersion = '2.0.5'
109+
googleTraceVersion = '2.0.0'
110+
cloudMonitoringVersion = '3.0.0'
111+
openTelemetryVersion = '1.5.0'
111112
openTelemetryInstrumentationVersion = '1.4.0'
112113
junitVersion = '4.13'
113114
mockitoVersion = '3.5.10'
@@ -124,8 +125,8 @@ subprojects {
124125
auto_value_annotations : "com.google.auto.value:auto-value-annotations:${autoValueVersion}",
125126
auto_value : "com.google.auto.value:auto-value:${autoValueVersion}",
126127
google_cloud_core : "com.google.cloud:google-cloud-core:${googleCloudVersion}",
127-
google_cloud_trace : "com.google.cloud:google-cloud-trace:${googleCloudVersion}",
128-
google_cloud_trace_grpc : "com.google.api.grpc:grpc-google-cloud-trace-v2:${googleCloudVersion}",
128+
google_cloud_trace : "com.google.cloud:google-cloud-trace:${googleTraceVersion}",
129+
google_cloud_trace_grpc : "com.google.api.grpc:grpc-google-cloud-trace-v2:2.0.1",
129130
google_cloud_monitoring : "com.google.cloud:google-cloud-monitoring:${cloudMonitoringVersion}",
130131
google_cloud_monitoring_grpc : "com.google.cloud:grpc-google-cloud-monitoring-v3:${cloudMonitoringVersion}",
131132
google_cloud_pubsub : "com.google.cloud:google-cloud-pubsub:${pubSubVersion}",
@@ -142,6 +143,9 @@ subprojects {
142143
spring_cloud_starter_openfeign : "org.springframework.cloud:spring-cloud-starter-openfeign:${springOpenFeignVersion}",
143144
spring_cloud_sleuth_otel_autoconf: "org.springframework.cloud:spring-cloud-sleuth-otel-autoconfigure:${springOtelVersion}",
144145
]
146+
agentLibraries = [
147+
agent_all: "io.opentelemetry.javaagent:opentelemetry-javaagent:${openTelemetryInstrumentationVersion}:all",
148+
]
145149
testLibraries = [
146150
junit : "junit:junit:${junitVersion}",
147151
mockito : "org.mockito:mockito-inline:${mockitoVersion}",

examples/autoconf/build.gradle

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2021 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
plugins {
17+
id 'java'
18+
id 'application'
19+
id 'com.google.cloud.tools.jib'
20+
}
21+
22+
23+
description = 'Examples for using java autoconfiguration and Google Cloud Operations'
24+
25+
dependencies {
26+
compile(libraries.opentelemetry_api)
27+
compile(libraries.opentelemetry_api_metrics)
28+
compile(libraries.opentelemetry_sdk_metrics)
29+
runtimeOnly(libraries.opentelemetry_sdk_autoconf)
30+
runtimeOnly(libraries.opentelemetry_sdk)
31+
runtimeOnly project(':exporter-auto')
32+
runtimeOnly project(':detector-resources')
33+
}
34+
35+
// Autoconfiguraation settings.
36+
// See: https://github.com/open-telemetry/opentelemetry-java/tree/main/sdk-extensions/autoconfigure
37+
def autoconf_config = [
38+
'-Dotel.traces.exporter=google_cloud_trace',
39+
'-Dotel.metrics.exporter=google_cloud_monitoring',
40+
]
41+
42+
mainClassName = 'com.google.cloud.opentelemetry.example.autoconf.AutoconfExample'
43+
44+
application {
45+
applicationDefaultJvmArgs = autoconf_config
46+
}
47+
48+
49+
jib {
50+
from.image = 'gcr.io/distroless/java-debian10:11'
51+
containerizingMode = 'packaged'
52+
container.jvmFlags = autoconf_config
53+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2021 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.opentelemetry.example.autoconf;
17+
18+
import io.opentelemetry.api.GlobalOpenTelemetry;
19+
import io.opentelemetry.api.common.AttributeKey;
20+
import io.opentelemetry.api.common.Attributes;
21+
import io.opentelemetry.api.metrics.GlobalMeterProvider;
22+
import io.opentelemetry.api.metrics.LongCounter;
23+
import io.opentelemetry.api.metrics.Meter;
24+
import io.opentelemetry.api.trace.Span;
25+
import io.opentelemetry.api.trace.Tracer;
26+
import io.opentelemetry.context.Scope;
27+
import io.opentelemetry.sdk.metrics.export.IntervalMetricReader;
28+
import java.util.Objects;
29+
import java.util.Random;
30+
31+
public class AutoconfExample {
32+
private static final Random random = new Random();
33+
private static final AttributeKey<String> DESCRIPTION_KEY = AttributeKey.stringKey("description");
34+
35+
private final Tracer tracer = GlobalOpenTelemetry.get().tracerBuilder("example-auto").build();
36+
private final Meter meter = GlobalMeterProvider.get().meterBuilder("example-auto").build();
37+
private final LongCounter useCaseCount = meter.counterBuilder("use_case").build();
38+
39+
private void myUseCase(String description) {
40+
Span span = tracer.spanBuilder(description).startSpan();
41+
try (Scope scope = span.makeCurrent()) {
42+
useCaseCount.add(1, Attributes.of(DESCRIPTION_KEY, description));
43+
span.addEvent("Event A");
44+
// Do some work for the use case
45+
for (int i = 0; i < 3; i++) {
46+
String work = String.format("%s - Work #%d", description, (i + 1));
47+
doWork(work);
48+
}
49+
50+
span.addEvent("Event B");
51+
} finally {
52+
span.end();
53+
}
54+
}
55+
56+
private void doWork(String description) {
57+
// Child span
58+
Span span = tracer.spanBuilder(description).startSpan();
59+
try (Scope scope = span.makeCurrent()) {
60+
// Simulate work: this could be simulating a network request or an expensive disk operation
61+
Thread.sleep(100 + random.nextInt(5) * 100);
62+
} catch (InterruptedException e) {
63+
throw new RuntimeException(e);
64+
} finally {
65+
span.end();
66+
}
67+
}
68+
69+
public static void main(String[] args) {
70+
// First, make sure we've configured opentelemetry with autoconfigure module.
71+
Objects.requireNonNull(GlobalOpenTelemetry.get(), "Failed to autoconfigure opentelemetry");
72+
Objects.requireNonNull(
73+
GlobalMeterProvider.get(), "Failed to autoconfigure opentelmetry metrics");
74+
75+
AutoconfExample example = new AutoconfExample();
76+
// Application-specific logic
77+
example.myUseCase("One");
78+
example.myUseCase("Two");
79+
80+
// Autoconf module registers shutdown hooks to flush telemetry, but metrics should be forced
81+
// for short-lived processes.
82+
IntervalMetricReader.forceFlushGlobal();
83+
}
84+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2021 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
plugins {
17+
id 'java'
18+
id 'com.github.johnrengelman.shadow'
19+
id 'com.google.cloud.tools.jib'
20+
}
21+
22+
23+
description = 'Examples for using java auto instrumentation and Google Cloud Operations'
24+
25+
configurations {
26+
agent
27+
}
28+
29+
dependencies {
30+
agent agentLibraries.agent_all
31+
agent project(path: ':exporter-auto', configuration: 'shadow')
32+
33+
implementation platform("io.grpc:grpc-bom:1.33.1")
34+
implementation platform("io.opentelemetry:opentelemetry-bom:1.0.0")
35+
implementation platform("io.opentelemetry:opentelemetry-bom-alpha:1.0.0-alpha")
36+
implementation platform("org.apache.logging.log4j:log4j-bom:2.13.2")
37+
38+
implementation "io.grpc:grpc-netty-shaded"
39+
implementation "io.grpc:grpc-protobuf"
40+
implementation "io.grpc:grpc-services"
41+
implementation "io.grpc:grpc-stub"
42+
implementation "io.opentelemetry:opentelemetry-proto"
43+
implementation "io.opentelemetry:opentelemetry-extension-annotations"
44+
implementation "org.apache.logging.log4j:log4j-core"
45+
46+
runtimeOnly("org.apache.logging.log4j:log4j-slf4j-impl")
47+
}
48+
49+
task copyAgent(type: Copy) {
50+
into layout.buildDirectory.dir("otelagent")
51+
from configurations.agent {
52+
rename "opentelemetry-javaagent(.*).jar", "otel_agent.jar"
53+
rename "exporter-auto(.*).jar", "gcp_ext.jar"
54+
}
55+
}
56+
57+
jib {
58+
from.image = 'gcr.io/distroless/java-debian10:11'
59+
containerizingMode = 'packaged'
60+
container.ports = ["8080"]
61+
extraDirectories {
62+
paths {
63+
path {
64+
from = layout.buildDirectory.dir("otelagent")
65+
into = '/otelagent'
66+
}
67+
}
68+
}
69+
// TODO: Figure out how to pull versions from build.
70+
container.jvmFlags = [
71+
// Use the downloaded java agent.
72+
'-javaagent:/otelagent/otel_agent.jar',
73+
// Use the GCP exporter extensions.
74+
'-Dotel.javaagent.experimental.extensions=/otelagent/gcp_ext.jar',
75+
// Configure auto instrumentation.
76+
'-Dotel.traces.exporter=google_cloud_trace',
77+
'-Dotel.metrics.exporter=google_cloud_monitoring',
78+
]
79+
}
80+
81+
tasks.named('jib').configure {
82+
dependsOn copyAgent
83+
}
84+
85+
tasks.named('jibDockerBuild').configure {
86+
dependsOn copyAgent
87+
}
88+
89+
tasks.named('jibBuildTar').configure {
90+
dependsOn copyAgent
91+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2021 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.example;
17+
18+
import io.grpc.Server;
19+
import io.grpc.ServerBuilder;
20+
import io.grpc.protobuf.services.ProtoReflectionService;
21+
import org.apache.logging.log4j.LogManager;
22+
import org.apache.logging.log4j.Logger;
23+
24+
/** Creates a gRPC service on port 8080 that will be in our container. */
25+
public class TestMain {
26+
27+
private static final Logger logger = LogManager.getLogger();
28+
29+
public static void main(String[] args) throws Exception {
30+
TestService service = new TestService();
31+
Server server =
32+
ServerBuilder.forPort(8080)
33+
.addService(ProtoReflectionService.newInstance())
34+
.addService(service)
35+
.directExecutor()
36+
.build()
37+
.start();
38+
Runtime.getRuntime().addShutdownHook(new Thread(server::shutdownNow));
39+
logger.info("Server started at port 8080.");
40+
server.awaitTermination();
41+
}
42+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2021 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.example;
17+
18+
import io.grpc.stub.StreamObserver;
19+
import io.opentelemetry.extension.annotations.WithSpan;
20+
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
21+
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceResponse;
22+
import io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc;
23+
import org.apache.logging.log4j.LogManager;
24+
import org.apache.logging.log4j.Logger;
25+
26+
/**
27+
* A service which uses OTLP's grpc services so we don't need to set up new gRPC protocol for
28+
* testing.
29+
*/
30+
public class TestService extends TraceServiceGrpc.TraceServiceImplBase {
31+
32+
private static final Logger logger = LogManager.getLogger();
33+
34+
@Override
35+
public void export(
36+
ExportTraceServiceRequest request,
37+
StreamObserver<ExportTraceServiceResponse> responseObserver) {
38+
logger.info("Request received");
39+
methodCallWithSpan();
40+
responseObserver.onNext(ExportTraceServiceResponse.getDefaultInstance());
41+
responseObserver.onCompleted();
42+
}
43+
44+
@WithSpan
45+
public String methodCallWithSpan() {
46+
return "My additional span";
47+
}
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} trace_id=%X{trace_id} span_id=%X{span_id} - %msg%n"/>
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="info">
10+
<AppenderRef ref="Console"/>
11+
</Root>
12+
</Loggers>
13+
</Configuration>

examples/metrics/src/main/java/com/google/cloud/opentelemetry/example/metrics/MetricsExporterExample.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public class MetricsExporterExample {
2929
private static SdkMeterProvider METER_PROVIDER =
3030
SdkMeterProvider.builder().buildAndRegisterGlobal();
3131
private static final Meter METER =
32-
METER_PROVIDER.get("instrumentation-library-name", "semver:1.0.0");
32+
METER_PROVIDER
33+
.meterBuilder("instrumentation-library-name")
34+
.setInstrumentationVersion("semver:1.0.0")
35+
.build();
3336
private static final Random RANDOM = new Random();
3437
private static io.opentelemetry.sdk.metrics.export.MetricExporter metricExporter;
3538
private static IntervalMetricReader intervalMetricReader;
@@ -54,7 +57,7 @@ private static void setupMetricExporter() {
5457
private static void myUseCase() {
5558
LongCounter counter =
5659
METER
57-
.longCounterBuilder("example_counter")
60+
.counterBuilder("example_counter")
5861
.setDescription("Processed jobs")
5962
.setUnit("1")
6063
.build();

0 commit comments

Comments
 (0)