Skip to content

Commit

Permalink
pass promql service to handler && disableMetricChart from prom client…
Browse files Browse the repository at this point in the history
…/config

Signed-off-by: adarsh0728 <[email protected]>
  • Loading branch information
adarsh0728 committed Nov 4, 2024
1 parent ed4bf6c commit 040fba4
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 87 deletions.
3 changes: 0 additions & 3 deletions cmd/commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func NewServerCommand() *cobra.Command {
namespaced bool
managedNamespace string
baseHref string
disableMetricsCharts bool
disableAuth bool
serverAddr string
corsAllowedOrigins string
Expand Down Expand Up @@ -64,7 +63,6 @@ func NewServerCommand() *cobra.Command {
ServerAddr: serverAddr,
CorsAllowedOrigins: corsAllowedOrigins,
ReadOnly: readOnly,
DisableMetricsCharts: disableMetricsCharts,
DaemonClientProtocol: daemonClientProtocol,
}
server := svrcmd.NewServer(opts)
Expand All @@ -78,7 +76,6 @@ func NewServerCommand() *cobra.Command {
command.Flags().StringVar(&managedNamespace, "managed-namespace", sharedutil.LookupEnvStringOr("NUMAFLOW_SERVER_MANAGED_NAMESPACE", sharedutil.LookupEnvStringOr("NAMESPACE", "numaflow-system")), "The namespace that the server watches when \"--namespaced\" is \"true\".")
command.Flags().StringVar(&baseHref, "base-href", sharedutil.LookupEnvStringOr("NUMAFLOW_SERVER_BASE_HREF", "/"), "Base href for Numaflow server, defaults to '/'.")
command.Flags().BoolVar(&readOnly, "readonly", sharedutil.LookupEnvBoolOr("NUMAFLOW_SERVER_READONLY", false), "Whether to enable read only view for the UX server, defaults to false.")
command.Flags().BoolVar(&disableMetricsCharts, "disable-metrics-charts", sharedutil.LookupEnvBoolOr("NUMAFLOW_SERVER_DISABLE_METRICS_CHARTS", true), "Whether to disable metrics charts for the UX server, defaults to true.")
command.Flags().BoolVar(&disableAuth, "disable-auth", sharedutil.LookupEnvBoolOr("NUMAFLOW_SERVER_DISABLE_AUTH", false), "Whether to disable authentication and authorization, defaults to false.")
command.Flags().StringVar(&serverAddr, "server-addr", sharedutil.LookupEnvStringOr("NUMAFLOW_SERVER_ADDRESS", "https://localhost:8443"), "The external address of the Numaflow server.")
command.Flags().StringVar(&corsAllowedOrigins, "cors-allowed-origins", sharedutil.LookupEnvStringOr("NUMAFLOW_SERVER_CORS_ALLOWED_ORIGINS", ""), "The values for allowed cors AllowOrigins header field, separated by comma.")
Expand Down
6 changes: 0 additions & 6 deletions config/advanced-install/namespaced-numaflow-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,6 @@ spec:
key: server.readonly
name: numaflow-cmd-params-config
optional: true
- name: NUMAFLOW_SERVER_DISABLE_METRICS_CHARTS
valueFrom:
configMapKeyRef:
key: server.disable.metrics.charts
name: numaflow-cmd-params-config
optional: true
- name: NUMAFLOW_SERVER_DISABLE_AUTH
valueFrom:
configMapKeyRef:
Expand Down
6 changes: 0 additions & 6 deletions config/advanced-install/numaflow-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,6 @@ spec:
key: server.readonly
name: numaflow-cmd-params-config
optional: true
- name: NUMAFLOW_SERVER_DISABLE_METRICS_CHARTS
valueFrom:
configMapKeyRef:
key: server.disable.metrics.charts
name: numaflow-cmd-params-config
optional: true
- name: NUMAFLOW_SERVER_DISABLE_AUTH
valueFrom:
configMapKeyRef:
Expand Down
6 changes: 0 additions & 6 deletions config/base/numaflow-server/numaflow-server-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ spec:
name: numaflow-cmd-params-config
key: server.readonly
optional: true
- name: NUMAFLOW_SERVER_DISABLE_METRICS_CHARTS
valueFrom:
configMapKeyRef:
name: numaflow-cmd-params-config
key: server.disable.metrics.charts
optional: true
- name: NUMAFLOW_SERVER_DISABLE_AUTH
valueFrom:
configMapKeyRef:
Expand Down
3 changes: 0 additions & 3 deletions config/base/shared-config/numaflow-cmd-params-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ data:
### Whether to enable read only view for the UX server, defaults to false.
# server.readonly: "false"
#
### Whether to disable metrics charts for the UX server, defaults to true.
# server.disable.metrics.charts: "false"
#
### Whether to disable authentication and authorization for the UX server, defaults to false.
server.disable.auth: "true"
#
Expand Down
6 changes: 0 additions & 6 deletions config/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28821,12 +28821,6 @@ spec:
key: server.readonly
name: numaflow-cmd-params-config
optional: true
- name: NUMAFLOW_SERVER_DISABLE_METRICS_CHARTS
valueFrom:
configMapKeyRef:
key: server.disable.metrics.charts
name: numaflow-cmd-params-config
optional: true
- name: NUMAFLOW_SERVER_DISABLE_AUTH
valueFrom:
configMapKeyRef:
Expand Down
6 changes: 0 additions & 6 deletions config/namespace-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28700,12 +28700,6 @@ spec:
key: server.readonly
name: numaflow-cmd-params-config
optional: true
- name: NUMAFLOW_SERVER_DISABLE_METRICS_CHARTS
valueFrom:
configMapKeyRef:
key: server.disable.metrics.charts
name: numaflow-cmd-params-config
optional: true
- name: NUMAFLOW_SERVER_DISABLE_AUTH
valueFrom:
configMapKeyRef:
Expand Down
4 changes: 4 additions & 0 deletions server/apis/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,10 @@ func (h *handler) DiscoverMetrics(c *gin.Context) {
object := c.Param("object")

configData := h.promQlService.GetConfigData()
if configData == nil {
h.respondWithError(c, "Prometheus metric config is not available")
return
}

var discoveredMetrics MetricsDiscoveryResponse

Expand Down
4 changes: 4 additions & 0 deletions server/apis/v1/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,7 @@ func (m *mockPromQlService) GetConfigData() *PrometheusConfig {
func (m *mockPromQlService) PopulateReqMap(MetricsRequestBody) map[string]string {
return map[string]string{}
}

func (m *mockPromQlService) DisableMetricsChart() bool {
return m.configData == nil
}
105 changes: 66 additions & 39 deletions server/apis/v1/promql_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,29 @@ type Prometheus struct {
Api PrometheusAPI
}

func NewPrometheusClient(url string) *Prometheus {
func NewPrometheusClient(url string) (*Prometheus, error) {
if url == "" {
return nil
return nil, fmt.Errorf("prometheus server url is not set")
}
client, err := api.NewClient(api.Config{
Address: url,
})
if err != nil {
return nil
return nil, fmt.Errorf("failed to create prometheus client, %w", err)
}
v1api := v1.NewAPI(client)
return &Prometheus{
Client: client,
Api: v1api,
}
}, nil
}

type PromQl interface {
QueryPrometheus(context.Context, string, time.Time, time.Time) (model.Value, error)
BuildQuery(MetricsRequestBody) (string, error)
PopulateReqMap(MetricsRequestBody) map[string]string
GetConfigData() *PrometheusConfig
DisableMetricsChart() bool
}

type PromQlService struct {
Expand Down Expand Up @@ -103,51 +104,72 @@ func substitutePlaceHolders(expr string, placeholders []string, reqMap map[strin
}

// NewPromQlService creates a new PromQlService instance
func NewPromQlService(client *Prometheus, config *PrometheusConfig) PromQl {
func NewPromQlService() (PromQl, error) {
var (
// map[metric_name][dimension] = expr
// map of [metric_name][dimension] = expr
expressions = make(map[string]map[string]string)
placeHolders = make(map[string]map[string][]string)
client *Prometheus
config *PrometheusConfig
err error
)
if config != nil {
for _, pattern := range config.Patterns {
patternExpression := pattern.Expression
for _, metric := range pattern.Metrics {
metricName := metric.Name
for _, dimension := range metric.Dimensions {
dimensionName := dimension.Name
_, ok := expressions[metricName]
if !ok {
expressions[metricName] = make(map[string]string)
}
if dimension.Expression != "" {
expressions[metricName][dimensionName] = dimension.Expression
} else {
expressions[metricName][dimensionName] = patternExpression
}
expr := expressions[metricName][dimensionName]
placeHoldersArr := make([]string, 0)
re := regexp.MustCompile(`\$(\w+)`)
matches := re.FindAllStringSubmatch(expr, -1)
for _, match := range matches {
placeHoldersArr = append(placeHoldersArr, match[0])
}
_, ok = placeHolders[metricName]
if !ok {
placeHolders[metricName] = map[string][]string{}
}
placeHolders[metricName][dimensionName] = placeHoldersArr
}
}
}
}

return &PromQlService{
var service = &PromQlService{
Prometheus: client,
PlaceHolders: placeHolders,
Expression: expressions,
ConfigData: config,
}

// load prometheus metric config.
config, err = LoadPrometheusMetricConfig()
if err != nil {
// return service with nil config data & client. Do not return error as this is not critical.
return service, nil
}
service.ConfigData = config
// prom client instance.
client, err = NewPrometheusClient(config.ServerUrl)
if err != nil {
// return service with nil prometheus client. Do not return error as this is not critical.
return service, nil
}
service.Prometheus = client

for _, pattern := range config.Patterns {
patternExpression := pattern.Expression
for _, metric := range pattern.Metrics {
metricName := metric.Name
for _, dimension := range metric.Dimensions {
dimensionName := dimension.Name
_, ok := expressions[metricName]
if !ok {
expressions[metricName] = make(map[string]string)
}
if dimension.Expression != "" {
expressions[metricName][dimensionName] = dimension.Expression
} else {
expressions[metricName][dimensionName] = patternExpression
}
expr := expressions[metricName][dimensionName]
placeHoldersArr := make([]string, 0)
re := regexp.MustCompile(`\$(\w+)`)
matches := re.FindAllStringSubmatch(expr, -1)
for _, match := range matches {
placeHoldersArr = append(placeHoldersArr, match[0])
}
_, ok = placeHolders[metricName]
if !ok {
placeHolders[metricName] = map[string][]string{}
}
placeHolders[metricName][dimensionName] = placeHoldersArr
}
}
}

service.PlaceHolders = placeHolders
service.Expression = expressions
return service, nil
}

// PopulateReqMap populate map based on req fields
Expand Down Expand Up @@ -208,3 +230,8 @@ func (b *PromQlService) QueryPrometheus(ctx context.Context, promql string, star
func (b *PromQlService) GetConfigData() *PrometheusConfig {
return b.ConfigData
}

func (b *PromQlService) DisableMetricsChart() bool {
// disable metrics charts if metric config or prometheus client is nil
return b.ConfigData == nil || b.Prometheus == nil
}
8 changes: 4 additions & 4 deletions server/apis/v1/response_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type PrometheusConfig struct {
Patterns []PatternData `yaml:"patterns"`
}

func LoadPrometheusMetricConfig() *PrometheusConfig {
func LoadPrometheusMetricConfig() (*PrometheusConfig, error) {
var (
data []byte
promConfig PrometheusConfig
Expand All @@ -70,12 +70,12 @@ func LoadPrometheusMetricConfig() *PrometheusConfig {

data, err = os.ReadFile(metricsProxyConfigPath)
if err != nil {
return &PrometheusConfig{}
return nil, err
}
err = yaml.Unmarshal(data, &promConfig)

if err != nil {
return &PrometheusConfig{}
return nil, err
}
return &promConfig
return &promConfig, nil
}
2 changes: 0 additions & 2 deletions server/cmd/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type ServerOptions struct {
ServerAddr string
CorsAllowedOrigins string
ReadOnly bool
DisableMetricsCharts bool
DaemonClientProtocol string
}

Expand Down Expand Up @@ -107,7 +106,6 @@ func (s *server) Start(ctx context.Context) {
ManagedNamespace: s.options.ManagedNamespace,
Namespaced: s.options.Namespaced,
IsReadOnly: s.options.ReadOnly,
DisableMetricsCharts: s.options.DisableMetricsCharts,
Version: numaflow.GetVersion().String(),
DaemonClientProtocol: s.options.DaemonClientProtocol,
},
Expand Down
15 changes: 9 additions & 6 deletions server/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ func Routes(ctx context.Context, r *gin.Engine, sysInfo SystemInfo, authInfo Aut
if err != nil {
panic(err)
}
// promql service instance.
promQlServiceObj, err := v1.NewPromQlService()
if err != nil {
panic(err)
}

// load prometheus metric config.
prometheusMetricConfig := v1.LoadPrometheusMetricConfig()
// prom client instance.
prometheusClient := v1.NewPrometheusClient(prometheusMetricConfig.ServerUrl)
// prom ql builder service instance.
promQlServiceObj := v1.NewPromQlService(prometheusClient, prometheusMetricConfig)
// disable metrics charts if metric config or prometheus client is nil
if promQlServiceObj.DisableMetricsChart() {
sysInfo.DisableMetricsCharts = true
}

// noAuthGroup is a group of routes that do not require AuthN/AuthZ no matter whether auth is enabled.
noAuthGroup := r.Group(baseHref + "auth/v1")
Expand Down

0 comments on commit 040fba4

Please sign in to comment.