Skip to content

Commit 90b4303

Browse files
Instana Agent Implementation
Instana Agent Implementation containing necessary framework code and tests.
1 parent 1ca7a11 commit 90b4303

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

config/components.yml

+1
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ frameworks:
8080
- "JavaBuildpack::Framework::TakipiAgent"
8181
- "JavaBuildpack::Framework::JavaSecurity"
8282
- "JavaBuildpack::Framework::JavaOpts"
83+
- "JavaBuildpack::Framework::InstanaAgent"

docs/framework-instana_agent.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Instana Agent Framework
2+
[IBM Instana](https://www.ibm.com/products/instana) is the only real-time full-stack observability solution: zero sample tracing
3+
4+
The Instana Agent Framework causes an application to be automatically configured to work with a bound Instana instance
5+
6+
<table>
7+
<tr>
8+
<td><strong>Detection Criterion</strong></td><td>Existence of a single bound Instana service.
9+
<ul>
10+
<li>Existence of a Instana service is defined as the <a href="http://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#VCAP-SERVICES"><code>VCAP_SERVICES</code></a> payload containing a service who's name, label or tag has <code>instana</code> as a substring with at least `agentkey` and `endpointurl` set as credentials.</li>
11+
</ul>
12+
</td>
13+
</tr>
14+
<tr>
15+
<td><strong>Tags</strong></td>
16+
<td><tt>instana-agent=&lt;version&gt;</tt></td>
17+
</tr>
18+
</table>
19+
Tags are printed to standard output by the buildpack detect script
20+
21+
## User-Provided Service
22+
Users must provide their own Instana service. A user-provided Instana service must have a name or tag with `instana` in it so that the Instana Framework will automatically configure the application to work with the service.
23+
24+
The credential payload of the service may contain the following entries:
25+
26+
| Name | Description
27+
| ---- | -----------
28+
| `agentkey` | The agent key is used to create a relationship between the monitoring agent and the environment that it belongs to.
29+
| `endpointurl` | This environment variable is your serverless monitoring endpoint. Make sure that you use the correct value for your region that starts with https://serverless-.
30+
31+
## Configuration
32+
For general information on configuring the buildpack, including how to specify configuration values through environment variables, refer to [Configuration and Extension][].
33+
34+
[Configuration and Extension]: ../README.md#configuration-and-extension
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# frozen_string_literal: true
2+
3+
# Cloud Foundry Java Buildpack
4+
# Copyright 2024 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+
module JavaBuildpack
19+
module Framework
20+
# Encapsulates the functionality for enabling Instana support.
21+
class InstanaAgent < JavaBuildpack::Component::VersionedDependencyComponent
22+
23+
# Creates an instance
24+
#
25+
# @param [Hash] context a collection of utilities used the component
26+
def initialize(context)
27+
@application = context[:application]
28+
@component_name = self.class.to_s.space_case
29+
@configuration = context[:configuration]
30+
@droplet = context[:droplet]
31+
32+
@version, @uri = standalone_agent_download_url if supports?
33+
@logger = JavaBuildpack::Logging::LoggerFactory.instance.get_logger InstanaAgent
34+
end
35+
36+
# (see JavaBuildpack::Component::BaseComponent#compile)
37+
def compile
38+
download_jar
39+
rescue StandardError => e
40+
@logger.error('Instana Download failed :' + e.to_s)
41+
end
42+
43+
# (see JavaBuildpack::Component::BaseComponent#release)
44+
def release
45+
@droplet.java_opts.add_javaagent(agent_path)
46+
setup_variables
47+
end
48+
49+
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
50+
def supports?
51+
@application.services.one_service? FILTER, AGENT_KEY, ENDPOINT_URL
52+
end
53+
54+
# Provides the Agent Path
55+
def agent_path
56+
@droplet.sandbox + jar_name
57+
end
58+
59+
# Provides the Credentials
60+
def credentials
61+
@application.services.find_service(FILTER, AGENT_KEY, ENDPOINT_URL)['credentials']
62+
end
63+
64+
private
65+
66+
FILTER = /instana/.freeze
67+
AGENT_KEY = 'agentkey'
68+
ENDPOINT_URL = 'endpointurl'
69+
INSTANA_AGENT_KEY = 'INSTANA_AGENT_KEY'
70+
INSTANA_ENDPOINT_URL = 'INSTANA_ENDPOINT_URL'
71+
INSTANA_BASE_URL = 'artifact-public.instana.io/artifactory'
72+
INSTANA_COLLECTOR_PATH = 'rel-generic-instana-virtual/com/instana/standalone-collector-jvm'
73+
74+
def standalone_agent_download_url
75+
download_uri = "https://_:#{credentials[AGENT_KEY]}@#{INSTANA_BASE_URL}/#{INSTANA_COLLECTOR_PATH}/%5BRELEASE%5D/standalone-collector-jvm-%5BRELEASE%5D.jar"
76+
['latest', download_uri]
77+
end
78+
79+
def setup_variables
80+
environment_variables = @droplet.environment_variables
81+
environment_variables
82+
.add_environment_variable(INSTANA_AGENT_KEY, credentials[AGENT_KEY])
83+
.add_environment_variable(INSTANA_ENDPOINT_URL, credentials[ENDPOINT_URL])
84+
end
85+
86+
end
87+
end
88+
end

spec/fixtures/stub-instana-agent.jar

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# frozen_string_literal: true
2+
3+
# Cloud Foundry Java Buildpack
4+
# Copyright 2024 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/instana_agent'
21+
require 'java_buildpack/util/tokenized_version'
22+
23+
describe JavaBuildpack::Framework::InstanaAgent do
24+
include_context 'with component help'
25+
26+
it 'does not detect without instana service' do
27+
expect(component.detect).to be_nil
28+
end
29+
30+
context do
31+
32+
before do
33+
allow(services).to receive(:one_service?).with(/instana/, 'agentkey', 'endpointurl').and_return(true)
34+
allow(services).to receive(:find_service).and_return('credentials' => { 'agentkey' => 'test-akey',
35+
'endpointurl' => 'test-epurl' })
36+
37+
allow(application_cache).to receive(:get)
38+
.with('https://_:[email protected]/artifactory/rel-generic-instana-virtual/com/instana/standalone-collector-jvm/%5BRELEASE%5D/standalone-collector-jvm-%5BRELEASE%5D.jar')
39+
.and_yield(Pathname.new('spec/fixtures/stub-instana-agent.jar').open, false)
40+
end
41+
42+
it 'detects with instana service' do
43+
expect(component.detect).to eq('instana-agent=latest')
44+
end
45+
46+
it 'downloads instana agent jar',
47+
cache_fixture: 'stub-instana-agent.jar' do
48+
49+
component.compile
50+
expect(sandbox + 'instana_agent-latest.jar').to exist
51+
end
52+
53+
it 'sets Java Agent with Instana Agent' do
54+
55+
component.release
56+
57+
expect(java_opts).to include('-javaagent:$PWD/.java-buildpack/instana_agent/instana_agent-latest.jar')
58+
end
59+
60+
it 'updates environment variables' do
61+
62+
component.release
63+
64+
expect(environment_variables).to include('INSTANA_AGENT_KEY=test-akey')
65+
expect(environment_variables).to include('INSTANA_ENDPOINT_URL=test-epurl')
66+
end
67+
68+
end
69+
70+
end

0 commit comments

Comments
 (0)