Skip to content

Commit 29cc897

Browse files
Merge pull request #1020 from tylerbenson/tyler/otel
Add OpenTelemetry Javaagent framework
2 parents 6716638 + ab6bb69 commit 29cc897

File tree

6 files changed

+199
-2
lines changed

6 files changed

+199
-2
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/open_telemetry_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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 `create-user-provided-service` (cups) of the collector
24+
and bind it to their application. The service MUST contain the string `otel-collector`.
25+
26+
For example, to create a service named `otel-collector` that represents an environment named `cf-demo`, you could use the following commands:
27+
28+
```
29+
$ cf cups otel-collector -p '{"otel.resource.attributes": "deployment.environment=cf-demo"}'
30+
$ cf bind-service myApp otel-collector
31+
$ cf restage myApp
32+
```
33+
34+
### Choosing a version
35+
36+
Most users should skip this and simply use the latest version of the agent available (the default).
37+
To override the default and choose a specific version, you can use the `JBP_CONFIG_*` mechanism
38+
and set the `JBP_CONFIG_OPENTELEMETRY_JAVAAGENT` environment variable for your application.
39+
40+
For example, to use version 1.27.0 of the OpenTelemetry Javaagent Instrumentation, you
41+
could run:
42+
```
43+
$ cf set-env testapp JBP_CONFIG_OPENTELEMETRY_JAVAAGENT '{version: 1.27.0}'
44+
```
45+
46+
# Additional Resources
47+
48+
* [OpenTelemetry Javaagent Instrumentation](https://github.com/open-telemetry/opentelemetry-java-instrumentation) on GitHub

docs/framework-splunk_otel_java_agent.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Tags are printed to standard output by the buildpack detect script
2020

2121
## User-Provided Service
2222

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 `splunk-o11y`.
23+
Users are currently expected to `create-user-provided-service` (cups) of the collector
24+
and bind it to their application. The service MUST contain the string `splunk-o11y`.
2525

2626
For example, to create a service named `splunk-o11y` that represents Observability Cloud
2727
realm `us0` and represents a user environment named `cf-demo`, you could use the following
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
credentials = @application.services.find_service(REQUIRED_SERVICE_NAME_FILTER)['credentials']
38+
# Add all otel.* credentials from the service bind as jvm system properties
39+
credentials&.each do |key, value|
40+
java_opts.add_system_property(key, value) if key.start_with?('otel.')
41+
end
42+
43+
# Set the otel.service.name to the application_name if not specified in credentials
44+
return if credentials.key? 'otel.service.name'
45+
46+
# Set the otel.service.name to the application_name
47+
app_name = @application.details['application_name']
48+
java_opts.add_system_property('otel.service.name', app_name)
49+
end
50+
51+
protected
52+
53+
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
54+
def supports?
55+
@application.services.one_service? REQUIRED_SERVICE_NAME_FILTER
56+
end
57+
58+
# bound service must contain the string `otel-collector`
59+
REQUIRED_SERVICE_NAME_FILTER = /otel-collector/.freeze
60+
61+
end
62+
end
63+
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/open_telemetry_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("open-telemetry-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 + "open_telemetry_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/open_telemetry_javaagent/open_telemetry_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)