|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# © 2023 SolarWinds Worldwide, LLC. All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
| 8 | + |
| 9 | +module SolarWindsAPM |
| 10 | + # OTLPEndPoint |
| 11 | + class OTLPEndPoint |
| 12 | + SWO_APM_ENDPOINT_REGEX = /^apm\.collector\.([a-z]{2}-\d{2})\.([^.]+)\.solarwinds\.com(?::\d+)?$/ |
| 13 | + SWO_APM_ENDPOINT_DEFAULT = 'apm.collector.na-01.cloud.solarwinds.com:443' |
| 14 | + |
| 15 | + SWO_OTLP_GENERAL_ENDPOINT_REGEX = %r{^https://otel\.collector\.[a-z0-9-]+\.[a-z0-9-]+\.solarwinds\.com(?::\d+)?$} |
| 16 | + SWO_OTLP_SIGNAL_ENDPOINT_REGEX = %r{^https://otel\.collector\.[a-z0-9-]+\.[a-z0-9-]+\.solarwinds\.com(?::\d+)?/v1/(?:logs|metrics|traces)$} |
| 17 | + SWO_OTLP_ENDPOINT_DEFAULT = 'https://otel.collector.na-01.cloud.solarwinds.com:443' |
| 18 | + |
| 19 | + OTEL_SIGNAL_TYPE = %w[TRACES METRICS LOGS].freeze |
| 20 | + |
| 21 | + def initialize |
| 22 | + @token = nil |
| 23 | + end |
| 24 | + |
| 25 | + def config_otlp_token_and_endpoint |
| 26 | + matches = ENV['SW_APM_COLLECTOR'].to_s.match(SWO_APM_ENDPOINT_REGEX) |
| 27 | + |
| 28 | + resolve_get_setting_endpoint(matches) |
| 29 | + |
| 30 | + service_key_checker = SolarWindsAPM::ServiceKeyChecker.new('ssl', SolarWindsAPM::Utils.determine_lambda_env) |
| 31 | + @token = service_key_checker.token unless service_key_checker.token.nil? |
| 32 | + |
| 33 | + OTEL_SIGNAL_TYPE.each do |data_type| |
| 34 | + config_token(data_type) |
| 35 | + configure_otlp_endpoint(data_type, matches) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + # APM Libraries should only set the bearer token header as a convenience if: |
| 40 | + # The OTEL config for exporter OTLP headers is not already set, i.e. explicitly configured by the end user, AND |
| 41 | + # The OTLP export endpoint is SWO, i.e. host is otel.collector.*.*.solarwinds.com |
| 42 | + def config_token(data_type) |
| 43 | + data_type_upper = data_type.upcase |
| 44 | + |
| 45 | + return unless @token |
| 46 | + |
| 47 | + # puts "#{ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_ENDPOINT"].to_s.match?(SWO_OTLP_SIGNAL_ENDPOINT_REGEX)}" |
| 48 | + # puts "#{ENV['OTEL_EXPORTER_OTLP_ENDPOINT'].to_s.match?(SWO_OTLP_GENERAL_ENDPOINT_REGEX)}" |
| 49 | + if ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_HEADERS"].to_s.empty? && ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_ENDPOINT"].to_s.match?(SWO_OTLP_SIGNAL_ENDPOINT_REGEX) |
| 50 | + ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_HEADERS"] = "authorization=Bearer #{@token}" |
| 51 | + elsif ENV['OTEL_EXPORTER_OTLP_HEADERS'].to_s.empty? && ENV['OTEL_EXPORTER_OTLP_ENDPOINT'].to_s.match?(SWO_OTLP_GENERAL_ENDPOINT_REGEX) |
| 52 | + ENV['OTEL_EXPORTER_OTLP_HEADERS'] = "authorization=Bearer #{@token}" |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + def configure_otlp_endpoint(data_type, matches) |
| 57 | + data_type_upper = data_type.upcase |
| 58 | + data_type_lower = data_type.downcase |
| 59 | + otlp_endpoint_source = if ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_ENDPOINT"] |
| 60 | + "#{data_type_lower}_endpoint" |
| 61 | + elsif ENV['OTEL_EXPORTER_OTLP_ENDPOINT'] |
| 62 | + 'general_endpoint' |
| 63 | + else |
| 64 | + 'no_endpoint' |
| 65 | + end |
| 66 | + |
| 67 | + SolarWindsAPM.logger.debug { "[#{self.class}/#{__method__}] otlp_endpoint_source: #{otlp_endpoint_source}" } |
| 68 | + |
| 69 | + return unless otlp_endpoint_source == 'no_endpoint' |
| 70 | + |
| 71 | + if matches&.size == 3 |
| 72 | + region = matches[1] |
| 73 | + env = matches[2] |
| 74 | + ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_ENDPOINT"] = "https://otel.collector.#{region}.#{env}.solarwinds.com:443/v1/#{data_type_lower}" |
| 75 | + else |
| 76 | + ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_ENDPOINT"] = "https://otel.collector.na-01.cloud.solarwinds.com:443/v1/#{data_type_lower}" |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + # only valid value is apm.collector.*.*.solarwinds.com |
| 81 | + # If SW APM config for collector is not set, the fallback: apm.collector.na-01.cloud.solarwinds.com |
| 82 | + def resolve_get_setting_endpoint(matches) |
| 83 | + return if matches&.size == 3 |
| 84 | + |
| 85 | + SolarWindsAPM.logger.warn { "[#{self.class}/#{__method__}] SW_APM_COLLECTOR format invalid: #{ENV.fetch('SW_APM_COLLECTOR', nil)}. Valid formt: apm.collector.*.*.solarwinds.com" } |
| 86 | + ENV['SW_APM_COLLECTOR'] = SWO_APM_ENDPOINT_DEFAULT |
| 87 | + end |
| 88 | + end |
| 89 | +end |
0 commit comments