forked from nats-io/jsm.go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nats-io#627 from ripienaar/jsm_options
Expect jsm options on JS related monitors
- Loading branch information
Showing
11 changed files
with
150 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//go:build !noexprlang | ||
|
||
package jsm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/expr-lang/expr" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func (q *streamQuery) matchExpression(streams []*Stream) ([]*Stream, error) { | ||
if q.expression == "" { | ||
return streams, nil | ||
} | ||
|
||
var matched []*Stream | ||
|
||
for _, stream := range streams { | ||
cfg := map[string]any{} | ||
state := map[string]any{} | ||
info := map[string]any{} | ||
|
||
cfgBytes, _ := yaml.Marshal(stream.Configuration()) | ||
yaml.Unmarshal(cfgBytes, &cfg) | ||
nfo, _ := stream.LatestInformation() | ||
nfoBytes, _ := yaml.Marshal(nfo) | ||
yaml.Unmarshal(nfoBytes, &info) | ||
stateBytes, _ := yaml.Marshal(nfo.State) | ||
yaml.Unmarshal(stateBytes, &state) | ||
|
||
env := map[string]any{ | ||
"config": cfg, | ||
"state": state, | ||
"info": info, | ||
"Info": nfo, | ||
} | ||
|
||
program, err := expr.Compile(q.expression, expr.Env(env), expr.AsBool()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out, err := expr.Run(program, env) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
should, ok := out.(bool) | ||
if !ok { | ||
return nil, fmt.Errorf("expression did not return a boolean") | ||
} | ||
|
||
if should { | ||
matched = append(matched, stream) | ||
} | ||
} | ||
|
||
return matched, nil | ||
} | ||
|
||
func (q *consumerQuery) matchExpression(consumers []*Consumer) ([]*Consumer, error) { | ||
if q.expression == "" { | ||
return consumers, nil | ||
} | ||
|
||
var matched []*Consumer | ||
|
||
for _, consumer := range consumers { | ||
cfg := map[string]any{} | ||
state := map[string]any{} | ||
|
||
cfgBytes, _ := yaml.Marshal(consumer.Configuration()) | ||
yaml.Unmarshal(cfgBytes, &cfg) | ||
nfo, _ := consumer.LatestState() | ||
stateBytes, _ := yaml.Marshal(nfo) | ||
yaml.Unmarshal(stateBytes, &state) | ||
|
||
env := map[string]any{ | ||
"config": cfg, | ||
"state": state, | ||
"info": state, | ||
"Info": nfo, | ||
} | ||
|
||
program, err := expr.Compile(q.expression, expr.Env(env), expr.AsBool()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out, err := expr.Run(program, env) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
should, ok := out.(bool) | ||
if !ok { | ||
return nil, fmt.Errorf("expression did not return a boolean") | ||
} | ||
|
||
if should { | ||
matched = append(matched, consumer) | ||
} | ||
} | ||
|
||
return matched, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build noexprlang | ||
|
||
package jsm | ||
|
||
import "fmt" | ||
|
||
// ErrNoExprLangBuild warns that expression matching is disabled when compiling | ||
// a go binary with the `noexprlang` build tag. | ||
var ErrNoExprLangBuild = fmt.Errorf("binary has been built with `noexprlang` build tag and thus does not support expression matching") | ||
|
||
func (q *streamQuery) matchExpression(streams []*Stream) ([]*Stream, error) { | ||
return nil, ErrNoExprLangBuild | ||
} | ||
|
||
func (q *consumerQuery) matchExpression(consumers []*Consumer) ([]*Consumer, error) { | ||
return nil, ErrNoExprLangBuild | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.