Skip to content

Commit

Permalink
feat: add profiler to query command in cli (#21006)
Browse files Browse the repository at this point in the history
  • Loading branch information
russorat authored Mar 23, 2021
1 parent 4ef09e1 commit 1b7d5f3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ or `/query` HTTP endpoints.
1. [20861](https://github.com/influxdata/influxdb/pull/20861): Update Telegraf plugins in UI to include additions and changes in 1.18 release.
1. [20894](https://github.com/influxdata/influxdb/pull/20894): Display task IDs in the UI.
1. [21046](https://github.com/influxdata/influxdb/pull/21046): Write to standard out when `--output-path -` is passed to `influxd inspect export-lp`.
1. [21006](https://github.com/influxdata/influxdb/pull/21006): Add `-p, --profilers` flag to `influx query` command.

### Bug Fixes

Expand Down
68 changes: 63 additions & 5 deletions cmd/influx/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
)

var queryFlags struct {
org organization
file string
raw bool
org organization
file string
raw bool
profilers []string
}

func cmdQuery(f *globalFlags, opts genericCLIOpts) *cobra.Command {
Expand All @@ -36,6 +37,7 @@ func cmdQuery(f *globalFlags, opts genericCLIOpts) *cobra.Command {
queryFlags.org.register(opts.viper, cmd, true)
cmd.Flags().StringVarP(&queryFlags.file, "file", "f", "", "Path to Flux query file")
cmd.Flags().BoolVarP(&queryFlags.raw, "raw", "r", false, "Display raw query results")
cmd.Flags().StringSliceVarP(&queryFlags.profilers, "profilers", "p", nil, "Names of Flux profilers to enable. Profiler information will be appended to query results")

return cmd
}
Expand Down Expand Up @@ -101,15 +103,21 @@ func fluxQueryF(cmd *cobra.Command, args []string) error {
}
u.RawQuery = params.Encode()

body, _ := json.Marshal(map[string]interface{}{
var body_map = map[string]interface{}{
"query": q,
"type": "flux",
"dialect": map[string]interface{}{
"annotations": []string{"group", "datatype", "default"},
"delimiter": ",",
"header": true,
},
})
}

if len(queryFlags.profilers) > 0 {
body_map["extern"] = buildProfilersExtern(queryFlags.profilers)
}

body, _ := json.Marshal(body_map)

req, _ := http.NewRequest("POST", u.String(), bytes.NewReader(body))
req.Header.Set("Authorization", "Token "+flags.config().Token)
Expand Down Expand Up @@ -155,6 +163,56 @@ func fluxQueryF(cmd *cobra.Command, args []string) error {
return results.Err()
}

// buildProfilersExtern constructs the AST representation of a Flux statement enabling
// the specified profilers in the query options.
//
// See the docs for more info: https://docs.influxdata.com/influxdb/cloud/reference/flux/stdlib/profiler/
func buildProfilersExtern(profilersToEnable []string) (profilersExtern map[string]interface{}) {
elements := make([]interface{}, len(profilersToEnable))
for i, profiler := range profilersToEnable {
elements[i] = map[string]interface{}{
"type": "StringLiteral",
"value": profiler,
}
}
profilersExtern = map[string]interface{}{
"type": "File",
"imports": []interface{}{
map[string]interface{}{
"type": "ImportDeclaration",
"path": map[string]interface{}{
"type": "StringLiteral",
"value": "profiler",
},
},
},
"body": []interface{}{
map[string]interface{}{
"assignment": map[string]interface{}{
"member": map[string]interface{}{
"object": map[string]interface{}{
"name": "profiler",
"type": "Identifier",
},
"property": map[string]interface{}{
"name": "enabledProfilers",
"type": "Identifier",
},
"type": "MemberExpression",
},
"init": map[string]interface{}{
"type": "ArrayExpression",
"elements": elements,
},
"type": "MemberAssignment",
},
"type": "OptionStatement",
},
},
}
return profilersExtern
}

// Below is a copy and trimmed version of the execute/format.go file from flux.
// It is copied here to avoid requiring a dependency on the execute package which
// may pull in the flux runtime as a dependency.
Expand Down

0 comments on commit 1b7d5f3

Please sign in to comment.