Skip to content

Commit

Permalink
pass promql service object to handler
Browse files Browse the repository at this point in the history
Signed-off-by: adarsh0728 <[email protected]>
  • Loading branch information
adarsh0728 committed Nov 4, 2024
1 parent 8555e77 commit ed4bf6c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 41 deletions.
10 changes: 1 addition & 9 deletions server/apis/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type handler struct {
}

// NewHandler is used to provide a new instance of the handler type
func NewHandler(ctx context.Context, dexObj *DexObject, localUsersAuthObject *LocalUsersAuthObject, opts ...HandlerOption) (*handler, error) {
func NewHandler(ctx context.Context, dexObj *DexObject, localUsersAuthObject *LocalUsersAuthObject, promQlService PromQl, opts ...HandlerOption) (*handler, error) {
var (
k8sRestConfig *rest.Config
err error
Expand All @@ -126,14 +126,6 @@ func NewHandler(ctx context.Context, dexObj *DexObject, localUsersAuthObject *Lo
_ = value.Close()
})

// create handler instance even if prometheus server details are not set
// the query API will respond with error message
prometheusMetricConfig := loadPrometheusMetricConfig()
// prom client instance
prometheusClient := NewPrometheusClient(prometheusMetricConfig.ServerUrl)
// prom ql builder service instance
promQlService := NewPromQlService(prometheusClient, prometheusMetricConfig)

o := defaultHandlerOptions()
for _, opt := range opts {
if opt != nil {
Expand Down
54 changes: 28 additions & 26 deletions server/apis/v1/promql_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,35 @@ func NewPromQlService(client *Prometheus, config *PrometheusConfig) PromQl {
expressions = make(map[string]map[string]string)
placeHolders = make(map[string]map[string][]string)
)
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 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
}
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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion 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 {
var (
data []byte
promConfig PrometheusConfig
Expand Down
17 changes: 12 additions & 5 deletions server/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func Routes(ctx context.Context, r *gin.Engine, sysInfo SystemInfo, authInfo Aut
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)

// noAuthGroup is a group of routes that do not require AuthN/AuthZ no matter whether auth is enabled.
noAuthGroup := r.Group(baseHref + "auth/v1")
v1RoutesNoAuth(noAuthGroup, dexObj, localUsersAuthObj)
Expand All @@ -73,9 +80,9 @@ func Routes(ctx context.Context, r *gin.Engine, sysInfo SystemInfo, authInfo Aut
}
// Add the AuthN/AuthZ middleware to the group.
r1Group.Use(authMiddleware(ctx, authorizer, dexObj, localUsersAuthObj, authRouteMap))
v1Routes(ctx, r1Group, dexObj, localUsersAuthObj, sysInfo.IsReadOnly, sysInfo.DaemonClientProtocol)
v1Routes(ctx, r1Group, dexObj, localUsersAuthObj, promQlServiceObj, sysInfo.IsReadOnly, sysInfo.DaemonClientProtocol)
} else {
v1Routes(ctx, r1Group, nil, nil, sysInfo.IsReadOnly, sysInfo.DaemonClientProtocol)
v1Routes(ctx, r1Group, nil, nil, promQlServiceObj, sysInfo.IsReadOnly, sysInfo.DaemonClientProtocol)
}
r1Group.GET("/sysinfo", func(c *gin.Context) {
c.JSON(http.StatusOK, v1.NewNumaflowAPIResponse(nil, sysInfo))
Expand All @@ -99,12 +106,12 @@ func v1RoutesNoAuth(r gin.IRouter, dexObj *v1.DexObject, localUsersAuthObject *v

// v1Routes defines the routes for the v1 API. For adding a new route, add a new handler function
// for the route along with an entry in the RouteMap in auth/route_map.go.
func v1Routes(ctx context.Context, r gin.IRouter, dexObj *v1.DexObject, localUsersAuthObject *v1.LocalUsersAuthObject, isReadOnly bool, daemonClientProtocol string) {
func v1Routes(ctx context.Context, r gin.IRouter, dexObj *v1.DexObject, localUsersAuthObject *v1.LocalUsersAuthObject, promQlServiceObj v1.PromQl, isReadOnly bool, daemonClientProtocol string) {
handlerOpts := []v1.HandlerOption{v1.WithDaemonClientProtocol(daemonClientProtocol)}
if isReadOnly {
handlerOpts = append(handlerOpts, v1.WithReadOnlyMode())
}
handler, err := v1.NewHandler(ctx, dexObj, localUsersAuthObject, handlerOpts...)
handler, err := v1.NewHandler(ctx, dexObj, localUsersAuthObject, promQlServiceObj, handlerOpts...)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -152,7 +159,7 @@ func v1Routes(ctx context.Context, r gin.IRouter, dexObj *v1.DexObject, localUse
r.GET("/metrics/namespaces/:namespace/pods", handler.ListPodsMetrics)
// Get pod logs.
r.GET("/namespaces/:namespace/pods/:pod/logs", handler.PodLogs)
// Get the pod metrics for a mono vertex
// Get the pod metrics for a mono vertex.
r.GET("/info/namespaces/:namespace/mono-vertices/:mono-vertex/pods", handler.GetMonoVertexPodsInfo)
// Get the pod metrics for a pipeline vertex.
r.GET("/info/namespaces/:namespace/pipelines/:pipeline/vertices/:vertex/pods", handler.GetVertexPodsInfo)
Expand Down

0 comments on commit ed4bf6c

Please sign in to comment.