Class: Terrafying::Components::Prometheus

Inherits:
Terrafying::Context
  • Object
show all
Defined in:
lib/terrafying/components/prometheus.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vpc:, thanos_name: 'thanos', thanos_version: 'master-2018-10-29-8f247d6', prom_name: 'prometheus', prom_version: 'v2.4.3') ⇒ Prometheus

Returns a new instance of Prometheus.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/terrafying/components/prometheus.rb', line 20

def initialize(
  vpc:,
  thanos_name: 'thanos',
  thanos_version: 'master-2018-10-29-8f247d6',
  prom_name: 'prometheus',
  prom_version: 'v2.4.3'
)
  super()
  @vpc = vpc
  @thanos_name = thanos_name
  @thanos_version = thanos_version
  @prom_name = prom_name
  @prom_version = prom_version
end

Instance Attribute Details

#prometheusObject (readonly)

Returns the value of attribute prometheus.



10
11
12
# File 'lib/terrafying/components/prometheus.rb', line 10

def prometheus
  @prometheus
end

#security_groupObject (readonly)

Returns the value of attribute security_group.



10
11
12
# File 'lib/terrafying/components/prometheus.rb', line 10

def security_group
  @security_group
end

Class Method Details

.create_in(options) ⇒ Object



12
13
14
# File 'lib/terrafying/components/prometheus.rb', line 12

def self.create_in(options)
  new(**options).tap(&:create)
end

.find_in(options) ⇒ Object



16
17
18
# File 'lib/terrafying/components/prometheus.rb', line 16

def self.find_in(options)
  new(**options).tap(&:find)
end

Instance Method Details

#allow_thanos_gossip(security_group) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/terrafying/components/prometheus.rb', line 75

def allow_thanos_gossip(security_group)
  rule_ident = Digest::SHA2.hexdigest("#{security_group}-thanos-#{@vpc.name}")[0..24]
  resource :aws_security_group_rule, rule_ident, {
    security_group_id: security_group,
    type:      'ingress',
    from_port: 10900,
    to_port:   10902,
    protocol:  'tcp',
    cidr_blocks: [@vpc.cidr]
  }
end

#cloudwatch_alarm(name, namespace, dimensions) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/terrafying/components/prometheus.rb', line 296

def cloudwatch_alarm(name, namespace, dimensions)
  resource 'aws_cloudwatch_metric_alarm', name, {
    alarm_name: name,
    comparison_operator: 'GreaterThanOrEqualToThreshold',
    evaluation_periods: '1',
    metric_name: 'UnHealthyHostCount',
    namespace: namespace,
    period: '180',
    threshold: '1',
    statistic: 'Minimum',
    alarm_description: "Monitoring #{name} target group host health",
    dimensions: dimensions,
    alarm_actions: ['arn:aws:sns:eu-west-1:136393635417:prometheus_cloudwatch_topic'],
    ok_actions: ['arn:aws:sns:eu-west-1:136393635417:prometheus_cloudwatch_topic'],
  }
end

#createObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/terrafying/components/prometheus.rb', line 42

def create
  thanos_peers = @vpc.zone.qualify(@thanos_name)

  @thanos = create_thanos(thanos_peers)
  create_thanos_cloudwatch_alert(@thanos)

  @prometheus = create_prom(thanos_peers)
  @security_group = @prometheus.egress_security_group
  create_prometheus_cloudwatch_alert(@prometheus)
  allow_thanos_gossip(@prometheus.egress_security_group)

  @prometheus.used_by_cidr(@vpc.cidr)
  @thanos.used_by_cidr(@vpc.cidr)
end

#create_prom(thanos_peers) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/terrafying/components/prometheus.rb', line 57

def create_prom(thanos_peers)
  add! Terrafying::Components::Service.create_in(
    @vpc, @prom_name,
    ports: [
      {
        type: 'http',
        number: 9090,
        health_check: { path: '/status', protocol: 'HTTP' }
      }
    ],
    instance_type: 'm5.large',
    iam_policy_statements: thanos_store_access,
    instances: { max: 3, min: 1, desired: 2 },
    units: [prometheus_unit, thanos_sidecar_unit(thanos_peers)],
    files: [prometheus_conf, thanos_bucket]
  )
end

#create_prometheus_cloudwatch_alert(service) ⇒ Object



313
314
315
316
317
318
# File 'lib/terrafying/components/prometheus.rb', line 313

def create_prometheus_cloudwatch_alert(service)
  cloudwatch_alarm service.name, 'AWS/ApplicationELB', {
    LoadBalancer: output_of('aws_lb', service.load_balancer.name, 'arn_suffix'),
    TargetGroup: service.load_balancer.targets.first.target_group.to_s.gsub(/id/, 'arn_suffix')
  }
end

#create_thanos(thanos_peers) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/terrafying/components/prometheus.rb', line 87

def create_thanos(thanos_peers)
  add! Terrafying::Components::Service.create_in(
    @vpc, @thanos_name,
    ports: [
      {
        number: 10902,
        health_check: {
          path: '/status',
          protocol: 'HTTP'
        }
      },
      {
        number: 10901
      },
      {
        number: 10900
      }
    ],
    instance_type: 't3.medium',
    units: [thanos_unit(thanos_peers)],
    instances: { max: 3, min: 1, desired: 2 }
  )
end

#create_thanos_cloudwatch_alert(service) ⇒ Object



320
321
322
323
324
325
326
327
# File 'lib/terrafying/components/prometheus.rb', line 320

def create_thanos_cloudwatch_alert(service)
  service.load_balancer.targets.each_with_index do |target, i|
    cloudwatch_alarm "#{service.name}_#{i}", 'AWS/NetworkELB', {
      LoadBalancer: output_of('aws_lb', service.load_balancer.name, 'arn_suffix'),
      TargetGroup: target.target_group.to_s.gsub(/id/, 'arn_suffix')
    }
  end
end

#expose_in(vpc) ⇒ Object



286
287
288
289
290
291
292
293
294
# File 'lib/terrafying/components/prometheus.rb', line 286

def expose_in(vpc)
  @endpoint_service ||= @thanos.with_endpoint_service(acceptance_required: false)

  options = {}
  endpoint = add! @endpoint_service.expose_in(vpc, options)
  endpoint.used_by_cidr(vpc.cidr)

  endpoint
end

#findObject



35
36
37
38
39
40
# File 'lib/terrafying/components/prometheus.rb', line 35

def find
  @security_group = aws.security_group_in_vpc(
    @vpc.id,
    "dynamicset-#{@vpc.name}-#{@prom_name}"
  )
end

#prometheus_confObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/terrafying/components/prometheus.rb', line 181

def prometheus_conf
  {
    path: '/opt/prometheus/prometheus.yml',
    mode: 0o644,
    contents: "      global:\n        external_labels:\n          monitor: prometheus\n          cluster: \"\#{@vpc.name}\"\n          replica: {{HOST}}\n        scrape_interval: 15s\n      scrape_configs:\n      - job_name: \"ec2\"\n        params:\n          format: [\"prometheus\"]\n        ec2_sd_configs:\n        - region: eu-west-1\n          filters:\n          - name: vpc-id\n            values: [\"\#{@vpc.id}\"]\n          - name: tag-key\n            values: [\"prometheus_port\"]\n        relabel_configs:\n        - source_labels: [__meta_ec2_private_ip, __meta_ec2_tag_prometheus_port]\n          replacement: $1:$2\n          regex: ([^:]+)(?::\\\\\\\\d+)?;(\\\\\\\\d+)\n          target_label: __address__\n        - source_labels: [__meta_ec2_instance_id]\n          target_label: instance_id\n        - source_labels: [__meta_ec2_tag_envoy_cluster]\n          target_label: envoy_cluster\n        - source_labels: [__meta_ec2_tag_prometheus_path]\n          regex: (.+)\n          replacement: $1\n          target_label: __metrics_path__\n    PROM\n  }\nend\n"

#prometheus_unitObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/terrafying/components/prometheus.rb', line 111

def prometheus_unit
  {
    name: 'prometheus.service',
    contents: "      [Install]\n      WantedBy=multi-user.target\n       [Unit]\n      Description=Prometheus Service\n      After=docker.service\n      Requires=docker.service\n       [Service]\n      ExecStartPre=-/usr/bin/docker network create --driver bridge prom\n      ExecStartPre=-/usr/bin/docker kill prometheus\n      ExecStartPre=-/usr/bin/docker rm prometheus\n      ExecStartPre=/usr/bin/docker pull quay.io/prometheus/prometheus:\#{@prom_version}\n      ExecStartPre=-/usr/bin/sed -i \"s/{{HOST}}/%H/\" /opt/prometheus/prometheus.yml\n      ExecStartPre=/usr/bin/install -d -o nobody -g nobody -m 0755 /opt/prometheus/data\n      ExecStart=/usr/bin/docker run --name prometheus \\\n        -p 9090:9090 \\\n        --network=prom \\\n        -v /opt/prometheus:/opt/prometheus \\\n        quay.io/prometheus/prometheus:\#{@prom_version} \\\n        --storage.tsdb.path=/opt/prometheus/data \\\n        --storage.tsdb.retention=1d \\\n        --storage.tsdb.min-block-duration=2h \\\n        --storage.tsdb.max-block-duration=2h \\\n        --config.file=/opt/prometheus/prometheus.yml \\\n        --web.enable-lifecycle \\\n        --log.level=warn\n      Restart=always\n      RestartSec=30\n    PROM_UNIT\n  }\nend\n"

#thanos_bucketObject



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/terrafying/components/prometheus.rb', line 250

def thanos_bucket
  {
    path: '/opt/thanos/bucket.yml',
    mode: 0o644,
    contents: "      type: S3\n      config:\n          bucket: uswitch-thanos-store\n          endpoint: s3.eu-west-1.amazonaws.com\n    S3CONF\n  }\nend\n"

#thanos_sidecar_unit(thanos_peers) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/terrafying/components/prometheus.rb', line 146

def thanos_sidecar_unit(thanos_peers)
  {
    name: 'thanos.service',
    contents: "      [Install]\n      WantedBy=multi-user.target\n       [Unit]\n      Description=Thanos Service\n      After=docker.service prometheus.service\n      Requires=docker.service prometheus.service\n       [Service]\n      EnvironmentFile=/run/metadata/coreos\n      ExecStartPre=-/usr/bin/docker kill thanos\n      ExecStartPre=-/usr/bin/docker rm thanos\n      ExecStartPre=/usr/bin/docker pull improbable/thanos:\#{@thanos_version}\n      ExecStart=/usr/bin/docker run --name thanos \\\n        -p 10900-10902:10900-10902 \\\n        -v /opt/prometheus:/opt/prometheus \\\n        -v /opt/thanos:/opt/thanos \\\n        --network=prom \\\n        improbable/thanos:\#{@thanos_version} \\\n        sidecar \\\n        --cluster.peers=\#{thanos_peers}:10900 \\\n        --cluster.advertise-address=$${COREOS_EC2_IPV4_LOCAL}:10900 \\\n        --grpc-advertise-address=$${COREOS_EC2_IPV4_LOCAL}:10901 \\\n        --prometheus.url=http://prometheus:9090 \\\n        --tsdb.path=/opt/prometheus/data \\\n        --objstore.config-file=/opt/thanos/bucket.yml \\\n        --log.level=warn\n      Restart=always\n      RestartSec=30\n    THANOS_SIDE\n  }\nend\n"

#thanos_store_accessObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/terrafying/components/prometheus.rb', line 263

def thanos_store_access
  [
    {
      Action: ['ec2:DescribeInstances'],
      Effect: 'Allow',
      Resource: '*'
    },
    {
      Action: [
        's3:ListBucket',
        's3:GetObject',
        's3:DeleteObject',
        's3:PutObject'
      ],
      Effect: 'Allow',
      Resource: [
        'arn:aws:s3:::uswitch-thanos-store/*',
        'arn:aws:s3:::uswitch-thanos-store'
      ]
    }
  ]
end

#thanos_unit(thanos_peers) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/terrafying/components/prometheus.rb', line 220

def thanos_unit(thanos_peers)
  {
    name: 'thanos.service',
    contents: "      [Install]\n      WantedBy=multi-user.target\n       [Unit]\n      Description=Thanos Service\n      After=docker.service\n      Requires=docker.service\n       [Service]\n      EnvironmentFile=/run/metadata/coreos\n      ExecStartPre=-/usr/bin/docker kill thanos\n      ExecStartPre=-/usr/bin/docker rm thanos\n      ExecStartPre=/usr/bin/docker pull improbable/thanos:\#{@thanos_version}\n      ExecStart=/usr/bin/docker run --name thanos \\\n        -p 10900-10902:10900-10902 \\\n        improbable/thanos:\#{@thanos_version} \\\n        query \\\n        --cluster.peers=\#{thanos_peers}:10900 \\\n        --cluster.advertise-address=$${COREOS_EC2_IPV4_LOCAL}:10900 \\\n        --grpc-advertise-address=$${COREOS_EC2_IPV4_LOCAL}:10901 \\\n        --query.replica-label=replica \\\n        --log.level=warn\n      Restart=always\n      RestartSec=30\n    THANOS_UNIT\n  }\nend\n"