Skip to content

Commit

Permalink
add metrics generation behavior config
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Sep 26, 2024
1 parent fe139f0 commit c8babc9
Show file tree
Hide file tree
Showing 12 changed files with 6,606 additions and 86 deletions.
20 changes: 17 additions & 3 deletions configuration/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type updateCommand struct {

func introspectSchema(ctx context.Context, args *UpdateArguments) error {
start := time.Now()
slog.Info("introspect metrics metadata...", slog.String("dir", args.Dir))
slog.Info("introspecting metadata", slog.String("dir", args.Dir))
originalConfig, err := metadata.ReadConfiguration(args.Dir)
if err != nil {
if !os.IsNotExist(err) {
Expand All @@ -62,6 +62,11 @@ func introspectSchema(ctx context.Context, args *UpdateArguments) error {
}

if originalConfig.Generator.Metrics.Enabled {
slog.Info("introspecting metrics",
slog.String("behavior", string(originalConfig.Generator.Metrics.Behavior)),
slog.Any("include", originalConfig.Generator.Metrics.Include),
slog.Any("exclude", originalConfig.Generator.Metrics.Exclude),
)
for _, el := range originalConfig.Generator.Metrics.ExcludeLabels {
if len(el.Labels) == 0 {
continue
Expand Down Expand Up @@ -97,6 +102,15 @@ func (uc *updateCommand) updateMetricsMetadata(ctx context.Context) error {
}

newMetrics := map[string]metadata.MetricInfo{}
if uc.Config.Generator.Metrics.Behavior == metadata.MetricsGenerationMerge {
for key, metric := range uc.Config.Metadata.Metrics {
if (len(uc.Include) > 0 && !validateRegularExpressions(uc.Include, key)) || validateRegularExpressions(uc.Exclude, key) {
continue
}
newMetrics[key] = metric
}
}

for key, info := range metricsInfo {
if len(info) == 0 {
continue
Expand All @@ -117,7 +131,6 @@ func (uc *updateCommand) updateMetricsMetadata(ctx context.Context) error {
Labels: labels,
}
}

uc.Config.Metadata.Metrics = newMetrics
return nil
}
Expand Down Expand Up @@ -185,7 +198,7 @@ func (uc *updateCommand) writeConfigFile() error {
var buf bytes.Buffer
writer := bufio.NewWriter(&buf)

_, _ = writer.WriteString("# yaml-language-server: $schema=../../jsonschema/configuration.json\n")
_, _ = writer.WriteString("# yaml-language-server: $schema=https://raw.githubusercontent.com/hasura/ndc-prometheus/main/jsonschema/configuration.json\n")
encoder := yaml.NewEncoder(writer)
encoder.SetIndent(2)
if err := encoder.Encode(uc.Config); err != nil {
Expand All @@ -203,6 +216,7 @@ var defaultConfiguration = metadata.Configuration{
Generator: metadata.GeneratorSettings{
Metrics: metadata.MetricsGeneratorSettings{
Enabled: true,
Behavior: metadata.MetricsGenerationMerge,
Include: []string{},
Exclude: []string{},
ExcludeLabels: []metadata.ExcludeLabelsSetting{},
Expand Down
1 change: 1 addition & 0 deletions connector-definition/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ connection_settings:
generator:
metrics:
enabled: true
behavior: merge
include: []
exclude: []
exclude_labels: []
Expand Down
12 changes: 11 additions & 1 deletion connector/metadata/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@ type Configuration struct {
Metadata Metadata `json:"metadata" yaml:"metadata"`
}

// MetricsGenerationBehavior the behavior of metrics generation
type MetricsGenerationBehavior string

const (
MetricsGenerationMerge = "merge"
MetricsGenerationReplace = "replace"
)

// MetricsGeneratorSettings contain settings for the metrics generation
type MetricsGeneratorSettings struct {
Enabled bool `json:"enabled" yaml:"enabled"`
// Enable the metrics generation
Enabled bool `json:"enabled" yaml:"enabled"`
Behavior MetricsGenerationBehavior `json:"behavior" yaml:"behavior" jsonschema:"enum=merge,enum=replace"`
// Include metrics with regular expression matching. Include all metrics by default
Include []string `json:"include" yaml:"include"`
// Exclude metrics with regular expression matching.
Expand Down
8 changes: 8 additions & 0 deletions jsonschema/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@
"enabled": {
"type": "boolean"
},
"behavior": {
"type": "string",
"enum": [
"merge",
"replace"
]
},
"include": {
"items": {
"type": "string"
Expand Down Expand Up @@ -283,6 +290,7 @@
"type": "object",
"required": [
"enabled",
"behavior",
"include",
"exclude",
"exclude_labels",
Expand Down
22 changes: 22 additions & 0 deletions tests/configuration/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ connection_settings:
generator:
metrics:
enabled: true
behavior: merge
include: []
exclude:
- ^prometheus_+
Expand All @@ -23,6 +24,20 @@ generator:
start_at: 2024-09-01T00:00:00Z
metadata:
metrics:
ndc_prometheus_query_total:
type: counter
description: Total number of query requests
labels:
collection: {}
http_status: {}
instance: {}
job: {}
otel_scope_name: {}
status: {}
ndc_prometheus_query_total_time:
type: histogram
description: Total time taken to plan and execute a query, in seconds
labels: {}
net_conntrack_dialer_conn_attempted_total:
type: counter
description: Total number of connections attempted by the given dialer a given name.
Expand All @@ -48,6 +63,13 @@ metadata:
instance: {}
job: {}
reason: {}
otel_scope_info:
type: gauge
description: Instrumentation Scope metadata
labels:
instance: {}
job: {}
otel_scope_name: {}
process_cpu_seconds_total:
type: counter
description: Total user and system CPU time spent in seconds.
Expand Down
Loading

0 comments on commit c8babc9

Please sign in to comment.