yet-another-cloudwatch-exporter icon indicating copy to clipboard operation
yet-another-cloudwatch-exporter copied to clipboard

Using UpdateMetrics with v2

Open sundarv85 opened this issue 1 year ago • 0 comments

I'm able to use the UpdateMetrics with clients/v1, but when I use clients/v2 then it fails with the error

level=error ts=2023-12-27T08:54:51.698742Z caller=discovery.go:47 job_type=AWS/EC2 region=us-east-1 arn= account=xxxx msg="Couldn't describe resources" err="not found, ResolveEndpointV2"

Any idea what should be done for getting it to work with clients/v2 as well. And any suggestions on the way I am using it.

The code is

package main

import (
	"bytes"
	"context"
	"fmt"

	exporter "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg" // Import the YACE package
	// clients "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/clients/v1"
	clients "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/clients/v2"
	. "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/config"
	"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/logging"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/common/expfmt"
)

func main() {

	// jobs:
	//   - type: AWS/EC2
	//     regions:
	//       - us-east-1
	//     metrics:
	//       - name: CPUUtilization
	//         statistics: [Average]
	//         period: 300

	ctx, _ := context.WithCancel(context.Background())
	logger := logging.NewLogger("info", false) // Initialize this based on YACE's logging package
	role1 := Role{
		RoleArn:    "",
		ExternalID: "",
	}
	nilToZero := false
	addCloudwatchTimestamp := true
	scrapeConfig := ScrapeConf{
		APIVersion: "v1alpha1",
		Discovery: Discovery{
			Jobs: []*Job{{
				Regions: []string{"us-east-1"},
				Type:    "AWS/EC2",
				Roles:   []Role{role1},
				Metrics: []*Metric{{
					Name:                   "CPUUtilization",
					Statistics:             []string{"Average"},
					Period:                 300,
					Length:                 4200,
					Delay:                  300,
					NilToZero:              &nilToZero,
					AddCloudwatchTimestamp: &addCloudwatchTimestamp,
				}},
			}},
		},
	}

	registry := prometheus.NewRegistry() // Initialize a new Prometheus registry

	// factory := clients.NewFactory(scrapeConfig, false, logger) // Initialize the client factory for clients/v1
	factory, err := clients.NewFactory(scrapeConfig, false, logger) // Initialize the client factory for clients/v2
	if err != nil {
		fmt.Printf("New Factory Error: %s\n", err.Error())
		return
	}

	factory.Refresh()
	defer factory.Clear()

	exporter.UpdateMetrics(ctx, logger, scrapeConfig, registry, factory)

	// Gather the metrics
	metricFamilies, err := registry.Gather()
	if err != nil {
		panic(err)
	}

	// Create an encoder and buffer to write the metrics to
	var buf bytes.Buffer
	for _, mf := range metricFamilies {
		enc := expfmt.NewEncoder(&buf, expfmt.FmtText)
		if err := enc.Encode(mf); err != nil {
			panic(err)
		}
	}

	// Output the metrics
	fmt.Println(buf.String())
}

sundarv85 avatar Dec 27 '23 09:12 sundarv85