Class: PrometheusConfigBuilder::ScrapeConfigECS
- Inherits:
-
Object
- Object
- PrometheusConfigBuilder::ScrapeConfigECS
- Extended by:
- PrometheusConfigBuilderLogger
- Defined in:
- lib/prometheus-config-builder/scrape_ecs.rb
Constant Summary collapse
- @@ecs =
Map of different roles to ecs clients. use get_client() to obtain them
{}
Class Method Summary collapse
- .get_client(region, assume_role = nil) ⇒ Object
- .get_task_endpoints(client, cluster, tasks, common_labels, metrics_port) ⇒ Object
- .get_tasks(client, cluster, service_name) ⇒ Object
- .handle(basename, config, dst_prefix) ⇒ Object
Methods included from PrometheusConfigBuilderLogger
Class Method Details
.get_client(region, assume_role = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/prometheus-config-builder/scrape_ecs.rb', line 13 def self.get_client(region, assume_role=nil) name = region + "/default" if assume_role == nil client = @@ecs[name] if client == nil logger.info("Creating default client: #{name} with region: #{region}") client = Aws::ECS::Client.new({ region: region, instance_profile_credentials_retries: 4, instance_profile_credentials_timeout: 2, }) @@ecs[name] = client end return client end role_arn = assume_role["role_arn"] if !role_arn raise ArgumentError.new("role_arn not found inside assume_role config structure") end role_session_name = (assume_role["role_session_name"] or "prometheus-scrape-ecs") name = region + "/" + role_arn + "/" + role_session_name client = @@ecs[name] if client == nil logger.info("Using sts:AssumeRole on #{role_arn} as client #{name}") role_credentials = Aws::AssumeRoleCredentials.new( client: Aws::STS::Client.new({ region: region }), role_arn: role_arn, role_session_name: role_session_name ) client = Aws::ECS::Client.new(credentials: role_credentials) @@ecs[name] = client end return client end |
.get_task_endpoints(client, cluster, tasks, common_labels, metrics_port) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/prometheus-config-builder/scrape_ecs.rb', line 158 def self.get_task_endpoints(client, cluster, tasks, common_labels, metrics_port) endpoints = [] if tasks.length == 0 return [] end task_chunk = tasks.pop(100) loop do begin = { cluster: cluster, tasks: task_chunk, } result = client.describe_tasks() if result && result.tasks result.tasks.each do |task| # FIXME: This assumes somewhat on the ip structure. if task.containers[0].network_interfaces && task.containers[0].network_interfaces.length > 0 ip = task.containers[0].network_interfaces[0].private_ipv_4_address else logger.warn("WARNING: Unable to obtain ipv4 address from task #{task}") next end if metrics_port ip = ip + ":" + metrics_port.to_s end labels = common_labels.clone labels["ecs_arn"] = task.task_arn endpoints << { "targets" => [ip], "labels" => labels } end end rescue Aws::ECS::Errors::ServiceError => e logger.warn("Error listing ecs tasks for cluster #{cluster}: #{e}\n") sleep(1) next end break if tasks.length == 0 task_chunk = tasks.pop(100) end return endpoints end |
.get_tasks(client, cluster, service_name) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/prometheus-config-builder/scrape_ecs.rb', line 127 def self.get_tasks(client, cluster, service_name) tasks = [] last_result = Aws::ECS::Types::ListTasksResponse.new last_result.next_token = nil retries = 3 loop do begin = { cluster: cluster, service_name: service_name, next_token: last_result.next_token } last_result = client.list_tasks() if last_result && last_result.task_arns tasks.push(*last_result.task_arns) end rescue Aws::ECS::Errors::ServiceError => e logger.warn("Error listing ecs tasks for service #{service_name} in cluster #{cluster}: #{e}") retries -= 1 if retries < 0 raise IOError.new("Unable to get ecs tasks for service #{service_name} in cluster #{cluster}: #{e}") end sleep(1) next end break if !last_result.next_token end return tasks end |
.handle(basename, config, dst_prefix) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/prometheus-config-builder/scrape_ecs.rb', line 55 def self.handle(basename, config, dst_prefix) x = $VERBOSE $VERBOSE = nil require 'aws-sdk' if !config["region"] logger.warn("File #{basename}: the scrape_configs of type:ecs-tasks doesn't have \"region\" field set. Ignoring!") return nil end region = (config["region"] or "us-east-1") assume_role = config["assume_role"] retries = 5 begin client = get_client(region, assume_role) $VERBOSE = x tasks = get_tasks(client, config["cluster"], config["service"]) endpoints = get_task_endpoints(client, config["cluster"], tasks, config["labels"], config["metrics_port"]) if !config["job_name"] logger.warn("File #{basename}: the scrape_configs of type:ecs-tasks doesn't have \"job_name\" field set. Ignoring!") return nil end rescue Aws::Errors::MissingCredentialsError => e logger.warn("Got a Aws::Errors::MissingCredentialsError: #{e}") if retries > 0 retries = retries - 1 logger.warn("Will try to retry") sleep(5) retry end raise e rescue ArgumentError => e logger.warn("File #{basename}: the scrape_configs of type:ecs-tasks had invalid arguments: #{e}") return nil end # Prometheus might notice that the scrape file is changed in the middle of the write. That's why we'll # first write the new contents into a temp file and atomically replace the file with the temp file. file = File.(dst_prefix + "_" + config["job_name"] + ".json") tmpfile = Tempfile.new().path File.open(tmpfile, "w") do |f| f.write(endpoints.to_json) end File.rename(tmpfile, file) # Make copy of the settings and remove our custom properties from it. # The rest user can set just as he wants according to the Prometheus schema. # See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config settings = config.clone settings.delete("type") settings.delete("assume_role") settings.delete("cluster") settings.delete("service") settings.delete("metrics_port") settings.delete("labels") settings.delete("region") settings["file_sd_configs"] = [ "files" => [ file ] ] return settings end |