|
| 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 |
0 commit comments