Skip to content

Commit

Permalink
Fixed the plugin initialization error by returning only the static ta…
Browse files Browse the repository at this point in the history
…bles if invalid config parameters were set for dynamic tables closes #38 (#39)
  • Loading branch information
misraved committed Feb 8, 2024
1 parent 25813d1 commit e5c652a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
18 changes: 12 additions & 6 deletions prometheus/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

type ctxKey struct{}
type ctxKey string

func Plugin(ctx context.Context) *plugin.Plugin {
p := &plugin.Plugin{
Expand All @@ -19,7 +19,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
NewInstance: ConfigInstance,
},
DefaultTransform: transform.FromGo(),
SchemaMode: plugin.SchemaModeDynamic,
SchemaMode: plugin.SchemaModeDynamic,
TableMapFunc: pluginTableDefinitions,
}
return p
Expand All @@ -38,15 +38,22 @@ func pluginTableDefinitions(ctx context.Context, p *plugin.TableMapData) (map[st
"prometheus_target": tablePrometheusTarget(ctx),
}

// Get list of metrics to create tables for from config
prometheusConfig := GetConfig(p.Connection)
if prometheusConfig.Metrics == nil {
return tables, nil
}

// Search for metrics to create as tables
metricNames, err := metricNameList(ctx, p)

if err != nil {
return nil, err
// Return only the static tables when encountering an error
return tables, err
}

for _, i := range metricNames {
tableCtx := context.WithValue(ctx, ctxKey{}, i)
tableCtx := context.WithValue(ctx, ctxKey("metric_name"), i)
base := filepath.Base(i)
tableName := base[0 : len(base)-len(filepath.Ext(base))]
// Add the table if it does not already exist, ensuring standard tables win
Expand All @@ -56,7 +63,7 @@ func pluginTableDefinitions(ctx context.Context, p *plugin.TableMapData) (map[st
plugin.Logger(ctx).Error("prometheus.pluginTableDefinitions", "table_already_exists", tableName)
}
}

return tables, nil
}

Expand Down Expand Up @@ -95,6 +102,5 @@ func metricNameList(ctx context.Context, p *plugin.TableMapData) ([]string, erro
for _, i := range warnings {
plugin.Logger(ctx).Error("prometheus.metricNameList", "warning", i)
}

return names, nil
}
2 changes: 1 addition & 1 deletion prometheus/table_prometheus_dynamic_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func tableDynamicMetric(ctx context.Context, p *plugin.TableMapData) *plugin.Tab
}

// Get the query for the metric (required)
metricName := ctx.Value("metric_name").(string)
metricName := ctx.Value(ctxKey("metric_name")).(string)
metricNameBytes, _ := json.Marshal(metricName)
q := fmt.Sprintf(`{__name__=%s}`, string(metricNameBytes))

Expand Down
8 changes: 5 additions & 3 deletions prometheus/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@ func connectRaw(ctx context.Context, cc *connection.ConnectionCache, c *plugin.C
Address: address,
})

conn := v1.NewAPI(client)

if err != nil {
return conn, err
plugin.Logger(ctx).Error("connectRaw", "client connection error", err)
return nil, err
}

conn := v1.NewAPI(client)

// Save to cache
err = cc.Set(ctx, cacheKey, conn)

if err != nil {
plugin.Logger(ctx).Error("connectRaw", "cache-set", err)
return conn, err
}

return conn, nil
Expand Down

0 comments on commit e5c652a

Please sign in to comment.