Skip to content

Commit da88bd5

Browse files
Merge pull request #981 from mayrstefan/issue-980
Add connection string for Azure Application Insights Agent 3.x
2 parents ba66915 + c455f27 commit da88bd5

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ The buildpack supports extension through the use of Git repository forking. The
112112
* Standard Frameworks
113113
* [AppDynamics Agent](docs/framework-app_dynamics_agent.md) ([Configuration](docs/framework-app_dynamics_agent.md#configuration))
114114
* [AspectJ Weaver Agent](docs/framework-aspectj_weaver_agent.md) ([Configuration](docs/framework-aspectj_weaver_agent.md#configuration))
115+
* [Azure Application Insights Agent](docs/framework-azure_application_insights_agent.md) ([Configuration](docs/framework-azure_application_insights_agent.md#configuration))
115116
* [Checkmarx IAST Agent](docs/framework-checkmarx_iast_agent.md) ([Configuration](docs/framework-checkmarx_iast_agent.md#configuration))
116117
* [Client Certificate Mapper](docs/framework-client_certificate_mapper.md) ([Configuration](docs/framework-client_certificate_mapper.md#configuration))
117118
* [Container Customizer](docs/framework-container_customizer.md) ([Configuration](docs/framework-container_customizer.md#configuration))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Azure Application Insights Agent Framework
2+
The Azure Application Insights Agent Framework causes an application to be automatically configured to work with a bound [Azure Application Insights Service][]. **Note:** This framework is disabled by default.
3+
4+
<table>
5+
<tr>
6+
<td><strong>Detection Criterion</strong></td><td>Existence of a single bound Azure Application Insights service.
7+
<ul>
8+
<li>Existence of a Azure Application Insights 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>azure-application-insights</code> as a substring with at least `connection_string` or `instrumentation_key` set as credentials.</li>
9+
</ul>
10+
</td>
11+
</tr>
12+
<tr>
13+
<td><strong>Tags</strong></td>
14+
<td><tt>azure-application-insights=&lt;version&gt;</tt></td>
15+
</tr>
16+
</table>
17+
Tags are printed to standard output by the buildpack detect script
18+
19+
## User-Provided Service
20+
Users must provide their own Azure Application Insights service. A user-provided Azure Application Insights service must have a name or tag with `azure-application-insights` in it so that the Azure Application Insights Agent Framework Framework will automatically configure the application to work with the service.
21+
22+
The credential payload of the service has to contain one of the following entries:
23+
24+
| Name | Description
25+
| ---- | -----------
26+
| `connection_string` | With agent version 3.x the connection string is required. You can find your connection string in your Application Insights resource.
27+
| `instrumentation_key` | With agent version 2.x the instrumentation key is required. With version 3.x this configuration is deprecated an it is recommended to switch to a connection string. You can find your instrumentation key in your Application Insights resource.
28+
29+
## Configuration
30+
For general information on configuring the buildpack, including how to specify configuration values through environment variables, refer to [Configuration and Extension][].
31+
32+
[Configuration and Extension]: ../README.md#configuration-and-extension
33+
[Azure Application Insights Service]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-in-process-agent

lib/java_buildpack/framework/azure_application_insights_agent.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,36 @@ def compile
3333

3434
# (see JavaBuildpack::Component::BaseComponent#release)
3535
def release
36-
credentials = @application.services.find_service(FILTER, INSTRUMENTATION_KEY)['credentials']
36+
credentials = @application.services.find_service(FILTER, [CONNECTION_STRING, INSTRUMENTATION_KEY])['credentials']
3737

38-
@droplet
39-
.java_opts.add_javaagent(@droplet.sandbox + jar_name)
40-
.add_system_property('APPLICATION_INSIGHTS_IKEY', credentials[INSTRUMENTATION_KEY])
38+
if credentials.key?(CONNECTION_STRING)
39+
@droplet.java_opts.add_system_property('applicationinsights.connection.string',
40+
credentials[CONNECTION_STRING])
41+
end
42+
if credentials.key?(INSTRUMENTATION_KEY)
43+
@droplet.java_opts.add_system_property('APPLICATION_INSIGHTS_IKEY',
44+
credentials[INSTRUMENTATION_KEY])
45+
# add environment variable for compatibility with agent version 3.x
46+
# this triggers a warning message to switch to connection string
47+
@droplet.environment_variables.add_environment_variable('APPINSIGHTS_INSTRUMENTATIONKEY',
48+
credentials[INSTRUMENTATION_KEY])
49+
end
50+
@droplet.java_opts.add_javaagent(@droplet.sandbox + jar_name)
4151
end
4252

4353
protected
4454

4555
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
4656
def supports?
47-
@application.services.one_service? FILTER, INSTRUMENTATION_KEY
57+
@application.services.one_service?(FILTER, [CONNECTION_STRING, INSTRUMENTATION_KEY])
4858
end
4959

5060
FILTER = /azure-application-insights/.freeze
5161

62+
CONNECTION_STRING = 'connection_string'
5263
INSTRUMENTATION_KEY = 'instrumentation_key'
5364

54-
private_constant :FILTER, :INSTRUMENTATION_KEY
65+
private_constant :FILTER, :CONNECTION_STRING, :INSTRUMENTATION_KEY
5566

5667
end
5768

spec/java_buildpack/framework/azure_application_insights_agent_spec.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
context do
3434

3535
before do
36-
allow(services).to receive(:one_service?).with(/azure-application-insights/, 'instrumentation_key')
36+
allow(services).to receive(:one_service?).with(/azure-application-insights/,
37+
'connection_string',
38+
'instrumentation_key')
3739
.and_return(true)
3840
end
3941

@@ -59,15 +61,26 @@
5961

6062
it 'updates JAVA_OPTS' do
6163
allow(services).to receive(:find_service)
62-
.and_return('credentials' => { 'instrumentation_key' => 'test-instrumentation-key' })
64+
.and_return('credentials' => { 'connection_string' => 'test-connection-string',
65+
'instrumentation_key' => 'test-instrumentation-key' })
6366

6467
component.release
6568

6669
expect(java_opts).to include('-javaagent:$PWD/.java-buildpack/azure_application_insights_agent/' \
6770
"azure_application_insights_agent-#{version}.jar")
71+
expect(java_opts).to include('-Dapplicationinsights.connection.string=test-connection-string')
6872
expect(java_opts).to include('-DAPPLICATION_INSIGHTS_IKEY=test-instrumentation-key')
6973
end
7074

75+
it 'updates environment variables' do
76+
allow(services).to receive(:find_service)
77+
.and_return('credentials' => { 'instrumentation_key' => 'test-instrumentation-key' })
78+
79+
component.release
80+
81+
expect(environment_variables).to include('APPINSIGHTS_INSTRUMENTATIONKEY=test-instrumentation-key')
82+
end
83+
7184
end
7285

7386
end

0 commit comments

Comments
 (0)