Skip to content

Commit 366fbeb

Browse files
committed
Add OpenTelemetry Javaagent framework
This framework will wire up the OpenTelemetry Javaagent auto instrumentation. It leverages VCAP_SERVICES and the existence of a service binding with a specific name (`otel-collector`).
1 parent bb37d7c commit 366fbeb

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

config/components.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ frameworks:
6767
- "JavaBuildpack::Framework::MariaDbJDBC"
6868
- "JavaBuildpack::Framework::MetricWriter"
6969
- "JavaBuildpack::Framework::NewRelicAgent"
70+
- "JavaBuildpack::Framework::OpenTelemetryJavaagent"
7071
- "JavaBuildpack::Framework::PostgresqlJDBC"
7172
- "JavaBuildpack::Framework::RiverbedAppinternalsAgent"
7273
- "JavaBuildpack::Framework::SealightsAgent"

config/opentelemetry_javaagent.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright 2013-2023 the original author or authors.
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+
# Configuration for the OpenTelemetry Javaagent
17+
---
18+
version: +
19+
repository_root: https://raw.githubusercontent.com/open-telemetry/opentelemetry-java-instrumentation/cloudfoundry/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# OpenTelemetry Javaagent
2+
3+
The OpenTelemetry Javaagent buildpack framework will cause an application to be automatically instrumented
4+
with the [OpenTelemetry Javaagent Instrumentation](https://github.com/open-telemetry/opentelemetry-java-instrumentation).
5+
6+
Data will be sent directly to the OpenTelemetry Collector.
7+
8+
<table>
9+
<tr>
10+
<td><strong>Detection Criterion</strong></td>
11+
<td>Existence of a bound service containing the string <code>otel-collector</code></td>
12+
</tr>
13+
<tr>
14+
<td><strong>Tags</strong></td>
15+
<td><code>opentelemetry-javaagent=&lt;version&gt;</code></td>
16+
</tr>
17+
</table>
18+
19+
Tags are printed to standard output by the buildpack detect script
20+
21+
## User-Provided Service
22+
23+
Users are currently expected to provide their own "custom user provided service" (cups)
24+
instance and bind it to their application. The service MUST contain the string `otel-collector`.
25+
26+
### Choosing a version
27+
28+
Most users should skip this and simply use the latest version of the agent available (the default).
29+
To override the default and choose a specific version, you can use the `JBP_CONFIG_*` mechanism
30+
and set the `JBP_CONFIG_OPENTELEMETRY_JAVAAGENT` environment variable for your application.
31+
32+
For example, to use version 1.27.0 of the OpenTelemetry Javaagent Instrumentation, you
33+
could run:
34+
```
35+
$ cf set-env testapp JBP_CONFIG_OPENTELEMETRY_JAVAAGENT '{version: 1.27.0}'
36+
```
37+
38+
# Additional Resources
39+
40+
* [OpenTelemetry Javaagent Instrumentation](https://github.com/open-telemetry/opentelemetry-java-instrumentation) on GitHub
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
# Cloud Foundry Java Buildpack
4+
# Copyright 2013-2023 the original author or authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
require 'java_buildpack/component/versioned_dependency_component'
19+
require 'java_buildpack/framework'
20+
21+
module JavaBuildpack
22+
module Framework
23+
24+
# Main class for adding the OpenTelemetry Javaagent instrumentation
25+
class OpenTelemetryJavaagent < JavaBuildpack::Component::VersionedDependencyComponent
26+
27+
# (see JavaBuildpack::Component::BaseComponent#compile)
28+
def compile
29+
download_jar
30+
end
31+
32+
# (see JavaBuildpack::Component::BaseComponent#release)
33+
def release
34+
java_opts = @droplet.java_opts
35+
java_opts.add_javaagent(@droplet.sandbox + jar_name)
36+
37+
# Set the otel.service.name to the application_name
38+
app_name = @application.details['application_name']
39+
java_opts.add_system_property('otel.service.name', app_name)
40+
end
41+
42+
protected
43+
44+
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
45+
def supports?
46+
@application.services.one_service? REQUIRED_SERVICE_NAME_FILTER
47+
end
48+
49+
# bound service must contain the string `otel-collector`
50+
REQUIRED_SERVICE_NAME_FILTER = /otel-collector/.freeze
51+
52+
end
53+
end
54+
end
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
# Cloud Foundry Java Buildpack
4+
# Copyright 2013-2020 the original author or authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
require 'spec_helper'
19+
require 'component_helper'
20+
require 'java_buildpack/framework/opentelemetry_javaagent'
21+
require 'java_buildpack/util/tokenized_version'
22+
23+
describe JavaBuildpack::Framework::OpenTelemetryJavaagent do
24+
include_context 'with component help'
25+
26+
let(:configuration) { { 'version' => '1.27.0' } }
27+
let(:vcap_application) { { 'application_name' => 'GreatServiceTM' } }
28+
29+
it 'does not detect without otel-collector service bind' do
30+
expect(component.detect).to be_nil
31+
end
32+
33+
context 'when detected' do
34+
35+
before do
36+
allow(services).to receive(:one_service?).with(/otel-collector/).and_return(true)
37+
end
38+
39+
it 'detects with opentelemetry-javaagent' do
40+
expect(component.detect).to eq("opentelemetry-javaagent=#{version}")
41+
end
42+
43+
it 'downloads the opentelemetry javaagent jar', cache_fixture: 'stub-download.jar' do
44+
45+
component.compile
46+
47+
expect(sandbox + "opentelemetry_javaagent-#{version}.jar").to exist
48+
end
49+
50+
it 'updates JAVA_OPTS' do
51+
component.release
52+
53+
expect(java_opts).to include(
54+
"-javaagent:$PWD/.java-buildpack/opentelemetry_javaagent/opentelemetry_javaagent-#{version}.jar"
55+
)
56+
end
57+
58+
it 'sets the service name from the application name' do
59+
component.release
60+
61+
expect(java_opts).to include('-Dotel.service.name=GreatServiceTM')
62+
end
63+
64+
end
65+
66+
end

0 commit comments

Comments
 (0)