queryrunner

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2023 License: MIT Imports: 20 Imported by: 2

README

queryrunner

Latest GitHub release Github Actions test License

query-runner is a helper tool that makes querying several AWS services convenient

Usage

query-runner is a helper tool that makes querying several AWS services convenient

  usages:
    query-runner -l
    query-runner [options] <query_name1> <query_name2> ...
    cat params.json | query-runner [options]

  options:
    -c, --config        config dir, config format is HCL (defualt: ~/.config/query-runner/)
    -l, --list          displays a list of formats
    -o, --output        output format [json|table|markdown|borderless|vertical] (default:json)
    -v, --variables     variables json
    -h, --help          prints help information
        --log-level     log output level (default: info)

sample config and sample command is following

$ query-runner --variables '{"function_name": "helloworld"}' lambda_hello_world

~/.config/query-runner/config.hcl

query_runner "cloudwatch_logs_insights" "default" {
  region = "ap-northeast-1"
}

query "lambda_logs" {
  runner     = query_runner.cloudwatch_logs_insights.default
  start_time = strftime_in_zone("%Y-%m-%dT%H:%M:%S%z", "UTC", now() - duration("15m"))
  end_time   = strftime_in_zone("%Y-%m-%dT%H:%M:%S%z", "UTC", now())
  query      = <<EOT
fields @timestamp, @message
    | parse @message "[*] *" as loggingType, loggingMessage
    | filter loggingType = "${var == null ? "info" : var.log_level}"
    | display @timestamp, loggingType, loggingMessage
    | sort @timestamp desc
    | limit 2000
EOT
  log_group_name = "/aws/lambda/${var.function_name}"
}

query with cloudwatch logs insights

For other query runner, please refer to docs.

Install

Homebrew (macOS and Linux)
$ brew install mashiike/tap/queryrunner
Binary packages

Releases

Usage as go Library
package main 

import (
	"log"

	"github.com/mashiike/hclconfig"
	"github.com/mashiike/queryrunner"
)

func main() {
	var queries queryrunner.PreparedQueries
	if err := hclconfig.Load(&queries, "./"); err != nil {
		return err
	}
    query, ok := queries.Get("<your query name>")
	if !ok {
        log.Fatalln("query not found")
	}
	result, err := query.Run(egctx, p.MarshalCTYValues(), nil)
    if err != nil {
            log.Fatalln(err)
    }
	log.Println(result.ToTable())
}

Usage with AWS Lambda (serverless)

query-runner works with AWS Lambda and Amazon SQS.

sample payload:

{
  "queries": [
    "lambda_logs"
  ],
  "variables": {
    "function_name": "query-runner"
  }
}

sample output

{
  "results": {
    "lambda_logs": [
      {
        "@message": "END RequestId: 00000000-0000-0000-0000-000000000000\n",
        "@timestamp": "2022-10-12 09:22:49.638"
      },
      {
        "@message": "REPORT RequestId: 00000000-0000-0000-0000-000000000000\tDuration: 2336.30 ms\tBilled Duration: 2346 ms\tMemory Size: 128 MB\tMax Memory Used: 20 MB\tInit Duration: 8.97 ms\t\n",
        "@timestamp": "2022-10-12 09:22:49.638"
      },
      {
        "@message": "2022/10/12 18:22:49 [debug] finish run `lambda_logs` runner type `cloudwatch_logs_insights`\n",
        "@timestamp": "2022-10-12 09:22:49.637"
      },
      {
        "@message": "2022/10/12 18:22:49 [debug][00000000-0000-0000-0000-000000000000] query result: 0 results, 0 B scanned, 0.000000 records matched, 0.000000 recoreds scanned\n",
        "@timestamp": "2022-10-12 09:22:49.637"
      },
      {
        "@message": "2022/10/12 18:22:49 [debug][00000000-0000-0000-0000-000000000000] wating cloudwatch logs insights query elapsed_time=1.642910477s\n",
        "@timestamp": "2022-10-12 09:22:49.601"
      }
    ]
  }
}

Let's solidify the Lambda package with the following zip arcive (runtime provided.al2)

lambda.zip
├── bootstrap    # build binary
└── config.hcl   # configuration file

A related document is https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html

for example.

deploy two lambda functions, prepalert-http and prepalert-worker in lambda directory
The example of lambda directory uses lambroll for deployment.

For more information on the infrastructure around lambda functions, please refer to example.tf.

LICENSE

MIT License

Copyright (c) 2022 IKEDA Masashi

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRequestID

func GetRequestID(ctx context.Context) string

func Register

func Register(def *QueryRunnerDefinition) error

func WithRequestID

func WithRequestID(ctx context.Context, requestID string) context.Context

func WithTimeoutExtender

func WithTimeoutExtender(ctx context.Context, extender TimeoutExtender) context.Context

Types

type PreparedQueries

type PreparedQueries []PreparedQuery

func DecodeBody

func DecodeBody(body hcl.Body, ctx *hcl.EvalContext) (PreparedQueries, hcl.Body, hcl.Diagnostics)

func (*PreparedQueries) DecodeBody

func (queries *PreparedQueries) DecodeBody(body hcl.Body, ctx *hcl.EvalContext) hcl.Diagnostics

func (PreparedQueries) Get

func (queries PreparedQueries) Get(name string) (PreparedQuery, bool)

type PreparedQuery

type PreparedQuery interface {
	Name() string
	Description() string
	RunnerType() string
	Run(ctx context.Context, variables map[string]cty.Value, functions map[string]function.Function) (*QueryResult, error)
}

func TraversalQuery

func TraversalQuery(traversal hcl.Traversal, queries PreparedQueries) (PreparedQuery, error)

type QueryBase

type QueryBase struct {
	// contains filtered or unexported fields
}

func (*QueryBase) Body

func (q *QueryBase) Body() hcl.Body

func (*QueryBase) DecodeBody

func (q *QueryBase) DecodeBody(body hcl.Body, ctx *hcl.EvalContext, queryRunners QueryRunners) (PreparedQuery, hcl.Diagnostics)

func (*QueryBase) Description

func (q *QueryBase) Description() string

func (*QueryBase) Name

func (q *QueryBase) Name() string

func (*QueryBase) NewEvalContext

func (q *QueryBase) NewEvalContext(variables map[string]cty.Value, functions map[string]function.Function) *hcl.EvalContext

func (*QueryBase) Remain

func (q *QueryBase) Remain() hcl.Body

func (*QueryBase) Runner

func (q *QueryBase) Runner() QueryRunner

func (*QueryBase) RunnerType

func (q *QueryBase) RunnerType() string

type QueryResult

type QueryResult struct {
	Name    string
	Query   string
	Columns []string
	Rows    [][]string
}

func NewEmptyQueryResult

func NewEmptyQueryResult(name string, query string) *QueryResult

func NewQueryResult

func NewQueryResult(name string, query string, columns []string, rows [][]string) *QueryResult

func NewQueryResultWithJSONLines

func NewQueryResultWithJSONLines(name string, query string, lines [][]byte) *QueryResult

func NewQueryResultWithRowsMap

func NewQueryResultWithRowsMap(name, query string, columnsMap map[string]int, rowsMap []map[string]interface{}) *QueryResult

func (*QueryResult) MarshalCTYValue

func (qr *QueryResult) MarshalCTYValue() cty.Value

func (*QueryResult) MarshalJSON

func (qr *QueryResult) MarshalJSON() ([]byte, error)

func (*QueryResult) ToBorderlessTable

func (qr *QueryResult) ToBorderlessTable() string

func (*QueryResult) ToJSONLines

func (qr *QueryResult) ToJSONLines() string

func (*QueryResult) ToMarkdownTable

func (qr *QueryResult) ToMarkdownTable() string

func (*QueryResult) ToTable

func (qr *QueryResult) ToTable(optFns ...func(*tablewriter.Table)) string

func (*QueryResult) ToVertical

func (qr *QueryResult) ToVertical() string

type QueryResults

type QueryResults []*QueryResult

func (QueryResults) MarshalJSON

func (qrs QueryResults) MarshalJSON() ([]byte, error)

type QueryRunner

type QueryRunner interface {
	Name() string
	Type() string
	Prepare(base *QueryBase) (PreparedQuery, hcl.Diagnostics)
}

func NewQueryRunner

func NewQueryRunner(queryRunnerType string, name string, body hcl.Body, ctx *hcl.EvalContext) (QueryRunner, hcl.Diagnostics)

type QueryRunnerDefinition

type QueryRunnerDefinition struct {
	TypeName             string
	BuildQueryRunnerFunc func(name string, body hcl.Body, ctx *hcl.EvalContext) (QueryRunner, hcl.Diagnostics)
}

type QueryRunners

type QueryRunners []QueryRunner

func (QueryRunners) Get

func (runners QueryRunners) Get(queryRunnerType string, name string) (QueryRunner, bool)

type TimeoutExtender

type TimeoutExtender interface {
	ExtendTimeout(ctx context.Context, timeout time.Duration) error
}

func GetTimeoutExtender

func GetTimeoutExtender(ctx context.Context) TimeoutExtender

type TimeoutExtenderFunc

type TimeoutExtenderFunc func(ctx context.Context, timeout time.Duration) error

func (TimeoutExtenderFunc) ExtendTimeout

func (f TimeoutExtenderFunc) ExtendTimeout(ctx context.Context, timeout time.Duration) error

type Waiter

type Waiter struct {
	StartTime time.Time
	MinDelay  time.Duration
	MaxDelay  time.Duration
	Timeout   time.Duration
	Jitter    time.Duration
	// contains filtered or unexported fields
}

func (*Waiter) Continue

func (w *Waiter) Continue(ctx context.Context) bool

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL