Skip to content

Commit b6e55e5

Browse files
Merge pull request #968 from breedx-splk/add_splunk_java_agent
Introduce the splunk-otel-java-agent framework.
2 parents 858b724 + 71b5948 commit b6e55e5

File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed

config/components.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ frameworks:
7171
- "JavaBuildpack::Framework::SealightsAgent"
7272
- "JavaBuildpack::Framework::SeekerSecurityProvider"
7373
- "JavaBuildpack::Framework::SpringAutoReconfiguration"
74+
- "JavaBuildpack::Framework::SplunkOtelJavaAgent"
7475
- "JavaBuildpack::Framework::SpringInsight"
7576
- "JavaBuildpack::Framework::SkyWalkingAgent"
7677
- "JavaBuildpack::Framework::YourKitProfiler"

config/splunk_otel_java_agent.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright 2013-2020 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 Splunk Distribution of OpenTelemetry Java Instrumentation
17+
# See https://github.com/signalfx/splunk-otel-java for more information
18+
---
19+
version: +
20+
repository_root: https://raw.githubusercontent.com/signalfx/splunk-otel-java/main/deployments/cloudfoundry/
21+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Splunk Distribution of OpenTelemetry Java Instrumentation
2+
3+
The Splunk OpenTelemetry Java Agent buildpack framework will cause an application to be automatically instrumented
4+
with the [Splunk distribution of OpenTelemetry Java Instrumentation](https://github.com/signalfx/splunk-otel-java).
5+
6+
Trace data will be sent directly to Splunk Observability Cloud.
7+
8+
<table>
9+
<tr>
10+
<td><strong>Detection Criterion</strong></td>
11+
<td>Existence of a bound service containing the string <code>splunk-o11y</code></td>
12+
</tr>
13+
<tr>
14+
<td><strong>Tags</strong></td>
15+
<td><code>splunk-otel-java-agent=&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 `splunk-o11y`.
25+
26+
For example, to create a service named `splunk-o11y` that represents Observability Cloud
27+
realm `us0` and represents a user environment named `cf-demo`, you could use the following
28+
commands:
29+
30+
```
31+
$ cf cups splunk-o11y -p \
32+
'{"splunk.realm": "us0", "splunk.access.token": "<redacted>", "otel.resource.attributes": "deployment.environment=cf-demo"}'
33+
$ cf bind-service myApp splunk-o11y
34+
$ cf restage myApp
35+
```
36+
37+
The `credential` field of the service should provide these entries:
38+
39+
| Name | Required? | Description
40+
|------------------------|-----------| -----------
41+
| `splunk.access.token` | Yes | The Splunk [org access token](https://docs.splunk.com/observability/admin/authentication-tokens/org-tokens.html).
42+
| `splunk.realm` | Yes | The Splunk realm where data will be sent. This is commonly `us0` or `eu0` etc.
43+
| `otel.*` or `splunk.*` | Optional | All additional credentials starting with these prefixes will be appended to the application's JVM arguments as system properties.
44+
45+
### Choosing a version
46+
47+
Most users should skip this and simply use the latest version of the agent available (the default).
48+
To override the default and choose a specific version, you can use the `JBP_CONFIG_*` mechanism
49+
and set the `JBP_CONFIG_SPLUNK_OTEL_JAVA_AGENT` environment variable for your application.
50+
51+
For example, to use version 1.16.0 of the Splunk OpenTelemetry Java Instrumentation, you
52+
could run:
53+
```
54+
$ cf set-env testapp JBP_CONFIG_SPLUNK_OTEL_JAVA_AGENT '{version: 1.16.0}'
55+
```
56+
57+
# Additional Resources
58+
59+
* [Splunk Observability](https://www.splunk.com/en_us/products/observability.html)
60+
* [Splunk Distribution of OpenTelemetry Java](https://github.com/signalfx/splunk-otel-java) on GitHub
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 '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 Splunk OpenTelemetry instrumentation agent
25+
class SplunkOtelJavaAgent < 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.* and splunk.* 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?('splunk.') || 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+
app_name = @application.details['application_name']
47+
java_opts.add_system_property('otel.service.name', app_name)
48+
end
49+
50+
protected
51+
52+
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
53+
def supports?
54+
@application.services.one_service? REQUIRED_SERVICE_NAME_FILTER
55+
end
56+
57+
# bound service must contain the string `splunk-o11y`
58+
REQUIRED_SERVICE_NAME_FILTER = /splunk-o11y/.freeze
59+
60+
end
61+
end
62+
end
341 Bytes
Binary file not shown.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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/splunk_otel_java_agent'
21+
require 'java_buildpack/util/tokenized_version'
22+
23+
describe JavaBuildpack::Framework::SplunkOtelJavaAgent do
24+
include_context 'with component help'
25+
26+
let(:configuration) { { 'version' => '1.16.0' } }
27+
let(:vcap_application) { { 'application_name' => 'GreatServiceTM' } }
28+
29+
it 'does not detect without splunk-o11y 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(/splunk-o11y/).and_return(true)
37+
end
38+
39+
it 'detects with splunk-otel-java' do
40+
expect(component.detect).to eq("splunk-otel-java-agent=#{version}")
41+
end
42+
43+
it 'downloads the splunk otel javaagent jar', cache_fixture: 'stub-splunk-otel-javaagent.jar' do
44+
45+
component.compile
46+
47+
expect(sandbox + "splunk_otel_java_agent-#{version}.jar").to exist
48+
end
49+
50+
it 'updates JAVA_OPTS' do
51+
allow(services).to receive(:find_service).and_return('credentials' => { 'splunk.access.token' => 'sekret',
52+
'ignored' => 'not used',
53+
'otel.foo' => 'bar' })
54+
component.release
55+
56+
expect(java_opts).to include(
57+
"-javaagent:$PWD/.java-buildpack/splunk_otel_java_agent/splunk_otel_java_agent-#{version}.jar"
58+
)
59+
expect(java_opts).to include('-Dsplunk.access.token=sekret')
60+
expect(java_opts).to include('-Dotel.foo=bar')
61+
end
62+
63+
it 'sets the service name from the application name' do
64+
allow(services).to receive(:find_service).and_return('credentials' => { 'splunk.access.token' => 'sekret' })
65+
66+
component.release
67+
68+
expect(java_opts).to include('-Dotel.service.name=GreatServiceTM')
69+
end
70+
71+
it 'prefers credentials over application_name for service name' do
72+
creds = { 'credentials' => { 'otel.service.name' => 'sweet', 'splunk.access.token' => 'sekret' } }
73+
allow(services).to receive(:find_service).and_return(creds)
74+
75+
component.release
76+
77+
expect(java_opts).to include('-Dotel.service.name=sweet')
78+
end
79+
80+
end
81+
82+
end

0 commit comments

Comments
 (0)