Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to automatic fetch all the LoadBalancer (With Target Group) tags #571

Open
JesusFrontelo opened this issue Aug 30, 2023 · 5 comments
Open

Comments

@JesusFrontelo
Copy link

JesusFrontelo commented Aug 30, 2023

When you have a bunch of Load Balancers, and each one has multiple target groups, its really hard to manually specify tags with group_left (tag_xxx, tag_yyy, tag_zzz) instead of doing it with group_right, which automatically joins all tags to the metric.

Here i give you an example of my configuration to scrape metrics and a query, for use in graph or alerts.

First of all, you have to define wich metrics you want to collect, and pay attention on the dimension of each metric.

  - aws_namespace: AWS/ApplicationELB
    aws_metric_name: ActiveConnectionCount
    aws_dimensions: [LoadBalancer]
    aws_tag_select:
      resource_type_selection: elasticloadbalancing:loadbalancer/app
      resource_id_dimension: LoadBalancer
    aws_statistics: [Sum]

  - aws_namespace: AWS/ApplicationELB
    aws_metric_name: ConsumedLCUs
    aws_dimensions: [LoadBalancer]
    aws_statistics: [Average, Maximum, Minimum]

  - aws_namespace: AWS/ApplicationELB
    aws_metric_name: ProcessedBytes
    aws_dimensions: [LoadBalancer]
    aws_statistics: [Sum]

  - aws_namespace: AWS/ApplicationELB
    aws_metric_name: NewConnectionCount
    aws_dimensions: [LoadBalancer]
    aws_statistics: [Sum]

  - aws_namespace: AWS/ApplicationELB
    aws_metric_name: RequestCount
    aws_dimensions: [LoadBalancer]
    aws_statistics: [Sum]

  - aws_namespace: AWS/ApplicationELB
    aws_metric_name: HealthyHostCount
    aws_dimensions: [TargetGroup, LoadBalancer]
    aws_statistics: [Average, Minimum, Maximum]

  - aws_namespace: AWS/ApplicationELB
    aws_metric_name: UnHealthyHostCount
    aws_dimensions: [TargetGroup, LoadBalancer]
    aws_tag_select:
      resource_type_selection: elasticloadbalancing:targetgroup
      resource_id_dimension: TargetGroup
    aws_statistics: [Average, Minimum, Maximum]

  - aws_namespace: AWS/ApplicationELB
    aws_metric_name: TargetResponseTime
    aws_dimensions: [TargetGroup, LoadBalancer]
    aws_statistics: [Average]

See how i set the aws_tag_select section when the metric has only LoadBalancer Dimension, or when it has two dimensions, like UnHealthyHostCount, wich has LoadBalancer and TargetGroup dimensions.
With this settings you will recieve the aws_resource_info for only LoadBalancer dimension and for TargetGroup separately, something like:

LoadBalancer Dimension:
aws_resource_info{arn="arn:aws:elasticloadbalancing:eu-west-1:************:loadbalancer/app/MY-LB/MY-LB-ID", exported_job="aws_applicationelb", instance="localhost:PORT", job="The name of the job", load_balancer="app/MY-LB/MY-LB-ID", tag_Application="APP-CODE", tag_Domain="DOM", tag_Environment="PRO", tag_Product="PRODUCT-CODE", tag_Terraform="True"}

With TargetGroup Dimension:
aws_resource_info{arn="arn:aws:elasticloadbalancing:eu-west-1:************:targetgroup/MY-LB-TARGETGROUP-0/TargetGroup-ID", exported_job="aws_applicationelb", instance="localhost:PORT", job="The name of the job", tag_Application="APP-CODE", tag_Domain="DOM", tag_Environment="PRO", tag_Product="PRODUCT-CODE", tag_Terraform="True", target_group="MY-LB-TARGETGROUP-0/TargetGroup-ID"}

And here is the second part of the magic trick :P

You should change the value of the target_group label from the metric, because tag value is like 'target_group="targetgroup/MY-LB-TARGETGROUP-0/TargetGroup-ID", removing the first part of its value 'targetgroup/'. To do this you must use the function label_replace.

The query:
label_replace(aws_applicationelb_un_healthy_host_count_average, "target_group", "$1", "target_group", "targetgroup/(.+)") * on (target_group) group_right (load_balancer) (aws_resource_info{exported_job="aws_applicationelb"})

I hope I have explained it as well as possible, and that it will be useful for everyone.

@JesusFrontelo
Copy link
Author

JesusFrontelo commented Aug 30, 2023

P.D: You can use this for NLB too. Only need to change the namespace of the metric from AWS/ApplicationELB to AWS/NetworkELB, and change the resource_type_selection, from :loadbalancer/app to :loadbalancer/net.

@JesusFrontelo JesusFrontelo changed the title How to automatic fetch all the ALB (With Target Group) tags How to automatic fetch all the LoadBalancer (With Target Group) tags Aug 30, 2023
@matthiasr
Copy link
Contributor

Oh this is awesome, thank you for the writeup! There is an example configuration for ALB, but it is very basic right now – could you extend it with your tricks and add some comments as explanation?

Instead of label_replace, one could also use metric relabeling to do this on ingestion, something like

- source_labels: [__name__,target_group]
  regex: 'aws_applicationelb_[^;]+;target_group/(.+)'
  target_label: target_group
  replacement: '$1'

(haven't tested this)

@JesusFrontelo
Copy link
Author

JesusFrontelo commented Sep 11, 2023

thanks @matthiasr , i´ve tested what you say and the right config for it is:

      - source_labels: [target_group]
        regex: '^targetgroup/(.+)$'
        replacement: '$1'
        target_label: target_group

it´s a great thing to modify label values at job instead of replacing labels on each metric, thanks for the advice.

@JesusFrontelo
Copy link
Author

Hi again @matthiasr ,

Do you have a special procedure to create a pull request? i mean, i.e do you need me to create a new branch with specific name?

@matthiasr
Copy link
Contributor

Fork, branch, and create a PR. You will have to git commit --signoff for legal reasons. Generally it's easier for you to manage if you create a branch, rather than submitting from your fork's master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants