Skip to content

Commit

Permalink
implement new tag manager for resource label from tags
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed May 2, 2023
1 parent dd10dd2 commit b2bfd97
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.7.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1
github.com/google/uuid v1.3.0
github.com/webdevops/go-common v0.0.0-20230425195838-2b3ad6880230
github.com/webdevops/go-common v0.0.0-20230502000651-d37d46be8ee7
go.uber.org/zap v1.24.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/webdevops/go-common v0.0.0-20230425195838-2b3ad6880230 h1:pC5GQM0ZeaZQmRUbQj9Euj2UwfcxA5vowERWgzBj3KE=
github.com/webdevops/go-common v0.0.0-20230425195838-2b3ad6880230/go.mod h1:izG2uin/dH7iEjAW1ak9qmfqfkrI1H/CEgaq/yDefkk=
github.com/webdevops/go-common v0.0.0-20230502000651-d37d46be8ee7 h1:DPIp1yL33mrFetoqAavviyt2iqKCuD2PRaR3hd+Nvr8=
github.com/webdevops/go-common v0.0.0-20230502000651-d37d46be8ee7/go.mod h1:/gBgzF9JefrEJqAqgPlDgwMcBtYwGmqFa3p4g39RvIU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ var (
argparser *flags.Parser
opts config.Opts

AzureClient *armclient.ArmClient
AzureClient *armclient.ArmClient
AzureResourceTagManager *armclient.ResourceTagManager

prometheusCollectTime *prometheus.SummaryVec
prometheusMetricRequests *prometheus.CounterVec
Expand Down Expand Up @@ -101,6 +102,11 @@ func initAzureConnection() {
if err := AzureClient.Connect(); err != nil {
logger.Fatal(err.Error())
}

AzureResourceTagManager, err = AzureClient.TagManager.ParseTagConfig(opts.Azure.ResourceTags)
if err != nil {
logger.Fatalf(`unable to parse resourceTag configuration "%s": %v"`, opts.Azure.ResourceTags, err.Error())
}
}

// start and handle prometheus handler
Expand Down
20 changes: 10 additions & 10 deletions metrics/insights.general.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type (
AzureInsightBaseMetricsResult struct {
settings *RequestMetricSettings
prober *MetricProber
}
)

Expand All @@ -20,32 +20,32 @@ func (r *AzureInsightBaseMetricsResult) buildMetric(labels prometheus.Labels, va
}

metric = PrometheusMetricResult{
Name: r.settings.MetricTemplate,
Name: r.prober.settings.MetricTemplate,
Labels: metricLabels,
Value: value,
}

// fallback if template is empty (should not be)
if r.settings.MetricTemplate == "" {
metric.Name = r.settings.Name
if r.prober.settings.MetricTemplate == "" {
metric.Name = r.prober.settings.Name
}

resourceType := r.settings.ResourceType
resourceType := r.prober.settings.ResourceType
// MetricNamespace is more descriptive than type
if r.settings.MetricNamespace != "" {
resourceType = r.settings.MetricNamespace
if r.prober.settings.MetricNamespace != "" {
resourceType = r.prober.settings.MetricNamespace
}

// set help
metric.Help = r.settings.HelpTemplate
metric.Help = r.prober.settings.HelpTemplate
if metricNamePlaceholders.MatchString(metric.Help) {
metric.Help = metricNamePlaceholders.ReplaceAllStringFunc(
metric.Help,
func(fieldName string) string {
fieldName = strings.Trim(fieldName, "{}")
switch fieldName {
case "name":
return r.settings.Name
return r.prober.settings.Name
case "type":
return resourceType
default:
Expand All @@ -65,7 +65,7 @@ func (r *AzureInsightBaseMetricsResult) buildMetric(labels prometheus.Labels, va
fieldName = strings.Trim(fieldName, "{}")
switch fieldName {
case "name":
return r.settings.Name
return r.prober.settings.Name
case "type":
return resourceType
default:
Expand Down
2 changes: 1 addition & 1 deletion metrics/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (p *MetricProber) MetricsClient(subscriptionId string) (*armmonitor.Metrics
func (p *MetricProber) FetchMetricsFromTarget(client *armmonitor.MetricsClient, target MetricProbeTarget, metrics, aggregations []string) (AzureInsightMetricsResult, error) {
ret := AzureInsightMetricsResult{
AzureInsightBaseMetricsResult: AzureInsightBaseMetricsResult{
settings: p.settings,
prober: p,
},
target: &target,
}
Expand Down
16 changes: 8 additions & 8 deletions metrics/insights.subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type (

func (r *AzureInsightSubscriptionMetricsResult) SendMetricToChannel(channel chan<- PrometheusMetricResult) {
if r.Result.Value != nil {
// DEBUGGING
// data, _ := json.Marshal(r.Result)
// fmt.Println(string(data))

for _, metric := range r.Result.Value {
if metric.Timeseries != nil {
for _, timeseries := range metric.Timeseries {
Expand Down Expand Up @@ -55,17 +59,13 @@ func (r *AzureInsightSubscriptionMetricsResult) SendMetricToChannel(channel chan
"resourceName": azureResource.ResourceName,
"metric": to.String(metric.Name.Value),
"unit": metricUnit,
"interval": to.String(r.settings.Interval),
"timespan": r.settings.Timespan,
"interval": to.String(r.prober.settings.Interval),
"timespan": r.prober.settings.Timespan,
"aggregation": "",
}

// // add resource tags as labels
// metricLabels = armclient.AddResourceTagsToPrometheusLabels(
// metricLabels,
// r.target.Tags,
// r.settings.TagLabels,
// )
// add resource tags as labels
metricLabels = r.prober.AzureResourceTagManager.AddResourceTagsToPrometheusLabels(r.prober.ctx, metricLabels, resourceId)

if len(dimensions) == 1 {
// we have only one dimension
Expand Down
11 changes: 4 additions & 7 deletions metrics/insights.target.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type (
AzureInsightMetricsResult struct {
AzureInsightBaseMetricsResult

target *MetricProbeTarget
Result *armmonitor.MetricsClientListResponse
}
Expand Down Expand Up @@ -51,17 +52,13 @@ func (r *AzureInsightMetricsResult) SendMetricToChannel(channel chan<- Prometheu
"resourceName": azureResource.ResourceName,
"metric": to.String(metric.Name.Value),
"unit": metricUnit,
"interval": to.String(r.settings.Interval),
"timespan": r.settings.Timespan,
"interval": to.String(r.prober.settings.Interval),
"timespan": r.prober.settings.Timespan,
"aggregation": "",
}

// add resource tags as labels
metricLabels = armclient.AddResourceTagsToPrometheusLabels(
metricLabels,
r.target.Tags,
r.settings.TagLabels,
)
metricLabels = r.prober.AzureResourceTagManager.AddResourceTagsToPrometheusLabels(r.prober.ctx, metricLabels, resourceId)

if len(dimensions) == 1 {
// we have only one dimension
Expand Down
9 changes: 7 additions & 2 deletions metrics/prober.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type (
MetricProber struct {
Conf config.Opts

AzureClient *armclient.ArmClient
AzureClient *armclient.ArmClient
AzureResourceTagManager *armclient.ResourceTagManager

userAgent string

Expand Down Expand Up @@ -103,6 +104,10 @@ func (p *MetricProber) SetAzureClient(client *armclient.ArmClient) {
p.AzureClient = client
}

func (p *MetricProber) SetAzureResourceTagManager(client *armclient.ResourceTagManager) {
p.AzureResourceTagManager = client
}

func (p *MetricProber) EnableMetricsCache(cache *cache.Cache, cacheKey string, cacheDuration *time.Duration) {
p.metricsCache.cache = cache
p.metricsCache.cacheKey = &cacheKey
Expand Down Expand Up @@ -224,7 +229,7 @@ func (p *MetricProber) collectMetricsFromSubscriptions() {

result := AzureInsightSubscriptionMetricsResult{
AzureInsightBaseMetricsResult: AzureInsightBaseMetricsResult{
settings: p.settings,
prober: p,
},
subscriptionID: subscriptionId,
Result: &response}
Expand Down
4 changes: 0 additions & 4 deletions metrics/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ type (
MetricTemplate string
HelpTemplate string

TagLabels []string

// cache
Cache *time.Duration
}
Expand Down Expand Up @@ -70,8 +68,6 @@ func NewRequestMetricSettings(r *http.Request, opts config.Opts) (RequestMetricS
ret := RequestMetricSettings{}
params := r.URL.Query()

ret.TagLabels = opts.Azure.ResourceTags

// param name
ret.Name = paramsGetWithDefault(params, "name", PrometheusMetricNameDefault)

Expand Down
1 change: 1 addition & 0 deletions probe_metrics_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func probeMetricsListHandler(w http.ResponseWriter, r *http.Request) {
prober := metrics.NewMetricProber(ctx, contextLogger, w, &settings, opts)
prober.SetUserAgent(UserAgent + gitTag)
prober.SetAzureClient(AzureClient)
prober.SetAzureResourceTagManager(AzureResourceTagManager)
prober.SetPrometheusRegistry(registry)
if settings.Cache != nil {
cacheKey := fmt.Sprintf("list:%x", sha1.Sum([]byte(r.URL.String()))) // #nosec G401
Expand Down
1 change: 1 addition & 0 deletions probe_metrics_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func probeMetricsResourceHandler(w http.ResponseWriter, r *http.Request) {
prober := metrics.NewMetricProber(ctx, contextLogger, w, &settings, opts)
prober.SetUserAgent(UserAgent + gitTag)
prober.SetAzureClient(AzureClient)
prober.SetAzureResourceTagManager(AzureResourceTagManager)
prober.SetPrometheusRegistry(registry)
if settings.Cache != nil {
cacheKey := fmt.Sprintf("resource:%x", sha1.Sum([]byte(r.URL.String()))) // #nosec G401
Expand Down
1 change: 1 addition & 0 deletions probe_metrics_resourcegraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func probeMetricsResourceGraphHandler(w http.ResponseWriter, r *http.Request) {
prober := metrics.NewMetricProber(ctx, contextLogger, w, &settings, opts)
prober.SetUserAgent(UserAgent + gitTag)
prober.SetAzureClient(AzureClient)
prober.SetAzureResourceTagManager(AzureResourceTagManager)
prober.SetPrometheusRegistry(registry)
if settings.Cache != nil {
cacheKey := fmt.Sprintf("scrape:%x", sha1.Sum([]byte(r.URL.String()))) // #nosec G401
Expand Down
1 change: 1 addition & 0 deletions probe_metrics_scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func probeMetricsScrapeHandler(w http.ResponseWriter, r *http.Request) {
prober := metrics.NewMetricProber(ctx, contextLogger, w, &settings, opts)
prober.SetUserAgent(UserAgent + gitTag)
prober.SetAzureClient(AzureClient)
prober.SetAzureResourceTagManager(AzureResourceTagManager)
prober.SetPrometheusRegistry(registry)
if settings.Cache != nil {
cacheKey := fmt.Sprintf("scrape:%x", sha1.Sum([]byte(r.URL.String()))) // #nosec G401
Expand Down
1 change: 1 addition & 0 deletions probe_metrics_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func probeMetricsSubscriptionHandler(w http.ResponseWriter, r *http.Request) {
prober := metrics.NewMetricProber(ctx, contextLogger, w, &settings, opts)
prober.SetUserAgent(UserAgent + gitTag)
prober.SetAzureClient(AzureClient)
prober.SetAzureResourceTagManager(AzureResourceTagManager)
prober.SetPrometheusRegistry(registry)
if settings.Cache != nil {
cacheKey := fmt.Sprintf("list:%x", sha1.Sum([]byte(r.URL.String()))) // #nosec G401
Expand Down

0 comments on commit b2bfd97

Please sign in to comment.