From 5645e4e15ae23d11504ae23c199f93cde687cc03 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Tue, 7 Jan 2020 12:04:34 +0800 Subject: [PATCH] add monitor service --- build/docker/Dockerfile.monitor | 4 + cmd/climc/climc.go | 1 + cmd/climc/shell/monitor/alert.go | 55 ++ cmd/climc/shell/monitor/common.go | 27 + cmd/climc/shell/monitor/monitor.go | 56 ++ cmd/climc/shell/monitor/notification.go | 102 +++ cmd/climc/shell/nodealert.go | 33 +- cmd/monitor/main.go | 23 + go.mod | 5 +- go.sum | 11 +- pkg/apis/monitor/alert.go | 129 ++++ pkg/apis/monitor/doc.go | 1 + pkg/apis/monitor/meteralert.go | 60 ++ pkg/apis/monitor/nodealert.go | 213 ++++++ pkg/apis/monitor/notification.go | 106 +++ pkg/apis/monitor/template.go | 41 ++ pkg/apis/monitor/types.go | 103 +++ pkg/cloudcommon/db/enabledstatusstandalone.go | 73 ++- pkg/keystone/models/users.go | 2 +- pkg/mcclient/modules/managers.go | 12 +- pkg/mcclient/modules/mod_meteralert.go | 4 +- pkg/mcclient/modules/mod_nodealert.go | 4 +- pkg/mcclient/modules/monitor/alert.go | 41 ++ pkg/mcclient/modules/monitor/datasource.go | 31 + pkg/mcclient/modules/monitor/doc.go | 1 + pkg/mcclient/options/monitor/doc.go | 1 + pkg/mcclient/options/monitor/ds.go | 146 +++++ pkg/monitor/alerting/conditions/doc.go | 1 + pkg/monitor/alerting/conditions/evaluator.go | 145 +++++ .../alerting/conditions/evaluator_test.go | 68 ++ pkg/monitor/alerting/conditions/query.go | 268 ++++++++ pkg/monitor/alerting/conditions/query_test.go | 15 + pkg/monitor/alerting/conditions/reducer.go | 169 +++++ .../alerting/conditions/reducer_test.go | 202 ++++++ pkg/monitor/alerting/doc.go | 1 + pkg/monitor/alerting/engine.go | 240 +++++++ pkg/monitor/alerting/eval_context.go | 194 ++++++ pkg/monitor/alerting/eval_context_test.go | 220 +++++++ pkg/monitor/alerting/eval_handler.go | 83 +++ pkg/monitor/alerting/eval_handler_test.go | 216 +++++++ pkg/monitor/alerting/interfaces.go | 54 ++ pkg/monitor/alerting/job.go | 57 ++ pkg/monitor/alerting/notifier.go | 164 +++++ pkg/monitor/alerting/notifiers/base.go | 143 ++++ pkg/monitor/alerting/notifiers/dingding.go | 154 +++++ pkg/monitor/alerting/notifiers/doc.go | 1 + pkg/monitor/alerting/notifiers/feishu.go | 218 +++++++ pkg/monitor/alerting/notifiers/onecloud.go | 137 ++++ .../alerting/notifiers/templates/doc.go | 1 + .../alerting/notifiers/templates/template.go | 54 ++ .../alerting/notifiers/templates/util.go | 29 + pkg/monitor/alerting/notifiers/util.go | 130 ++++ pkg/monitor/alerting/reader.go | 54 ++ pkg/monitor/alerting/result_handler.go | 84 +++ pkg/monitor/alerting/rule.go | 155 +++++ pkg/monitor/alerting/scheduler.go | 98 +++ pkg/monitor/alerting/ticker.go | 70 ++ pkg/monitor/bus/bus.go | 238 +++++++ pkg/monitor/bus/bus_test.go | 144 +++++ pkg/monitor/bus/doc.go | 1 + pkg/monitor/expressions/doc.go | 1 + pkg/monitor/expressions/expressions.go | 80 +++ pkg/monitor/models/alert.go | 308 +++++++++ pkg/monitor/models/datasource.go | 169 +++++ pkg/monitor/models/doc.go | 1 + pkg/monitor/models/initdb.go | 39 ++ pkg/monitor/models/meteralert.go | 478 ++++++++++++++ pkg/monitor/models/nodealert.go | 611 ++++++++++++++++++ pkg/monitor/models/notification.go | 313 +++++++++ pkg/monitor/notifydrivers/doc.go | 1 + pkg/monitor/notifydrivers/drivers.go | 91 +++ pkg/monitor/notifydrivers/feishu/client.go | 132 ++++ pkg/monitor/notifydrivers/feishu/doc.go | 1 + pkg/monitor/notifydrivers/feishu/types.go | 226 +++++++ pkg/monitor/options/doc.go | 1 + pkg/monitor/options/options.go | 34 + pkg/monitor/registry/doc.go | 1 + pkg/monitor/registry/registry.go | 118 ++++ pkg/monitor/service/doc.go | 1 + pkg/monitor/service/handlers.go | 43 ++ pkg/monitor/service/service.go | 106 +++ pkg/monitor/tsdb/datasource.go | 170 +++++ pkg/monitor/tsdb/doc.go | 1 + pkg/monitor/tsdb/driver/influxdb/doc.go | 1 + pkg/monitor/tsdb/driver/influxdb/influxdb.go | 165 +++++ pkg/monitor/tsdb/driver/influxdb/models.go | 58 ++ pkg/monitor/tsdb/driver/influxdb/query.go | 172 +++++ .../tsdb/driver/influxdb/query_parser.go | 97 +++ .../tsdb/driver/influxdb/query_parser_test.go | 111 ++++ .../tsdb/driver/influxdb/query_part.go | 182 ++++++ .../tsdb/driver/influxdb/query_part_test.go | 58 ++ .../tsdb/driver/influxdb/query_test.go | 205 ++++++ .../tsdb/driver/influxdb/response_parser.go | 160 +++++ .../driver/influxdb/response_parser_test.go | 185 ++++++ pkg/monitor/tsdb/interval.go | 219 +++++++ pkg/monitor/tsdb/interval_test.go | 69 ++ pkg/monitor/tsdb/models.go | 114 ++++ pkg/monitor/tsdb/query_endpoint.go | 52 ++ pkg/monitor/tsdb/request.go | 30 + pkg/monitor/tsdb/time_range.go | 134 ++++ pkg/monitor/tsdb/time_range_test.go | 109 ++++ pkg/monitor/validators/doc.go | 1 + pkg/monitor/validators/validators.go | 161 +++++ scripts/docker_push.sh | 4 +- vendor/github.com/benbjohnson/clock/LICENSE | 21 + vendor/github.com/benbjohnson/clock/README.md | 104 +++ vendor/github.com/benbjohnson/clock/clock.go | 327 ++++++++++ vendor/github.com/benbjohnson/clock/go.mod | 3 + vendor/github.com/gopherjs/gopherjs/LICENSE | 24 + vendor/github.com/gopherjs/gopherjs/js/js.go | 168 +++++ vendor/github.com/jtolds/gls/LICENSE | 18 + vendor/github.com/jtolds/gls/README.md | 89 +++ vendor/github.com/jtolds/gls/context.go | 153 +++++ vendor/github.com/jtolds/gls/gen_sym.go | 21 + vendor/github.com/jtolds/gls/gid.go | 25 + vendor/github.com/jtolds/gls/id_pool.go | 34 + vendor/github.com/jtolds/gls/stack_tags.go | 147 +++++ vendor/github.com/jtolds/gls/stack_tags_js.go | 75 +++ .../github.com/jtolds/gls/stack_tags_main.go | 30 + .../smartystreets/assertions/.gitignore | 5 + .../smartystreets/assertions/.travis.yml | 11 + .../smartystreets/assertions/CONTRIBUTING.md | 12 + .../smartystreets/assertions/LICENSE.md | 23 + .../smartystreets/assertions/README.md | 611 ++++++++++++++++++ .../smartystreets/assertions/collections.go | 244 +++++++ .../smartystreets/assertions/doc.go | 109 ++++ .../smartystreets/assertions/equal_method.go | 75 +++ .../smartystreets/assertions/equality.go | 328 ++++++++++ .../smartystreets/assertions/filter.go | 31 + .../assertions/internal/go-render/LICENSE | 27 + .../internal/go-render/render/render.go | 481 ++++++++++++++ .../internal/go-render/render/render_time.go | 26 + .../internal/oglematchers/.gitignore | 5 + .../internal/oglematchers/.travis.yml | 4 + .../assertions/internal/oglematchers/LICENSE | 202 ++++++ .../internal/oglematchers/README.md | 58 ++ .../internal/oglematchers/any_of.go | 94 +++ .../internal/oglematchers/contains.go | 61 ++ .../internal/oglematchers/deep_equals.go | 88 +++ .../internal/oglematchers/equals.go | 541 ++++++++++++++++ .../internal/oglematchers/greater_or_equal.go | 39 ++ .../internal/oglematchers/greater_than.go | 39 ++ .../internal/oglematchers/less_or_equal.go | 41 ++ .../internal/oglematchers/less_than.go | 152 +++++ .../internal/oglematchers/matcher.go | 86 +++ .../assertions/internal/oglematchers/not.go | 53 ++ .../oglematchers/transform_description.go | 36 ++ .../smartystreets/assertions/messages.go | 97 +++ .../smartystreets/assertions/panic.go | 115 ++++ .../smartystreets/assertions/quantity.go | 141 ++++ .../smartystreets/assertions/serializer.go | 63 ++ .../smartystreets/assertions/strings.go | 227 +++++++ .../smartystreets/assertions/time.go | 202 ++++++ .../smartystreets/assertions/type.go | 134 ++++ .../smartystreets/goconvey/LICENSE.md | 23 + .../goconvey/convey/assertions.go | 71 ++ .../smartystreets/goconvey/convey/context.go | 272 ++++++++ .../goconvey/convey/convey.goconvey | 4 + .../goconvey/convey/discovery.go | 103 +++ .../smartystreets/goconvey/convey/doc.go | 218 +++++++ .../goconvey/convey/gotest/utils.go | 28 + .../smartystreets/goconvey/convey/init.go | 81 +++ .../goconvey/convey/nilReporter.go | 15 + .../goconvey/convey/reporting/console.go | 16 + .../goconvey/convey/reporting/doc.go | 5 + .../goconvey/convey/reporting/dot.go | 40 ++ .../goconvey/convey/reporting/gotest.go | 33 + .../goconvey/convey/reporting/init.go | 94 +++ .../goconvey/convey/reporting/json.go | 88 +++ .../goconvey/convey/reporting/printer.go | 60 ++ .../goconvey/convey/reporting/problems.go | 80 +++ .../goconvey/convey/reporting/reporter.go | 39 ++ .../convey/reporting/reporting.goconvey | 2 + .../goconvey/convey/reporting/reports.go | 179 +++++ .../goconvey/convey/reporting/statistics.go | 108 ++++ .../goconvey/convey/reporting/story.go | 73 +++ vendor/golang.org/x/xerrors/LICENSE | 27 + vendor/golang.org/x/xerrors/PATENTS | 22 + vendor/golang.org/x/xerrors/README | 2 + vendor/golang.org/x/xerrors/adaptor.go | 193 ++++++ vendor/golang.org/x/xerrors/codereview.cfg | 1 + vendor/golang.org/x/xerrors/doc.go | 22 + vendor/golang.org/x/xerrors/errors.go | 33 + vendor/golang.org/x/xerrors/fmt.go | 187 ++++++ vendor/golang.org/x/xerrors/format.go | 34 + vendor/golang.org/x/xerrors/frame.go | 56 ++ vendor/golang.org/x/xerrors/go.mod | 3 + .../golang.org/x/xerrors/internal/internal.go | 8 + vendor/golang.org/x/xerrors/wrap.go | 106 +++ vendor/modules.txt | 19 +- .../x/pkg/util/reflectutils/jsonfield.go | 24 + .../pkg/util/signalutils/dumpstack_others.go | 1 + 192 files changed, 18327 insertions(+), 49 deletions(-) create mode 100644 build/docker/Dockerfile.monitor create mode 100644 cmd/climc/shell/monitor/alert.go create mode 100644 cmd/climc/shell/monitor/common.go create mode 100644 cmd/climc/shell/monitor/monitor.go create mode 100644 cmd/climc/shell/monitor/notification.go create mode 100644 cmd/monitor/main.go create mode 100644 pkg/apis/monitor/alert.go create mode 100644 pkg/apis/monitor/doc.go create mode 100644 pkg/apis/monitor/meteralert.go create mode 100644 pkg/apis/monitor/nodealert.go create mode 100644 pkg/apis/monitor/notification.go create mode 100644 pkg/apis/monitor/template.go create mode 100644 pkg/apis/monitor/types.go create mode 100644 pkg/mcclient/modules/monitor/alert.go create mode 100644 pkg/mcclient/modules/monitor/datasource.go create mode 100644 pkg/mcclient/modules/monitor/doc.go create mode 100644 pkg/mcclient/options/monitor/doc.go create mode 100644 pkg/mcclient/options/monitor/ds.go create mode 100644 pkg/monitor/alerting/conditions/doc.go create mode 100644 pkg/monitor/alerting/conditions/evaluator.go create mode 100644 pkg/monitor/alerting/conditions/evaluator_test.go create mode 100644 pkg/monitor/alerting/conditions/query.go create mode 100644 pkg/monitor/alerting/conditions/query_test.go create mode 100644 pkg/monitor/alerting/conditions/reducer.go create mode 100644 pkg/monitor/alerting/conditions/reducer_test.go create mode 100644 pkg/monitor/alerting/doc.go create mode 100644 pkg/monitor/alerting/engine.go create mode 100644 pkg/monitor/alerting/eval_context.go create mode 100644 pkg/monitor/alerting/eval_context_test.go create mode 100644 pkg/monitor/alerting/eval_handler.go create mode 100644 pkg/monitor/alerting/eval_handler_test.go create mode 100644 pkg/monitor/alerting/interfaces.go create mode 100644 pkg/monitor/alerting/job.go create mode 100644 pkg/monitor/alerting/notifier.go create mode 100644 pkg/monitor/alerting/notifiers/base.go create mode 100644 pkg/monitor/alerting/notifiers/dingding.go create mode 100644 pkg/monitor/alerting/notifiers/doc.go create mode 100644 pkg/monitor/alerting/notifiers/feishu.go create mode 100644 pkg/monitor/alerting/notifiers/onecloud.go create mode 100644 pkg/monitor/alerting/notifiers/templates/doc.go create mode 100644 pkg/monitor/alerting/notifiers/templates/template.go create mode 100644 pkg/monitor/alerting/notifiers/templates/util.go create mode 100644 pkg/monitor/alerting/notifiers/util.go create mode 100644 pkg/monitor/alerting/reader.go create mode 100644 pkg/monitor/alerting/result_handler.go create mode 100644 pkg/monitor/alerting/rule.go create mode 100644 pkg/monitor/alerting/scheduler.go create mode 100644 pkg/monitor/alerting/ticker.go create mode 100644 pkg/monitor/bus/bus.go create mode 100644 pkg/monitor/bus/bus_test.go create mode 100644 pkg/monitor/bus/doc.go create mode 100644 pkg/monitor/expressions/doc.go create mode 100644 pkg/monitor/expressions/expressions.go create mode 100644 pkg/monitor/models/alert.go create mode 100644 pkg/monitor/models/datasource.go create mode 100644 pkg/monitor/models/doc.go create mode 100644 pkg/monitor/models/initdb.go create mode 100644 pkg/monitor/models/meteralert.go create mode 100644 pkg/monitor/models/nodealert.go create mode 100644 pkg/monitor/models/notification.go create mode 100644 pkg/monitor/notifydrivers/doc.go create mode 100644 pkg/monitor/notifydrivers/drivers.go create mode 100644 pkg/monitor/notifydrivers/feishu/client.go create mode 100644 pkg/monitor/notifydrivers/feishu/doc.go create mode 100644 pkg/monitor/notifydrivers/feishu/types.go create mode 100644 pkg/monitor/options/doc.go create mode 100644 pkg/monitor/options/options.go create mode 100644 pkg/monitor/registry/doc.go create mode 100644 pkg/monitor/registry/registry.go create mode 100644 pkg/monitor/service/doc.go create mode 100644 pkg/monitor/service/handlers.go create mode 100644 pkg/monitor/service/service.go create mode 100644 pkg/monitor/tsdb/datasource.go create mode 100644 pkg/monitor/tsdb/doc.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/doc.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/influxdb.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/models.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/query.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/query_parser.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/query_parser_test.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/query_part.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/query_part_test.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/query_test.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/response_parser.go create mode 100644 pkg/monitor/tsdb/driver/influxdb/response_parser_test.go create mode 100644 pkg/monitor/tsdb/interval.go create mode 100644 pkg/monitor/tsdb/interval_test.go create mode 100644 pkg/monitor/tsdb/models.go create mode 100644 pkg/monitor/tsdb/query_endpoint.go create mode 100644 pkg/monitor/tsdb/request.go create mode 100644 pkg/monitor/tsdb/time_range.go create mode 100644 pkg/monitor/tsdb/time_range_test.go create mode 100644 pkg/monitor/validators/doc.go create mode 100644 pkg/monitor/validators/validators.go create mode 100644 vendor/github.com/benbjohnson/clock/LICENSE create mode 100644 vendor/github.com/benbjohnson/clock/README.md create mode 100644 vendor/github.com/benbjohnson/clock/clock.go create mode 100644 vendor/github.com/benbjohnson/clock/go.mod create mode 100644 vendor/github.com/gopherjs/gopherjs/LICENSE create mode 100644 vendor/github.com/gopherjs/gopherjs/js/js.go create mode 100644 vendor/github.com/jtolds/gls/LICENSE create mode 100644 vendor/github.com/jtolds/gls/README.md create mode 100644 vendor/github.com/jtolds/gls/context.go create mode 100644 vendor/github.com/jtolds/gls/gen_sym.go create mode 100644 vendor/github.com/jtolds/gls/gid.go create mode 100644 vendor/github.com/jtolds/gls/id_pool.go create mode 100644 vendor/github.com/jtolds/gls/stack_tags.go create mode 100644 vendor/github.com/jtolds/gls/stack_tags_js.go create mode 100644 vendor/github.com/jtolds/gls/stack_tags_main.go create mode 100644 vendor/github.com/smartystreets/assertions/.gitignore create mode 100644 vendor/github.com/smartystreets/assertions/.travis.yml create mode 100644 vendor/github.com/smartystreets/assertions/CONTRIBUTING.md create mode 100644 vendor/github.com/smartystreets/assertions/LICENSE.md create mode 100644 vendor/github.com/smartystreets/assertions/README.md create mode 100644 vendor/github.com/smartystreets/assertions/collections.go create mode 100644 vendor/github.com/smartystreets/assertions/doc.go create mode 100644 vendor/github.com/smartystreets/assertions/equal_method.go create mode 100644 vendor/github.com/smartystreets/assertions/equality.go create mode 100644 vendor/github.com/smartystreets/assertions/filter.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/go-render/LICENSE create mode 100644 vendor/github.com/smartystreets/assertions/internal/go-render/render/render.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/go-render/render/render_time.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/.gitignore create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/.travis.yml create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/LICENSE create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/README.md create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/any_of.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/contains.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/deep_equals.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/equals.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/greater_or_equal.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/greater_than.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/less_or_equal.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/less_than.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/matcher.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/not.go create mode 100644 vendor/github.com/smartystreets/assertions/internal/oglematchers/transform_description.go create mode 100644 vendor/github.com/smartystreets/assertions/messages.go create mode 100644 vendor/github.com/smartystreets/assertions/panic.go create mode 100644 vendor/github.com/smartystreets/assertions/quantity.go create mode 100644 vendor/github.com/smartystreets/assertions/serializer.go create mode 100644 vendor/github.com/smartystreets/assertions/strings.go create mode 100644 vendor/github.com/smartystreets/assertions/time.go create mode 100644 vendor/github.com/smartystreets/assertions/type.go create mode 100644 vendor/github.com/smartystreets/goconvey/LICENSE.md create mode 100644 vendor/github.com/smartystreets/goconvey/convey/assertions.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/context.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/convey.goconvey create mode 100644 vendor/github.com/smartystreets/goconvey/convey/discovery.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/doc.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/gotest/utils.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/init.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/nilReporter.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/console.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/doc.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/dot.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/gotest.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/init.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/json.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/printer.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/problems.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/reporter.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/reporting.goconvey create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/reports.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/statistics.go create mode 100644 vendor/github.com/smartystreets/goconvey/convey/reporting/story.go create mode 100644 vendor/golang.org/x/xerrors/LICENSE create mode 100644 vendor/golang.org/x/xerrors/PATENTS create mode 100644 vendor/golang.org/x/xerrors/README create mode 100644 vendor/golang.org/x/xerrors/adaptor.go create mode 100644 vendor/golang.org/x/xerrors/codereview.cfg create mode 100644 vendor/golang.org/x/xerrors/doc.go create mode 100644 vendor/golang.org/x/xerrors/errors.go create mode 100644 vendor/golang.org/x/xerrors/fmt.go create mode 100644 vendor/golang.org/x/xerrors/format.go create mode 100644 vendor/golang.org/x/xerrors/frame.go create mode 100644 vendor/golang.org/x/xerrors/go.mod create mode 100644 vendor/golang.org/x/xerrors/internal/internal.go create mode 100644 vendor/golang.org/x/xerrors/wrap.go diff --git a/build/docker/Dockerfile.monitor b/build/docker/Dockerfile.monitor new file mode 100644 index 0000000000..de767c4d7d --- /dev/null +++ b/build/docker/Dockerfile.monitor @@ -0,0 +1,4 @@ +FROM registry.cn-beijing.aliyuncs.com/yunionio/onecloud-base:latest + +ADD ./_output/bin/monitor /opt/yunion/bin/monitor + diff --git a/cmd/climc/climc.go b/cmd/climc/climc.go index 31430dd147..4605073277 100644 --- a/cmd/climc/climc.go +++ b/cmd/climc/climc.go @@ -35,6 +35,7 @@ import ( _ "yunion.io/x/onecloud/cmd/climc/shell/cloudnet" _ "yunion.io/x/onecloud/cmd/climc/shell/etcd" _ "yunion.io/x/onecloud/cmd/climc/shell/k8s" + _ "yunion.io/x/onecloud/cmd/climc/shell/monitor" "yunion.io/x/onecloud/pkg/mcclient" ) diff --git a/cmd/climc/shell/monitor/alert.go b/cmd/climc/shell/monitor/alert.go new file mode 100644 index 0000000000..4996d06c85 --- /dev/null +++ b/cmd/climc/shell/monitor/alert.go @@ -0,0 +1,55 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modules/monitor" + options "yunion.io/x/onecloud/pkg/mcclient/options/monitor" +) + +func init() { + aN := cmdN("alert") + R(&options.AlertListOptions{}, aN("list"), "List all alerts", + func(s *mcclient.ClientSession, args *options.AlertListOptions) error { + params, err := args.Params() + if err != nil { + return err + } + ret, err := monitor.Alerts.List(s, params) + if err != nil { + return err + } + printList(ret, monitor.Alerts.GetColumns(s)) + return nil + }) + + R(&options.AlertShowOptions{}, aN("show"), "Show details of a alert rule", + func(s *mcclient.ClientSession, args *options.AlertShowOptions) error { + ret, err := monitor.Alerts.Get(s, args.ID, nil) + if err != nil { + return err + } + printObject(ret) + return nil + }) + + R(&options.AlertDeleteOptions{}, aN("delete"), "Delete alerts", + func(s *mcclient.ClientSession, args *options.AlertDeleteOptions) error { + ret := monitor.Alerts.BatchDelete(s, args.ID, nil) + printBatchResults(ret, monitor.Alerts.GetColumns(s)) + return nil + }) +} diff --git a/cmd/climc/shell/monitor/common.go b/cmd/climc/shell/monitor/common.go new file mode 100644 index 0000000000..9299db260d --- /dev/null +++ b/cmd/climc/shell/monitor/common.go @@ -0,0 +1,27 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "yunion.io/x/onecloud/cmd/climc/shell" + "yunion.io/x/onecloud/pkg/util/printutils" +) + +var ( + R = shell.R + printList = printutils.PrintJSONList + printObject = printutils.PrintJSONObject + printBatchResults = printutils.PrintJSONBatchResults +) diff --git a/cmd/climc/shell/monitor/monitor.go b/cmd/climc/shell/monitor/monitor.go new file mode 100644 index 0000000000..5839619223 --- /dev/null +++ b/cmd/climc/shell/monitor/monitor.go @@ -0,0 +1,56 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "fmt" + + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modules/monitor" + options "yunion.io/x/onecloud/pkg/mcclient/options/monitor" +) + +func cmdN(suffix string) func(action string) string { + return func(action string) string { + return fmt.Sprintf("monitor-%s-%s", suffix, action) + } +} + +func init() { + dsN := cmdN("datasource") + R(&options.DataSourceListOptions{}, dsN("list"), "List all monitor data source", + func(s *mcclient.ClientSession, args *options.DataSourceListOptions) error { + params, err := args.Params() + if err != nil { + return err + } + ret, err := monitor.DataSources.List(s, params) + if err != nil { + return err + } + printList(ret, monitor.DataSources.GetColumns(s)) + return nil + }) + + R(&options.DataSourceDeleteOptions{}, dsN("delete"), "Delete monitor data source", + func(s *mcclient.ClientSession, args *options.DataSourceDeleteOptions) error { + ret, err := monitor.DataSources.Delete(s, args.ID, nil) + if err != nil { + return err + } + printObject(ret) + return nil + }) +} diff --git a/cmd/climc/shell/monitor/notification.go b/cmd/climc/shell/monitor/notification.go new file mode 100644 index 0000000000..9f19bb4e20 --- /dev/null +++ b/cmd/climc/shell/monitor/notification.go @@ -0,0 +1,102 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modules/monitor" + options "yunion.io/x/onecloud/pkg/mcclient/options/monitor" +) + +func init() { + nN := cmdN("notification") + R(&options.NotificationListOptions{}, nN("list"), "List all alert notification", + func(s *mcclient.ClientSession, args *options.NotificationListOptions) error { + params, err := args.Params() + if err != nil { + return err + } + ret, err := monitor.AlertNotifications.List(s, params) + if err != nil { + return err + } + printList(ret, monitor.AlertNotifications.GetColumns(s)) + return nil + }) + + R(&options.NotificationDingDingCreateOptions{}, nN("create-dingding"), + "Create dingding alert notification", + func(s *mcclient.ClientSession, args *options.NotificationDingDingCreateOptions) error { + params, err := args.Params() + if err != nil { + return err + } + ret, err := monitor.AlertNotifications.Create(s, params.JSON(params)) + if err != nil { + return err + } + printObject(ret) + return nil + }) + + R(&options.NotificationFeishuCreateOptions{}, nN("create-feishu"), + "Create feishu alert notification", + func(s *mcclient.ClientSession, args *options.NotificationFeishuCreateOptions) error { + params, err := args.Params() + if err != nil { + return err + } + ret, err := monitor.AlertNotifications.Create(s, params.JSON(params)) + if err != nil { + return err + } + printObject(ret) + return nil + }) + + R(&options.NotificationShowOptions{}, nN("show"), "Show alert notification", + func(s *mcclient.ClientSession, args *options.NotificationShowOptions) error { + ret, err := monitor.AlertNotifications.Get(s, args.ID, nil) + if err != nil { + return err + } + printObject(ret) + return nil + }) + + R(&options.NotificationUpdateOptions{}, nN("update"), "Update alert notification", + func(s *mcclient.ClientSession, args *options.NotificationUpdateOptions) error { + params, err := args.Params() + if err != nil { + return err + } + ret, err := monitor.AlertNotifications.Update(s, args.ID, params.JSON(params)) + if err != nil { + return err + } + printObject(ret) + return nil + }) + + R(&options.NotificationShowOptions{}, nN("delete"), "Show delete notification", + func(s *mcclient.ClientSession, args *options.NotificationShowOptions) error { + ret, err := monitor.AlertNotifications.Delete(s, args.ID, nil) + if err != nil { + return err + } + printObject(ret) + return nil + }) +} diff --git a/cmd/climc/shell/nodealert.go b/cmd/climc/shell/nodealert.go index 81885697ca..6ee25d7547 100644 --- a/cmd/climc/shell/nodealert.go +++ b/cmd/climc/shell/nodealert.go @@ -59,18 +59,18 @@ func init() { * 修改指定的报警规则 */ type NodealertUpdateOptions struct { - ID string `help:"ID of the alert rule" required:"true" positional:"true"` - Type string `help:"Alert rule type" choices:"guest|host"` - Metric string `help:"Metric name, include measurement and field, such as vm_cpu.usage_active"` - NodeName string `help:"Name of the guest or host"` - NodeID string `help:"ID of the guest or host"` - Period string `help:"Specify the query time period for the data"` - Window string `help:"Specify the query interval for the data"` - Threshold float64 `help:"Threshold value of the metric"` - Comparator string `help:"Comparison operator for join expressions" choices:">|<|>=|<=|=|!="` - Recipients string `help:"Comma separated recipient ID"` - Level string `help:"Alert level" choices:"normal|important|fatal"` - Channel string `help:"Ways to send an alarm" choices:"email|mobile"` + ID string `help:"ID of the alert rule" required:"true" positional:"true"` + Type string `help:"Alert rule type" choices:"guest|host"` + Metric string `help:"Metric name, include measurement and field, such as vm_cpu.usage_active"` + NodeName string `help:"Name of the guest or host"` + NodeID string `help:"ID of the guest or host"` + Period string `help:"Specify the query time period for the data"` + Window string `help:"Specify the query interval for the data"` + Threshold *float64 `help:"Threshold value of the metric"` + Comparator string `help:"Comparison operator for join expressions" choices:">|<|>=|<=|=|!="` + Recipients string `help:"Comma separated recipient ID"` + Level string `help:"Alert level" choices:"normal|important|fatal"` + Channel string `help:"Ways to send an alarm" choices:"email|mobile"` } R(&NodealertUpdateOptions{}, "nodealert-update", "Update the node alert rule", func(s *mcclient.ClientSession, args *NodealertUpdateOptions) error { params, err := options.StructToParams(args) @@ -92,14 +92,11 @@ func init() { * 删除指定ID的报警规则 */ type NodealertDeleteOptions struct { - ID string `help:"ID of node alert" required:"true" positional:"true"` + ID []string `help:"ID of node alert" required:"true" positional:"true"` } R(&NodealertDeleteOptions{}, "nodealert-delete", "Delete a node alert", func(s *mcclient.ClientSession, args *NodealertDeleteOptions) error { - alarm, err := modules.NodeAlert.Delete(s, args.ID, nil) - if err != nil { - return err - } - printObject(alarm) + ret := modules.NodeAlert.BatchDelete(s, args.ID, nil) + printBatchResults(ret, modules.NodeAlert.GetColumns(s)) return nil }) diff --git a/cmd/monitor/main.go b/cmd/monitor/main.go new file mode 100644 index 0000000000..120719543d --- /dev/null +++ b/cmd/monitor/main.go @@ -0,0 +1,23 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "yunion.io/x/onecloud/pkg/monitor/service" +) + +func main() { + service.StartService() +} diff --git a/go.mod b/go.mod index 427d59b256..bb45ac765e 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/aokoli/goutils v1.0.1 github.com/aws/aws-sdk-go v1.21.4 github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect + github.com/benbjohnson/clock v1.0.0 github.com/bitly/go-simplejson v0.5.0 github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect github.com/c-bata/go-prompt v0.2.1 @@ -101,6 +102,7 @@ require ( github.com/shirou/gopsutil v2.18.10+incompatible github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9 + github.com/smartystreets/goconvey v1.6.4 github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spf13/pflag v1.0.3 // indirect github.com/stretchr/testify v1.4.0 @@ -121,6 +123,7 @@ require ( golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 golang.org/x/sync v0.0.0-20190423024810-112230192c58 golang.org/x/sys v0.0.0-20191008105621-543471e840be + golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 golang.zx2c4.com/wireguard/wgctrl v0.0.0-20191008142428-8d021180e987 google.golang.org/api v0.13.0 // indirect google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873 @@ -140,7 +143,7 @@ require ( yunion.io/x/executor v0.0.0-20200227030256-a18417815e74 yunion.io/x/jsonutils v0.0.0-20200113074440-9297fd00ba07 yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d - yunion.io/x/pkg v0.0.0-20200227105015-b0738bd1ffe9 + yunion.io/x/pkg v0.0.0-20200302034534-fdf44d54b070 yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e yunion.io/x/sqlchemy v0.0.0-20200221103553-6a98f7f8ab92 yunion.io/x/structarg v0.0.0-20190809075558-115bed041de3 diff --git a/go.sum b/go.sum index 1eeb9abfad..e16c5276c9 100644 --- a/go.sum +++ b/go.sum @@ -80,6 +80,8 @@ github.com/aws/aws-sdk-go v1.21.4 h1:1xB+x6Dzev8ETmeHEiSfUVbIzmC/0EyFfXMkJpzKPCE github.com/aws/aws-sdk-go v1.21.4/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f h1:ZNv7On9kyUzm7fvRZumSyy/IUiSC7AzL0I1jKKtwooA= github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc= +github.com/benbjohnson/clock v1.0.0 h1:78Jk/r6m4wCi6sndMpty7A//t4dw/RW5fV4ZgDVfX1w= +github.com/benbjohnson/clock v1.0.0/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0= @@ -219,6 +221,7 @@ github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gopacket v1.1.17 h1:rMrlX2ZY2UbvT+sdz3+6J+pp2z+msCq9MxTU6ymxbBY= github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= +github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -449,6 +452,8 @@ github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a h1:JSvGDIbm github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s= github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a h1:pa8hGb/2YqsZKovtsgrwcDH1RZhVbTKCjLp47XpqCDs= github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/gunit v0.0.0-20180314194857-6f0d6275bdcd h1:p5kvxG4NHogJX1brTLtvUSGdW0/aBvIyqDSW7tmnsmQ= github.com/smartystreets/gunit v0.0.0-20180314194857-6f0d6275bdcd/go.mod h1:XUKj4gbqj2QvJk/OdLWzyZ3FYli0f+MdpngyryX0gcw= github.com/soheilhy/cmux v0.1.4 h1:0HKaf1o97UwFjHH9o5XsHUOF+tqmdA7KEzXLpiyaw0E= @@ -618,6 +623,8 @@ golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.zx2c4.com/wireguard v0.0.20190908 h1:SUoXDdwSMtomLdvke+zz83/u9tNvl4hHmcTIWp38tow= golang.zx2c4.com/wireguard v0.0.20190908/go.mod h1:LhfXh5z6bLC2lW2ve6BzYZFwnnsXK3OQjySR0Yh2dO8= golang.zx2c4.com/wireguard/wgctrl v0.0.0-20191008142428-8d021180e987 h1:26OAgqBTufVr8WKonCEhhjO1oKsYhHv0iM5Dg92G1TM= @@ -705,8 +712,8 @@ yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf/go.mod h1:t6rEGG2sQ4J7DhFxSZV yunion.io/x/pkg v0.0.0-20190628082551-f4033ba2ea30/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/pkg v0.0.0-20200103043034-27c6f82160fa h1:+7zYi8MhaOW/53/7FOERnhQqAU4UhgaOVIS+AMzTKNU= yunion.io/x/pkg v0.0.0-20200103043034-27c6f82160fa/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= -yunion.io/x/pkg v0.0.0-20200227105015-b0738bd1ffe9 h1:O6/7+SUm2MDVC8TUEjn6GBeVFPvDprg0TFEl8A+aas8= -yunion.io/x/pkg v0.0.0-20200227105015-b0738bd1ffe9/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= +yunion.io/x/pkg v0.0.0-20200302034534-fdf44d54b070 h1:rKnYgtvMHKmzPEUTkyNjyKOG7wzjpUvI7fcZwLNGQXw= +yunion.io/x/pkg v0.0.0-20200302034534-fdf44d54b070/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo= yunion.io/x/sqlchemy v0.0.0-20200221103553-6a98f7f8ab92 h1:Iz70/alKMAW3KeePhmExuhWsYw1MGTcMr5ewAL5lj1I= diff --git a/pkg/apis/monitor/alert.go b/pkg/apis/monitor/alert.go new file mode 100644 index 0000000000..57e93405da --- /dev/null +++ b/pkg/apis/monitor/alert.go @@ -0,0 +1,129 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/apis" +) + +type AlertStateType string +type AlertSeverityType string +type NoDataOption string +type ExecutionErrorOption string + +const ( + AlertStateNoData AlertStateType = "no_data" + AlertStatePaused AlertStateType = "paused" + AlertStateAlerting AlertStateType = "alerting" + AlertStateOK AlertStateType = "ok" + AlertStatePending AlertStateType = "pending" + AlertStateUnknown AlertStateType = "unknown" +) + +const ( + NoDataSetOK NoDataOption = "ok" + NoDataSetNoData NoDataOption = "no_data" + NoDataKeepState NoDataOption = "keep_state" + NoDataSetAlerting NoDataOption = "alerting" +) + +const ( + ExecutionErrorSetAlerting ExecutionErrorOption = "alerting" + ExecutionErrorKeepState ExecutionErrorOption = "keep_state" +) + +var ( + ErrCannotChangeStateOnPausedAlert = errors.Error("Cannot change state on pause alert") + ErrRequiresNewState = errors.Error("update alert state requires a new state") +) + +func (s AlertStateType) IsValid() bool { + return s == AlertStateOK || + s == AlertStateNoData || + s == AlertStatePaused || + s == AlertStatePending || + s == AlertStateAlerting || + s == AlertStateUnknown +} + +func (s NoDataOption) IsValid() bool { + return s == NoDataSetNoData || s == NoDataSetAlerting || s == NoDataKeepState || s == NoDataSetOK +} + +func (s NoDataOption) ToAlertState() AlertStateType { + return AlertStateType(s) +} + +func (s ExecutionErrorOption) IsValid() bool { + return s == ExecutionErrorSetAlerting || s == ExecutionErrorKeepState +} + +func (s ExecutionErrorOption) ToAlertState() AlertStateType { + return AlertStateType(s) +} + +// AlertSettings contains alert conditions +type AlertSetting struct { + Conditions []AlertCondition `json:"conditions"` + Notifications []string `json:"notifications"` + Level string `json:"level"` +} + +type AlertCondition struct { + Type string `json:"type"` + Query AlertQuery `json:"query"` + Reducer Condition `json:"reducer"` + Evaluator Condition `json:"evaluator"` + Operator string `json:"operator"` +} + +type AlertQuery struct { + Model MetricQuery `json:"model"` + DataSourceId string `json:"data_source_id"` + From string `json:"from"` + To string `json:"to"` +} + +type AlertCreateInput struct { + apis.Meta + + Name string `json:"name"` + Frequency int64 `json:"frequency"` + Settings AlertSetting `json:"settings"` + Enabled *bool `json:"enabled"` +} + +type AlertUpdateInput struct { + apis.Meta + + Name *string `json:"name"` + Frequency *int64 `json:"frequency"` + Settings *AlertSetting `json:"settings"` + ResourceId *string `json:"resource_id"` + ResourceType *string `json:"resource_type"` + Message *string `json:"message"` + Enabled *bool `json:"enabled"` +} + +type AlertListInput struct { + apis.VirtualResourceListInput + + // 监控指标名称 + Metric string `json:"metric"` + // 以报警是否启用/禁用过滤列表 + Enabled *bool `json:"enabled"` +} diff --git a/pkg/apis/monitor/doc.go b/pkg/apis/monitor/doc.go new file mode 100644 index 0000000000..b7f781ca03 --- /dev/null +++ b/pkg/apis/monitor/doc.go @@ -0,0 +1 @@ +package monitor // import "yunion.io/x/onecloud/pkg/apis/monitor" diff --git a/pkg/apis/monitor/meteralert.go b/pkg/apis/monitor/meteralert.go new file mode 100644 index 0000000000..86f5df03e5 --- /dev/null +++ b/pkg/apis/monitor/meteralert.go @@ -0,0 +1,60 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "yunion.io/x/onecloud/pkg/apis" +) + +const ( + MeterAlertTypeBalance = "balance" + MeterAlertTypeDailyResFee = "resFee" + MeterAlertTypeMonthResFee = "monthFee" +) + +type MeterAlertCreateInput struct { + ResourceAlertV1CreateInput + + // 监控资源类型, 比如: balance, resFree, monthFee + Type string `json:"type"` + // 云平台类型 + Provider string `json:"provider"` + // 云账号 Id + AccountId string `json:"account_id"` + // 项目 Id string + ProjectId string `json:"project_id"` +} + +type MeterAlertListInput struct { + apis.VirtualResourceListInput + + // 监控资源类型, 比如: balance, resFree, monthFee + Type string `json:"type"` + // 云平台类型 + Provider string `json:"provider"` + // 云账号 Id + AccountId string `json:"account_id"` + // 项目 Id string + ProjectId string `json:"project_id"` +} + +type MeterAlertDetails struct { + AlertV1Details + + Type string `json:"type"` + ProjectId string `json:"project_id"` + AccountId string `json:"account_id"` + Provider string `json:"provider"` +} diff --git a/pkg/apis/monitor/nodealert.go b/pkg/apis/monitor/nodealert.go new file mode 100644 index 0000000000..a3ab04c333 --- /dev/null +++ b/pkg/apis/monitor/nodealert.go @@ -0,0 +1,213 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "time" + + "yunion.io/x/onecloud/pkg/apis" +) + +const ( + NodeAlertTypeGuest = "guest" + NodeAlertTypeHost = "host" +) + +type ResourceAlertV1CreateInput struct { + *AlertCreateInput + + // 查询指标周期 + Period string `json:"period"` + // 每隔多久查询一次 + Window string `json:"window"` + // 比较运算符, 比如: >, <, >=, <= + Comparator string `json:"comparator"` + // 报警阀值 + Threshold float64 `json:"threshold"` + // 报警级别 + Level string `json:"level"` + // 通知方式, 比如: email, mobile + Channel string `json:"channel"` + // 通知接受者 + Recipients string `json:"recipients"` +} + +type NodeAlertCreateInput struct { + ResourceAlertV1CreateInput + + // 监控指标名称 + Metric string `json:"metric"` + // 监控资源类型, 比如: guest, host + Type string `json:"type"` + // 监控资源名称 + NodeName string `json:"node_name"` + // 监控资源 Id + NodeId string `json:"node_id"` +} + +func (input NodeAlertCreateInput) ToAlertCreateInput( + name string, + field string, + measurement string, + db string, + notifications []string) AlertCreateInput { + freq, _ := time.ParseDuration(input.Window) + ret := AlertCreateInput{ + Name: name, + Frequency: int64(freq / time.Second), + Settings: AlertSetting{ + Level: input.Level, + Notifications: notifications, + Conditions: []AlertCondition{ + { + Type: "query", + Operator: "and", + Query: AlertQuery{ + Model: input.GetQuery(field, measurement, db), + From: input.Period, + To: "now", + }, + Evaluator: input.GetEvaluator(), + Reducer: Condition{ + Type: "avg", + }, + }, + }, + }, + } + return ret +} + +func (input NodeAlertCreateInput) GetQuery(field, measurement, db string) MetricQuery { + return GetNodeAlertQuery(input.Type, field, measurement, db, input.NodeId) +} + +func GetNodeAlertQuery(typ, field, measurement, db, nodeId string) MetricQuery { + var idField string + switch typ { + case NodeAlertTypeGuest: + idField = "vm_id" + case NodeAlertTypeHost: + idField = "host_id" + } + sels := make([]MetricQuerySelect, 0) + sels = append(sels, NewMetricQuerySelect(MetricQueryPart{Type: "field", Params: []string{field}})) + return MetricQuery{ + Selects: sels, + Tags: []MetricQueryTag{ + { + Key: idField, + Value: nodeId, + }, + }, + GroupBy: []MetricQueryPart{ + { + Type: "field", + Params: []string{"*"}, + }, + }, + Measurement: measurement, + Database: db, + } +} + +func (input NodeAlertCreateInput) GetEvaluator() Condition { + return GetNodeAlertEvaluator(input.Comparator, input.Threshold) +} + +func GetNodeAlertEvaluator(comparator string, threshold float64) Condition { + typ := "gt" + switch comparator { + case ">=", ">": + typ = "gt" + case "<=", "<": + typ = "lt" + } + return Condition{ + Type: typ, + Params: []float64{threshold}, + } +} + +type NodeAlertListInput struct { + apis.VirtualResourceListInput + + // 监控指标名称 + Metric string `json:"metric"` + // 监控资源类型, 比如: guest, host + Type string `json:"type"` + // 监控资源名称 + NodeName string `json:"node_name"` + // 监控资源 Id + NodeId string `json:"node_id"` +} + +func (input NodeAlertListInput) ToAlertListInput() AlertListInput { + return AlertListInput{ + VirtualResourceListInput: input.VirtualResourceListInput, + Metric: input.Metric, + } +} + +type AlertV1Details struct { + apis.VirtualResourceDetails + + Name string `json:"name"` + Period string `json:"period"` + Window string `json:"window"` + Comparator string `json:"comparator"` + Threshold float64 `json:"threshold"` + Recipients string `json:"recipients"` + Level string `json:"level"` + Channel string `json:"channel"` + DB string `json:"db"` + Measurement string `json:"measurement"` + Field string `json:"field"` + NotifierId string `json:"notifier_id"` +} + +type NodeAlertDetails struct { + AlertV1Details + + Type string `json:"type"` + Metric string `json:"metric"` + NodeId string `json:"node_id"` + NodeName string `json:"node_name"` +} + +type NodeAlertUpdateInput struct { + // 监控指标名称 + Metric *string `json:"metric"` + // 监控资源类型, 比如: guest, host + Type *string `json:"type"` + // 监控资源名称 + NodeName *string `json:"node_name"` + // 监控资源 Id + NodeId *string `json:"node_id"` + // 查询指标周期 + Period *string `json:"period"` + // 每隔多久查询一次 + Window *string `json:"window"` + // 比较运算符, 比如: >, <, >=, <= + Comparator *string `json:"comparator"` + // 报警阀值 + Threshold *float64 `json:"threshold"` + // 报警级别 + Level *string `json:"level"` + // 通知方式, 比如: email, mobile + Channel *string `json:"channel"` + // 通知接受者 + Recipients *string `json:"recipients"` +} diff --git a/pkg/apis/monitor/notification.go b/pkg/apis/monitor/notification.go new file mode 100644 index 0000000000..c66298a134 --- /dev/null +++ b/pkg/apis/monitor/notification.go @@ -0,0 +1,106 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "time" + + "yunion.io/x/jsonutils" + + "yunion.io/x/onecloud/pkg/apis" +) + +type AlertNotificationStateType string + +var ( + AlertNotificationStatePending = AlertNotificationStateType("pending") + AlertNotificationStateCompleted = AlertNotificationStateType("completed") + AlertNotificationStateUnknown = AlertNotificationStateType("unknown") +) + +const ( + AlertNotificationTypeOneCloud = "onecloud" + AlertNotificationTypeDingding = "dingding" + AlertNotificationTypeFeishu = "feishu" +) + +type AlertNotificationCreateInput struct { + apis.Meta + + // 报警通知名称 + Name string `json:"name"` + // 类型 + Type string `json:"type"` + // 是否为默认通知配置 + IsDefault bool `json:"is_default"` + // 是否一直提醒 + SendReminder *bool `json:"send_reminder"` + // 是否禁用报警恢复提醒 + DisableResolveMessage *bool `json:"disable_resolve_message"` + // 发送频率 + Frequency time.Duration `json:"frequency"` + // 通知配置 + Settings jsonutils.JSONObject `json:"settings"` +} + +type AlertNotificationUpdateInput struct { + apis.Meta + + // 报警通知名称 + Name string `json:"name"` + // 是否为默认通知配置 + IsDefault *bool `json:"is_default"` + // 是否一直提醒 + SendReminder *bool `json:"send_reminder"` + // 是否禁用报警恢复提醒 + DisableResolveMessage *bool `json:"disable_resolve_message"` + // 发送频率 + Frequency *time.Duration `json:"frequency"` +} + +type NotificationSettingOneCloud struct { + Channel string `json:"channel"` + UserIds []string `json:"user_ids"` +} + +type SendWebhookSync struct { + Url string + User string + Password string + Body string + HttpMethod string + HttpHeader map[string]string + ContentType string +} + +type NotificationSettingDingding struct { + Url string `json:"url"` + MessageType string `json:"message_type"` +} + +type NotificationSettingFeishu struct { + // Url string `json:"url"` + AppId string `json:"app_id"` + AppSecret string `json:"app_secret"` +} + +type AlertNotificationStateCreateInput struct { + apis.Meta + + Name string `json:"name"` + AlertId string `json:"alert_id"` + NotifierId string `json:"notifier_id"` + State AlertNotificationStateType `json:"state"` +} diff --git a/pkg/apis/monitor/template.go b/pkg/apis/monitor/template.go new file mode 100644 index 0000000000..86ced38626 --- /dev/null +++ b/pkg/apis/monitor/template.go @@ -0,0 +1,41 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +type NotificationTemplateCreateInput struct { + Content string `json:"content"` +} + +type NotificationTemplateConfig struct { + Title string `json:"title"` + Name string `json:"name"` + Matches []EvalMatch `json:"matches"` + // PrevAlertState AlertStateType `json:"prev_alert_state"` + // State AlertStateType `json:"state"` + StartTime string `json:"start_time"` + EndTime string `json:"end_time"` + Description string `json:"description"` + Priority string `json:"priority"` + Level string `json:"level"` + IsRecovery bool `json:"is_recovery"` +} + +// EvalMatch represents the series violating the threshold. +type EvalMatch struct { + Condition string `json:"condition"` + Value *float64 `json:"value"` + Metric string `json:"metric"` + Tags map[string]string `json:"tags"` +} diff --git a/pkg/apis/monitor/types.go b/pkg/apis/monitor/types.go new file mode 100644 index 0000000000..0ad5f4e438 --- /dev/null +++ b/pkg/apis/monitor/types.go @@ -0,0 +1,103 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +const ( + DataSourceTypeInfluxdb = "influxdb" +) + +type DataSourceConfig struct { + Id string + Name string + Driver string + Config interface{} +} + +type MetricResource struct { + // Type is the metric resource type. e.g: host, vm, lbinstance + Type string `json:"type"` + // ConfigId is the data source config id + ConfigId string `json:"config_id"` +} + +type Metric struct { + Resource MetricResource `json:"resource"` + Measurement string `json:"measurement"` + Field string `json:"field"` + DisplayName string `json:"displayname"` +} + +type TimeSeries struct { + Results []TimeSeriesResult `json:"results"` +} + +type TimeSeriesResult struct { + Series []TimeSeriesRow `json:"series"` +} + +type TimeSeriesRow struct { + Metric Metric `json:"metric"` + Tags map[string]string `json:"tags,omitempty"` + Columns []string `json:"columns,omitempty"` + // Value item is a point with timestamp and value + Values [][]interface{} `json:"values,omitempty"` +} + +type MetricRequest struct { + // The start time for the query + From string `json:"from"` + // An end time for the query + To string `json:"to"` + Queries []*MetricQuery `json:"queries"` + Debug bool `json:"debug"` +} + +type MetricQueryTag struct { + Key string `json:"key"` + Operator string `json:"operator"` + Value string `json:"value"` + Condition string `json:"condition"` +} + +type MetricQueryPart struct { + Type string `json:"type"` + Params []string `json:"params"` +} + +type MetricQuerySelect []MetricQueryPart + +func NewMetricQuerySelect(parts ...MetricQueryPart) MetricQuerySelect { + return parts +} + +type MetricQuery struct { + Alias string `json:"alias"` + Tz string `json:"tz"` + Database string `json:"database"` + Measurement string `json:"measurement"` + Tags []MetricQueryTag `json:"tags"` + GroupBy []MetricQueryPart `json:"group_by"` + Selects []MetricQuerySelect `json:"select"` + Interval string `json:"interval"` + Policy string `json:"policy"` + ResultFormat string `json:"result_format"` +} + +type AlertConditionCombiner string + +type Condition struct { + Type string `json:"type"` + Params []float64 `json:"params"` +} diff --git a/pkg/cloudcommon/db/enabledstatusstandalone.go b/pkg/cloudcommon/db/enabledstatusstandalone.go index ee4ddf520a..5fe50a6804 100644 --- a/pkg/cloudcommon/db/enabledstatusstandalone.go +++ b/pkg/cloudcommon/db/enabledstatusstandalone.go @@ -42,42 +42,83 @@ func NewEnabledStatusStandaloneResourceBaseManager(dt interface{}, tableName str return SEnabledStatusStandaloneResourceBaseManager{SStatusStandaloneResourceBaseManager: NewStatusStandaloneResourceBaseManager(dt, tableName, keyword, keywordPlural)} } +type IEnableModel interface { + IModel + IsEnable() bool + SetEnable() error + SetDisable() error +} + +func (self *SEnabledStatusStandaloneResourceBase) IsEnable() bool { + return self.Enabled +} + +func (self *SEnabledStatusStandaloneResourceBase) SetEnable() error { + self.Enabled = true + return nil +} + +func (self *SEnabledStatusStandaloneResourceBase) SetDisable() error { + self.Enabled = false + return nil +} + func (self *SEnabledStatusStandaloneResourceBase) AllowPerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool { - return IsAllowPerform(rbacutils.ScopeSystem, userCred, self, "enable") + return AllowPerformEnable(self, rbacutils.ScopeSystem, userCred) +} + +func AllowPerformEnable(obj IEnableModel, scope rbacutils.TRbacScope, userCred mcclient.TokenCredential) bool { + return IsAllowPerform(scope, userCred, obj, "enable") } func (self *SEnabledStatusStandaloneResourceBase) PerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { - if !self.Enabled { - _, err := Update(self, func() error { - self.Enabled = true + return PerformEnable(self, userCred) +} + +func PerformEnable(obj IEnableModel, userCred mcclient.TokenCredential) (jsonutils.JSONObject, error) { + if !obj.IsEnable() { + _, err := Update(obj, func() error { + if err := obj.SetEnable(); err != nil { + return err + } return nil }) if err != nil { log.Errorf("PerformEnable save update fail %s", err) return nil, err } - OpsLog.LogEvent(self, ACT_ENABLE, "", userCred) - logclient.AddSimpleActionLog(self, logclient.ACT_ENABLE, nil, userCred, true) + OpsLog.LogEvent(obj, ACT_ENABLE, "", userCred) + logclient.AddSimpleActionLog(obj, logclient.ACT_ENABLE, nil, userCred, true) } return nil, nil } func (self *SEnabledStatusStandaloneResourceBase) AllowPerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool { - return IsAllowPerform(rbacutils.ScopeSystem, userCred, self, "disable") + return AllowPerformDisable(self, rbacutils.ScopeSystem, userCred) +} + +func AllowPerformDisable(obj IEnableModel, scope rbacutils.TRbacScope, userCred mcclient.TokenCredential) bool { + return IsAllowPerform(scope, userCred, obj, "disable") } func (self *SEnabledStatusStandaloneResourceBase) PerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { - if self.Enabled { - _, err := Update(self, func() error { - self.Enabled = false + return PerformDisable(self, userCred) +} + +func PerformDisable(obj IEnableModel, userCred mcclient.TokenCredential) (jsonutils.JSONObject, error) { + if obj.IsEnable() { + _, err := Update(obj, func() error { + if err := obj.SetDisable(); err != nil { + return err + } return nil }) if err != nil { log.Errorf("PerformDisable save update fail %s", err) return nil, err } - OpsLog.LogEvent(self, ACT_DISABLE, "", userCred) - logclient.AddSimpleActionLog(self, logclient.ACT_DISABLE, nil, userCred, true) + OpsLog.LogEvent(obj, ACT_DISABLE, "", userCred) + logclient.AddSimpleActionLog(obj, logclient.ACT_DISABLE, nil, userCred, true) } return nil, nil } @@ -96,8 +137,12 @@ func (manager *SEnabledStatusStandaloneResourceBaseManager) ListItemFilter(ctx c if err != nil { return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.ListItemFilter") } - if query.Enabled != nil { - if *query.Enabled { + return ListEnableItemFilter(q, query.Enabled) +} + +func ListEnableItemFilter(q *sqlchemy.SQuery, enabled *bool) (*sqlchemy.SQuery, error) { + if enabled != nil { + if *enabled { q = q.IsTrue("enabled") } else { q = q.IsFalse("enabled") diff --git a/pkg/keystone/models/users.go b/pkg/keystone/models/users.go index fc6aabd308..0641486b70 100644 --- a/pkg/keystone/models/users.go +++ b/pkg/keystone/models/users.go @@ -309,7 +309,7 @@ func localUserVerifyPassword(user *api.SUserExtended, passwd string) error { if err == nil { return nil } - return errors.Error("invalid password") + return errors.Error(fmt.Sprintf("invalid password: %v", err)) } // 用户列表 diff --git a/pkg/mcclient/modules/managers.go b/pkg/mcclient/modules/managers.go index 410601a93d..03830d6a62 100644 --- a/pkg/mcclient/modules/managers.go +++ b/pkg/mcclient/modules/managers.go @@ -48,6 +48,12 @@ func NewMonitorManager(keyword, keywordPlural string, columns, adminColumns []st Keyword: keyword, KeywordPlural: keywordPlural} } +func NewMonitorV2Manager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { + return modulebase.ResourceManager{ + BaseManager: *modulebase.NewBaseManager("monitor", "", "", columns, adminColumns), + Keyword: keyword, KeywordPlural: keywordPlural} +} + func NewCloudwatcherManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ BaseManager: *modulebase.NewBaseManager("cloudwatcher", "", "v1", columns, adminColumns), @@ -115,12 +121,6 @@ func NewMeterManager(keyword, keywordPlural string, columns, adminColumns []stri Keyword: keyword, KeywordPlural: keywordPlural} } -func NewMeterAlertManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { - return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager("meteralert", "", "", columns, adminColumns), - Keyword: keyword, KeywordPlural: keywordPlural} -} - func NewYunionAgentManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ BaseManager: *modulebase.NewBaseManager("yunionagent", "", "", columns, adminColumns), diff --git a/pkg/mcclient/modules/mod_meteralert.go b/pkg/mcclient/modules/mod_meteralert.go index 5b95dbb656..df6a90828e 100644 --- a/pkg/mcclient/modules/mod_meteralert.go +++ b/pkg/mcclient/modules/mod_meteralert.go @@ -21,8 +21,8 @@ var ( ) func init() { - MeterAlert = NewMeterAlertManager("meteralert", "meteralerts", - []string{"id", "type", "provider", "account", "account_id", "comparator", "threshold", "recipients", "level", "channel", "status", "create_by", "update_by", "delete_by", "gmt_create", "gmt_modified", "gmt_delete", "is_deleted", "project_id", "remark"}, + MeterAlert = NewMonitorV2Manager("meteralert", "meteralerts", + []string{"id", "type", "provider", "account", "account_id", "comparator", "threshold", "recipients", "level", "channel", "state", "project_id"}, []string{}) register(&MeterAlert) diff --git a/pkg/mcclient/modules/mod_nodealert.go b/pkg/mcclient/modules/mod_nodealert.go index e8283fec70..d1bf5c8429 100644 --- a/pkg/mcclient/modules/mod_nodealert.go +++ b/pkg/mcclient/modules/mod_nodealert.go @@ -21,8 +21,8 @@ var ( ) func init() { - NodeAlert = NewMeterAlertManager("nodealert", "nodealerts", - []string{"id", "type", "metric", "node_name", "node_id", "period", "window", "comparator", "threshold", "recipients", "level", "channel", "status", "create_by", "update_by", "delete_by", "gmt_create", "gmt_modified", "gmt_delete", "is_deleted", "project_id", "remark"}, + NodeAlert = NewMonitorV2Manager("nodealert", "nodealerts", + []string{"id", "type", "metric", "node_name", "node_id", "period", "window", "comparator", "threshold", "recipients", "level", "channel", "state", "project_id"}, []string{}) register(&NodeAlert) diff --git a/pkg/mcclient/modules/monitor/alert.go b/pkg/mcclient/modules/monitor/alert.go new file mode 100644 index 0000000000..9347998aa6 --- /dev/null +++ b/pkg/mcclient/modules/monitor/alert.go @@ -0,0 +1,41 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "yunion.io/x/onecloud/pkg/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" +) + +var ( + Alerts modulebase.ResourceManager + AlertNotifications modulebase.ResourceManager +) + +func init() { + Alerts = modules.NewMonitorV2Manager("alert", "alerts", + []string{"id", "name", "settings"}, + []string{}) + AlertNotifications = modules.NewMonitorV2Manager( + "alert_notification", "alert_notifications", + []string{"id", "name", "type", "is_default", "disable_resolve_message", "send_reminder", "settings"}, + []string{}) + for _, m := range []modulebase.ResourceManager{ + Alerts, + AlertNotifications, + } { + modules.Register(&m) + } +} diff --git a/pkg/mcclient/modules/monitor/datasource.go b/pkg/mcclient/modules/monitor/datasource.go new file mode 100644 index 0000000000..270a1ce10e --- /dev/null +++ b/pkg/mcclient/modules/monitor/datasource.go @@ -0,0 +1,31 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "yunion.io/x/onecloud/pkg/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" +) + +var ( + DataSources modulebase.ResourceManager +) + +func init() { + DataSources = modules.NewMonitorV2Manager("datasource", "datasources", + []string{"Id", "Name", "Type", "Url"}, + []string{}) + modules.Register(&DataSources) +} diff --git a/pkg/mcclient/modules/monitor/doc.go b/pkg/mcclient/modules/monitor/doc.go new file mode 100644 index 0000000000..b06f5a99d1 --- /dev/null +++ b/pkg/mcclient/modules/monitor/doc.go @@ -0,0 +1 @@ +package monitor // import "yunion.io/x/onecloud/pkg/mcclient/modules/monitor" diff --git a/pkg/mcclient/options/monitor/doc.go b/pkg/mcclient/options/monitor/doc.go new file mode 100644 index 0000000000..fc8c4a12e0 --- /dev/null +++ b/pkg/mcclient/options/monitor/doc.go @@ -0,0 +1 @@ +package monitor // import "yunion.io/x/onecloud/pkg/mcclient/options/monitor" diff --git a/pkg/mcclient/options/monitor/ds.go b/pkg/mcclient/options/monitor/ds.go new file mode 100644 index 0000000000..84446a4a6e --- /dev/null +++ b/pkg/mcclient/options/monitor/ds.go @@ -0,0 +1,146 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import ( + "yunion.io/x/jsonutils" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +type DataSourceCreateOptions struct { + NAME string +} + +type DataSourceListOptions struct { + options.BaseListOptions +} + +type DataSourceDeleteOptions struct { + ID string `json:"-"` +} + +type NotificationListOptions struct { + options.BaseListOptions +} + +type NotificationShowOptions struct { + ID string `help:"ID or name of the alert notification config" json:"-"` +} + +type NotificationFields struct { + Frequency string `help:"notify frequency, e.g. 5m, 1h"` + IsDefault *bool `help:"set as default notification"` + DisableResolveMessage *bool `help:"disable notify recover message"` + SendReminder *bool `help:"send reminder"` +} + +type NotificationCreateOptions struct { + NAME string `help:"notification config name"` + NotificationFields +} + +func (opt NotificationCreateOptions) Params() (*monitor.AlertNotificationCreateInput, error) { + ret := &monitor.AlertNotificationCreateInput{ + Name: opt.NAME, + SendReminder: opt.SendReminder, + DisableResolveMessage: opt.DisableResolveMessage, + } + if opt.IsDefault != nil && *opt.IsDefault { + ret.IsDefault = true + } + return ret, nil +} + +type NotificationDingDingCreateOptions struct { + NotificationCreateOptions + URL string `help:"dingding webhook url"` + MsgType string `help:"message type" choices:"markdown|actionCard" default:"markdown"` +} + +func (opt NotificationDingDingCreateOptions) Params() (*monitor.AlertNotificationCreateInput, error) { + out, err := opt.NotificationCreateOptions.Params() + if err != nil { + return nil, err + } + out.Type = monitor.AlertNotificationTypeDingding + out.Settings = jsonutils.Marshal(monitor.NotificationSettingDingding{ + Url: opt.URL, + MessageType: opt.MsgType, + }) + return out, nil +} + +type NotificationFeishuCreateOptions struct { + NotificationCreateOptions + APPID string `help:"feishu robot appId"` + APPSECRET string `help:"feishu robt appSecret"` +} + +func (opt NotificationFeishuCreateOptions) Params() (*monitor.AlertNotificationCreateInput, error) { + out, err := opt.NotificationCreateOptions.Params() + if err != nil { + return nil, err + } + out.Type = monitor.AlertNotificationTypeFeishu + out.Settings = jsonutils.Marshal(monitor.NotificationSettingFeishu{ + AppId: opt.APPID, + AppSecret: opt.APPSECRET, + }) + return out, nil +} + +type NotificationUpdateOptions struct { + NotificationFields + + ID string `help:"ID or name of the alert notification config" json:"-"` + DisableDefault *bool `help:"disable as default notification" json:"-"` + ResolveMessage *bool `help:"enable notify recover message" json:"-"` + DisableSendReminder *bool `help:"disable send reminder" json:"-"` +} + +func (opt NotificationUpdateOptions) Params() (*monitor.AlertNotificationUpdateInput, error) { + if opt.DisableDefault != nil && *opt.DisableDefault { + tmp := false + opt.IsDefault = &tmp + } + if opt.ResolveMessage != nil && *opt.ResolveMessage { + tmp := false + opt.DisableDefault = &tmp + } + if opt.DisableSendReminder != nil && *opt.DisableSendReminder { + tmp := false + opt.SendReminder = &tmp + } + ret := &monitor.AlertNotificationUpdateInput{ + IsDefault: opt.IsDefault, + DisableResolveMessage: opt.DisableResolveMessage, + SendReminder: opt.SendReminder, + } + return ret, nil +} + +type AlertListOptions struct { + options.BaseListOptions +} + +type AlertShowOptions struct { + ID string `help:"ID or name of the alert" json:"-"` +} + +type AlertDeleteOptions struct { + ID []string `help:"ID of alert to delete"` +} diff --git a/pkg/monitor/alerting/conditions/doc.go b/pkg/monitor/alerting/conditions/doc.go new file mode 100644 index 0000000000..82544f5e02 --- /dev/null +++ b/pkg/monitor/alerting/conditions/doc.go @@ -0,0 +1 @@ +package conditions // import "yunion.io/x/onecloud/pkg/monitor/alerting/conditions" diff --git a/pkg/monitor/alerting/conditions/evaluator.go b/pkg/monitor/alerting/conditions/evaluator.go new file mode 100644 index 0000000000..31dee0f6f0 --- /dev/null +++ b/pkg/monitor/alerting/conditions/evaluator.go @@ -0,0 +1,145 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package conditions + +import ( + "fmt" + + "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/utils" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/monitor/validators" +) + +// AlertEvaluator evaluates the reduced value of a timeserie. +// Returning true if a timeseries is violating the condition +// ex: ThresholdEvaluator, NoValueEvaluator, RangeEvaluator +type AlertEvaluator interface { + Eval(reducedValue *float64) bool + String() string +} + +type noValueEvaluator struct{} + +func (e *noValueEvaluator) Eval(reducedValue *float64) bool { + return reducedValue == nil +} + +func (e *noValueEvaluator) String() string { + return "no_data" +} + +type thresholdEvaluator struct { + Type string + Threshold float64 +} + +func newThresholdEvaluator(cond *monitor.Condition) (*thresholdEvaluator, error) { + defaultEval := &thresholdEvaluator{ + Type: cond.Type, + Threshold: cond.Params[0], + } + return defaultEval, nil +} + +func (e *thresholdEvaluator) Eval(reducedValue *float64) bool { + if reducedValue == nil { + return false + } + + val := *reducedValue + switch e.Type { + case "gt": + return val > e.Threshold + case "lt": + return val < e.Threshold + } + + return false +} + +func (e *thresholdEvaluator) String() string { + var op string + switch e.Type { + case "gt": + op = ">" + case "lt": + op = "<" + } + return fmt.Sprintf("%s %.2f", op, e.Threshold) +} + +type rangedEvaluator struct { + Type string + Lower float64 + Upper float64 +} + +func newRangedEvaluator(cond *monitor.Condition) (*rangedEvaluator, error) { + if len(cond.Params) == 0 { + return nil, errors.Wrap(validators.ErrMissingParameterThreshold, "RangedEvaluator parameter is empty") + } + if len(cond.Params) == 1 { + return nil, errors.Wrap(validators.ErrMissingParameterThreshold, "RangedEvaluator parameter second parameter is missing") + } + + rangedEval := &rangedEvaluator{ + Type: cond.Type, + Lower: cond.Params[0], + Upper: cond.Params[1], + } + return rangedEval, nil +} + +func (e *rangedEvaluator) Eval(reducedValue *float64) bool { + if reducedValue == nil { + return false + } + val := *reducedValue + switch e.Type { + case "within_range": + return (e.Lower < val && e.Upper > val) || (e.Upper < val && e.Lower > val) + case "outside_range": + return (e.Upper < val && e.Lower < val) || (e.Upper > val && e.Lower > val) + } + return false +} + +func (e *rangedEvaluator) String() string { + return fmt.Sprintf("%s [%.2f, %.2f]", e.Type, e.Lower, e.Upper) +} + +// NewAlertEvaluator is a factory function for returning +// an `AlertEvaluator` depending on the input condition. +func NewAlertEvaluator(cond *monitor.Condition) (AlertEvaluator, error) { + typ := cond.Type + if typ == "" { + return nil, validators.ErrMissingParameterType + } + + if utils.IsInStringArray(typ, validators.EvaluatorDefaultTypes) { + return newThresholdEvaluator(cond) + } + if utils.IsInStringArray(typ, validators.EvaluatorRangedTypes) { + return newRangedEvaluator(cond) + } + + if typ == "no_value" { + return &noValueEvaluator{}, nil + } + + return nil, errors.Wrapf(validators.ErrInvalidEvaluatorType, "type: %s", typ) +} diff --git a/pkg/monitor/alerting/conditions/evaluator_test.go b/pkg/monitor/alerting/conditions/evaluator_test.go new file mode 100644 index 0000000000..fbc795b0fe --- /dev/null +++ b/pkg/monitor/alerting/conditions/evaluator_test.go @@ -0,0 +1,68 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package conditions + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" + + "yunion.io/x/onecloud/pkg/apis/monitor" +) + +func evalutorScenario(typ string, params []float64, reducedValue float64) bool { + evaluator, err := NewAlertEvaluator(&monitor.Condition{Type: typ, Params: params}) + So(err, ShouldBeNil) + + return evaluator.Eval(&reducedValue) +} + +func TestEvalutors(t *testing.T) { + Convey("greater than", t, func() { + So(evalutorScenario("gt", []float64{1}, 3), ShouldBeTrue) + So(evalutorScenario("gt", []float64{3}, 1), ShouldBeFalse) + }) + + Convey("less than", t, func() { + So(evalutorScenario("lt", []float64{1}, 3), ShouldBeFalse) + So(evalutorScenario("lt", []float64{3}, 1), ShouldBeTrue) + }) + + Convey("within_range", t, func() { + So(evalutorScenario("within_range", []float64{1, 100}, 3), ShouldBeTrue) + So(evalutorScenario("within_range", []float64{1, 100}, 300), ShouldBeFalse) + So(evalutorScenario("within_range", []float64{100, 1}, 3), ShouldBeTrue) + So(evalutorScenario("within_range", []float64{100, 1}, 300), ShouldBeFalse) + }) + + Convey("outside_range", t, func() { + So(evalutorScenario("outside_range", []float64{1, 100}, 1000), ShouldBeTrue) + So(evalutorScenario("outside_range", []float64{1, 100}, 50), ShouldBeFalse) + So(evalutorScenario("outside_range", []float64{100, 1}, 1000), ShouldBeTrue) + So(evalutorScenario("outside_range", []float64{100, 1}, 50), ShouldBeFalse) + }) + + Convey("no_value", t, func() { + Convey("should be false if series have values", func() { + So(evalutorScenario("no_value", nil, 50), ShouldBeFalse) + }) + + Convey("should be true when the series have no value", func() { + evaluator, err := NewAlertEvaluator(&monitor.Condition{Type: "no_value"}) + So(err, ShouldBeNil) + So(evaluator.Eval(nil), ShouldBeTrue) + }) + }) +} diff --git a/pkg/monitor/alerting/conditions/query.go b/pkg/monitor/alerting/conditions/query.go new file mode 100644 index 0000000000..db191c2307 --- /dev/null +++ b/pkg/monitor/alerting/conditions/query.go @@ -0,0 +1,268 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package conditions + +import ( + gocontext "context" + "fmt" + "strings" + + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/monitor/alerting" + "yunion.io/x/onecloud/pkg/monitor/models" + "yunion.io/x/onecloud/pkg/monitor/tsdb" + "yunion.io/x/onecloud/pkg/monitor/validators" +) + +func init() { + alerting.RegisterCondition("query", func(model *monitor.AlertCondition, index int) (alerting.Condition, error) { + return newQueryCondition(model, index) + }) +} + +// QueryCondition is responsible for issue and query. reduce the +// timeseries into single values and evaluate if they are firing or not. +type QueryCondition struct { + Index int + Query AlertQuery + Reducer *queryReducer + Evaluator AlertEvaluator + Operator string + HandleRequest tsdb.HandleRequestFunc +} + +// AlertQuery contains information about what datasource a query +// should be send to and the query object. +type AlertQuery struct { + Model monitor.MetricQuery + DataSourceId string + From string + To string +} + +type FormatCond struct { + QueryMeta *tsdb.QueryResultMeta + Reducer string + Evaluator AlertEvaluator +} + +func (c *QueryCondition) GenerateFormatCond(meta *tsdb.QueryResultMeta) *FormatCond { + return &FormatCond{ + QueryMeta: meta, + Reducer: c.Reducer.Type, + Evaluator: c.Evaluator, + } +} +func (c FormatCond) String() string { + if c.QueryMeta != nil { + return fmt.Sprintf("%s(%q) %s", c.Reducer, c.QueryMeta.RawQuery, c.Evaluator.String()) + } + return "no_data" +} + +func (c *QueryCondition) filterTags(tags map[string]string) map[string]string { + ret := make(map[string]string) + for key, val := range tags { + if strings.HasSuffix(key, "_id") { + continue + } + ret[key] = val + } + return ret +} + +// Eval evaluates te `QueryCondition`. +func (c *QueryCondition) Eval(context *alerting.EvalContext) (*alerting.ConditionResult, error) { + timeRange := tsdb.NewTimeRange(c.Query.From, c.Query.To) + + ret, err := c.executeQuery(context, timeRange) + if err != nil { + return nil, err + } + seriesList := ret.series + metas := ret.metas + + emptySeriesCount := 0 + evalMatchCount := 0 + var matches []*alerting.EvalMatch + + for idx, series := range seriesList { + reducedValue := c.Reducer.Reduce(series) + evalMatch := c.Evaluator.Eval(reducedValue) + + if reducedValue == nil { + emptySeriesCount++ + } + + if context.IsTestRun { + context.Logs = append(context.Logs, &alerting.ResultLogEntry{ + Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %v", c.Index, evalMatch, series.Name, reducedValue), + }) + } + + if evalMatch { + evalMatchCount++ + } + tags := c.filterTags(series.Tags) + matches = append(matches, &alerting.EvalMatch{ + Condition: c.GenerateFormatCond(&metas[idx]).String(), + Metric: series.Name, + Value: reducedValue, + Tags: tags, + }) + } + + // handle no series special case + if len(seriesList) == 0 { + // eval condition for null value + evalMatch := c.Evaluator.Eval(nil) + + if context.IsTestRun { + context.Logs = append(context.Logs, &alerting.ResultLogEntry{ + Message: fmt.Sprintf("Condition: Eval: %v, Query returned No Series (reduced to null/no value)", evalMatch), + }) + } + + if evalMatch { + evalMatchCount++ + matches = append(matches, &alerting.EvalMatch{ + Metric: "NoData", + Value: nil, + }) + } + } + + return &alerting.ConditionResult{ + Firing: evalMatchCount > 0, + NoDataFound: emptySeriesCount == len(seriesList), + Operator: c.Operator, + EvalMatches: matches, + }, nil +} + +type queryResult struct { + series tsdb.TimeSeriesSlice + metas []tsdb.QueryResultMeta +} + +func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange *tsdb.TimeRange) (*queryResult, error) { + ds, err := models.DataSourceManager.GetSource(c.Query.DataSourceId) + if err != nil { + return nil, errors.Wrapf(err, "Cound not find datasource %v", c.Query.DataSourceId) + } + + req := c.getRequestForAlertRule(ds, timeRange, context.IsDebug) + result := make(tsdb.TimeSeriesSlice, 0) + metas := make([]tsdb.QueryResultMeta, 0) + + if context.IsDebug { + // TODO: record info when is debug mode + } + + resp, err := c.HandleRequest(context.Ctx, ds.ToTSDBDataSource(""), req) + if err != nil { + if err == gocontext.DeadlineExceeded { + return nil, errors.Error("Alert execution exceeded the timeout") + } + + return nil, errors.Wrap(err, "tsdb.HandleRequest() error") + } + + // log.Errorf("===query resp %s", jsonutils.Marshal(resp).PrettyString()) + + for _, v := range resp.Results { + if v.Error != nil { + return nil, errors.Wrap(err, "tsdb.HandleResult() response") + } + + result = append(result, v.Series...) + metas = append(metas, v.Meta) + + queryResultData := map[string]interface{}{} + + if context.IsTestRun { + queryResultData["series"] = v.Series + } + + /*if context.IsDebug && v.Meta != nil { + queryResultData["meta"] = v.Meta + }*/ + + if context.IsTestRun || context.IsDebug { + context.Logs = append(context.Logs, &alerting.ResultLogEntry{ + Message: fmt.Sprintf("Condition[%d]: Query Result", c.Index), + Data: queryResultData, + }) + } + } + + return &queryResult{ + series: result, + metas: metas, + }, nil +} + +func (c *QueryCondition) getRequestForAlertRule(ds *models.SDataSource, timeRange *tsdb.TimeRange, debug bool) *tsdb.TsdbQuery { + req := &tsdb.TsdbQuery{ + TimeRange: timeRange, + Queries: []*tsdb.Query{ + { + RefId: "A", + MetricQuery: c.Query.Model, + DataSource: *ds.ToTSDBDataSource(c.Query.Model.Database), + }, + }, + Debug: debug, + } + return req +} + +func newQueryCondition(model *monitor.AlertCondition, index int) (*QueryCondition, error) { + cond := new(QueryCondition) + cond.Index = index + cond.HandleRequest = tsdb.HandleRequest + + q := model.Query + cond.Query.Model = q.Model + cond.Query.From = q.From + cond.Query.To = q.To + + if err := validators.ValidateFromValue(cond.Query.From); err != nil { + return nil, errors.Wrapf(err, "from value %q", cond.Query.From) + } + + if err := validators.ValidateToValue(cond.Query.To); err != nil { + return nil, errors.Wrapf(err, "to value %q", cond.Query.To) + } + + cond.Query.DataSourceId = q.DataSourceId + reducer := model.Reducer + cond.Reducer = newSimpleReducer(reducer.Type) + + evaluator, err := NewAlertEvaluator(&model.Evaluator) + if err != nil { + return nil, fmt.Errorf("error in condition %v: %v", index, err) + } + cond.Evaluator = evaluator + operator := model.Operator + if operator == "" { + operator = "and" + } + cond.Operator = operator + + return cond, nil +} diff --git a/pkg/monitor/alerting/conditions/query_test.go b/pkg/monitor/alerting/conditions/query_test.go new file mode 100644 index 0000000000..b3d30cf928 --- /dev/null +++ b/pkg/monitor/alerting/conditions/query_test.go @@ -0,0 +1,15 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package conditions diff --git a/pkg/monitor/alerting/conditions/reducer.go b/pkg/monitor/alerting/conditions/reducer.go new file mode 100644 index 0000000000..77b6c52ac7 --- /dev/null +++ b/pkg/monitor/alerting/conditions/reducer.go @@ -0,0 +1,169 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package conditions + +import ( + "math" + "sort" + + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +// queryReducer reduces an timeseries to a float +type queryReducer struct { + // Type is how the timeseries should be reduced. + // Ex: avg, sum, max, min, count + Type string +} + +func (s *queryReducer) Reduce(series *tsdb.TimeSeries) *float64 { + if len(series.Points) == 0 { + return nil + } + + value := float64(0) + allNull := true + + switch s.Type { + case "avg": + validPointsCount := 0 + for _, point := range series.Points { + if point.IsValid() { + value += point.Value() + validPointsCount++ + allNull = false + } + } + if validPointsCount > 0 { + value = value / float64(validPointsCount) + } + case "sum": + for _, point := range series.Points { + if point.IsValid() { + value += point.Value() + allNull = false + } + } + case "min": + value = math.MaxFloat64 + for _, point := range series.Points { + if point.IsValid() { + allNull = false + if value > point.Value() { + value = point.Value() + } + } + } + case "max": + value = -math.MaxFloat64 + for _, point := range series.Points { + if point.IsValid() { + allNull = false + if value < point.Value() { + value = point.Value() + } + } + } + case "count": + value = float64(len(series.Points)) + allNull = false + case "last": + points := series.Points + for i := len(points) - 1; i >= 0; i-- { + if points[i].IsValid() { + value = points[i].Value() + allNull = false + break + } + } + case "median": + var values []float64 + for _, v := range series.Points { + if v.IsValid() { + allNull = false + values = append(values, v.Value()) + } + } + if len(values) >= 1 { + sort.Float64s(values) + length := len(values) + if length%2 == 1 { + value = values[(length-1)/2] + } else { + value = (values[(length/2)-1] + values[length/2]) / 2 + } + } + case "diff": + allNull, value = calculateDiff(series, allNull, value, diff) + case "percent_diff": + allNull, value = calculateDiff(series, allNull, value, percentDiff) + case "count_non_null": + for _, v := range series.Points { + if v.IsValid() { + value++ + } + } + + if value > 0 { + allNull = false + } + } + + if allNull { + return nil + } + + return &value +} + +func newSimpleReducer(t string) *queryReducer { + return &queryReducer{Type: t} +} + +func calculateDiff(series *tsdb.TimeSeries, allNull bool, value float64, fn func(float64, float64) float64) (bool, float64) { + var ( + points = series.Points + first float64 + i int + ) + // get the newest point + for i = len(points) - 1; i >= 0; i-- { + if points[i].IsValid() { + allNull = false + first = points[i].Value() + break + } + } + if i >= 1 { + // get the oldest point + for i := 0; i < len(points); i++ { + if points[i].IsValid() { + allNull = false + val := fn(first, points[i].Value()) + value = math.Abs(val) + break + } + } + } + return allNull, value +} + +var diff = func(newest, oldest float64) float64 { + return newest - oldest +} + +var percentDiff = func(newest, oldest float64) float64 { + return (newest - oldest) / oldest * 100 +} diff --git a/pkg/monitor/alerting/conditions/reducer_test.go b/pkg/monitor/alerting/conditions/reducer_test.go new file mode 100644 index 0000000000..dc0f4aada0 --- /dev/null +++ b/pkg/monitor/alerting/conditions/reducer_test.go @@ -0,0 +1,202 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package conditions + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" + + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +func TestSimpleReducer(t *testing.T) { + Convey("Test simple reducer by calculating", t, func() { + + Convey("sum", func() { + result := testReducer("sum", 1, 2, 3) + So(result, ShouldEqual, float64(6)) + }) + + Convey("min", func() { + result := testReducer("min", 3, 2, 1) + So(result, ShouldEqual, float64(1)) + }) + + Convey("max", func() { + result := testReducer("max", 1, 2, 3) + So(result, ShouldEqual, float64(3)) + }) + + Convey("count", func() { + result := testReducer("count", 1, 2, 3000) + So(result, ShouldEqual, float64(3)) + }) + + Convey("last", func() { + result := testReducer("last", 1, 2, 3000) + So(result, ShouldEqual, float64(3000)) + }) + + Convey("median odd amount of numbers", func() { + result := testReducer("median", 1, 2, 3000) + So(result, ShouldEqual, float64(2)) + }) + + Convey("median even amount of numbers", func() { + result := testReducer("median", 1, 2, 4, 3000) + So(result, ShouldEqual, float64(3)) + }) + + Convey("median with one values", func() { + result := testReducer("median", 1) + So(result, ShouldEqual, float64(1)) + }) + + Convey("median should ignore null values", func() { + reducer := newSimpleReducer("median") + series := &tsdb.TimeSeries{ + Name: "test time series", + } + + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 1)) + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 2)) + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 3)) + series.Points = append(series.Points, tsdb.NewTimePointByVal(1, 4)) + series.Points = append(series.Points, tsdb.NewTimePointByVal(2, 5)) + series.Points = append(series.Points, tsdb.NewTimePointByVal(3, 6)) + + result := reducer.Reduce(series) + So(result, ShouldNotBeNil) + So(*result, ShouldEqual, 2) + }) + + Convey("avg", func() { + result := testReducer("avg", 1, 2, 3) + So(result, ShouldEqual, float64(2)) + }) + + Convey("count_non_null", func() { + Convey("with null values and real values", func() { + reducer := newSimpleReducer("count_non_null") + series := &tsdb.TimeSeries{ + Name: "test time series", + } + + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 1)) + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 2)) + series.Points = append(series.Points, tsdb.NewTimePointByVal(3, 3)) + series.Points = append(series.Points, tsdb.NewTimePointByVal(4, 4)) + + So(reducer.Reduce(series), ShouldNotBeNil) + So(*reducer.Reduce(series), ShouldEqual, 2) + }) + + Convey("with null values", func() { + reducer := newSimpleReducer("count_non_null") + series := &tsdb.TimeSeries{ + Name: "test time series", + } + + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 1)) + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 2)) + + So(reducer.Reduce(series), ShouldBeNil) + }) + }) + + Convey("avg of number values and null values should ignore nulls", func() { + reduer := newSimpleReducer("avg") + series := &tsdb.TimeSeries{ + Name: "test time series", + } + + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 1)) + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 2)) + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 3)) + series.Points = append(series.Points, tsdb.NewTimePointByVal(3, 4)) + + So(*reduer.Reduce(series), ShouldEqual, 3) + }) + + Convey("diff one point", func() { + result := testReducer("diff", 30) + So(result, ShouldEqual, float64(0)) + }) + + Convey("diff two points", func() { + result := testReducer("diff", 30, 40) + So(result, ShouldEqual, float64(10)) + }) + + Convey("diff three points", func() { + result := testReducer("diff", 30, 40, 40) + So(result, ShouldEqual, float64(10)) + }) + + Convey("diff with only nulls", func() { + reducer := newSimpleReducer("diff") + series := &tsdb.TimeSeries{ + Name: "test time serie", + } + + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 1)) + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 2)) + + So(reducer.Reduce(series), ShouldBeNil) + }) + + Convey("percent_diff one point", func() { + result := testReducer("percent_diff", 40) + So(result, ShouldEqual, float64(0)) + }) + + Convey("percent_diff two points", func() { + result := testReducer("percent_diff", 30, 40) + So(result, ShouldEqual, float64(33.33333333333333)) + }) + + Convey("percent_diff three points", func() { + result := testReducer("percent_diff", 30, 40, 40) + So(result, ShouldEqual, float64(33.33333333333333)) + }) + + Convey("percent_diff with only nulls", func() { + reducer := newSimpleReducer("percent_diff") + series := &tsdb.TimeSeries{ + Name: "test time serie", + } + + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 1)) + series.Points = append(series.Points, tsdb.NewTimePoint(nil, 2)) + + So(reducer.Reduce(series), ShouldBeNil) + }) + }) +} + +func testReducer(reducerType string, datapoints ...float64) float64 { + reducer := newSimpleReducer(reducerType) + serires := &tsdb.TimeSeries{ + Name: "test time series", + } + + for idx := range datapoints { + val := datapoints[idx] + serires.Points = append(serires.Points, tsdb.NewTimePoint(&val, 1234134)) + } + + return *reducer.Reduce(serires) +} diff --git a/pkg/monitor/alerting/doc.go b/pkg/monitor/alerting/doc.go new file mode 100644 index 0000000000..c92ecbcc01 --- /dev/null +++ b/pkg/monitor/alerting/doc.go @@ -0,0 +1 @@ +package alerting // import "yunion.io/x/onecloud/pkg/monitor/alerting" diff --git a/pkg/monitor/alerting/engine.go b/pkg/monitor/alerting/engine.go new file mode 100644 index 0000000000..2ff744df6e --- /dev/null +++ b/pkg/monitor/alerting/engine.go @@ -0,0 +1,240 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "context" + "runtime/debug" + "time" + + "github.com/benbjohnson/clock" + "golang.org/x/sync/errgroup" + "golang.org/x/xerrors" + + "yunion.io/x/log" + + "yunion.io/x/onecloud/pkg/mcclient/auth" + "yunion.io/x/onecloud/pkg/monitor/options" + "yunion.io/x/onecloud/pkg/monitor/registry" +) + +// AlertEngine is the background process that +// schedules alert evaluations and makes sure notifications +// are sent. +type AlertEngine struct { + execQueue chan *Job + ticker *Ticker + scheduler scheduler + evalHandler evalHandler + ruleReader ruleReader + resultHandler resultHandler +} + +func init() { + registry.RegisterService(&AlertEngine{}) +} + +// IsDisabled returns true if the alerting service is disable for this instance. +func (e *AlertEngine) IsDisabled() bool { + // TODO: read from config options + return false +} + +// Init initalizes the AlertingService. +func (e *AlertEngine) Init() error { + e.ticker = NewTicker(time.Now(), time.Second*0, clock.New()) + e.execQueue = make(chan *Job, 1000) + e.scheduler = newScheduler() + e.evalHandler = NewEvalHandler() + e.ruleReader = newRuleReader() + e.resultHandler = newResultHandler() + return nil +} + +// Run starts the alerting service background process. +func (e *AlertEngine) Run(ctx context.Context) error { + alertGroup, ctx := errgroup.WithContext(ctx) + alertGroup.Go(func() error { return e.alertingTicker(ctx) }) + alertGroup.Go(func() error { return e.runJobDispatcher(ctx) }) + + err := alertGroup.Wait() + return err +} + +func (e *AlertEngine) alertingTicker(ctx context.Context) error { + defer func() { + if err := recover(); err != nil { + log.Errorf("Scheduler panic: stopping alertingTicker, error: %v", err) + debug.PrintStack() + } + }() + + tickIndex := 0 + + for { + select { + case <-ctx.Done(): + return ctx.Err() + case tick := <-e.ticker.C: + // TEMP SOLUTION update rules ever tenth tick + if tickIndex%10 == 0 { + e.scheduler.Update(e.ruleReader.fetch()) + } + + e.scheduler.Tick(tick, e.execQueue) + tickIndex++ + } + } +} + +func (e *AlertEngine) runJobDispatcher(ctx context.Context) error { + dispatcherGroup, alertCtx := errgroup.WithContext(ctx) + + for { + select { + case <-ctx.Done(): + return dispatcherGroup.Wait() + case job := <-e.execQueue: + dispatcherGroup.Go(func() error { return e.processJobWithRetry(alertCtx, job) }) + } + } +} + +var ( + unfinishedWorkTimeout = time.Second * 5 +) + +func (e *AlertEngine) processJobWithRetry(ctx context.Context, job *Job) error { + defer func() { + if err := recover(); err != nil { + log.Errorf("Alert panic, error: %v", err) + } + }() + + cancelChan := make(chan context.CancelFunc, options.Options.AlertingMaxAttempts*2) + attemptChan := make(chan int, 1) + + // Initialize with first attemptID=1 + attemptChan <- 1 + job.SetRunning(true) + + for { + select { + case <-ctx.Done(): + // In case monitor server is cancel, let a chance to job processing + // to finish gracefully - by waiting a timeout duration - + unfinishedWorkTimer := time.NewTimer(unfinishedWorkTimeout) + select { + case <-unfinishedWorkTimer.C: + return e.endJob(ctx.Err(), cancelChan, job) + case <-attemptChan: + return e.endJob(nil, cancelChan, job) + } + case attemptId, more := <-attemptChan: + if !more { + return e.endJob(nil, cancelChan, job) + } + go e.processJob(attemptId, attemptChan, cancelChan, job) + } + } +} + +func (e *AlertEngine) endJob(err error, cancelChan chan context.CancelFunc, job *Job) error { + job.SetRunning(false) + close(cancelChan) + for cancelFn := range cancelChan { + cancelFn() + } + return err +} + +func (e *AlertEngine) processJob(attemptID int, attemptChan chan int, cancelChan chan context.CancelFunc, job *Job) { + defer func() { + if err := recover(); err != nil { + log.Errorf("Alert Panic: error: %v", err) + } + }() + + alertCtx, cancelFn := context.WithTimeout(context.Background(), time.Duration(options.Options.AlertingEvaluationTimeoutSeconds)*time.Second) + cancelChan <- cancelFn + // span := opentracing.StartSpan("alert execution") + // alertCtx = opentracing.ContextWithSpan(alertCtx, span) + + evalContext := NewEvalContext(alertCtx, auth.AdminCredential(), job.Rule) + evalContext.Ctx = alertCtx + + go func() { + defer func() { + if err := recover(); err != nil { + log.Errorf("Alert panic, error: %v", err) + debug.PrintStack() + // ext.Error.Set(span, true) + // span.LogFields( + // tlog.Error(fmt.Errorf("%v", err)), + // tlog.String("message", "failed to execute alert rule. panic was recovered."), + //) + //span.Finish() + close(attemptChan) + } + }() + + e.evalHandler.Eval(evalContext) + + /*span.SetTag("alertId", evalContext.Rule.ID) + span.SetTag("dashboardId", evalContext.Rule.DashboardID) + span.SetTag("firing", evalContext.Firing) + span.SetTag("nodatapoints", evalContext.NoDataFound) + span.SetTag("attemptID", attemptID)*/ + + if evalContext.Error != nil { + /*ext.Error.Set(span, true) + span.LogFields( + tlog.Error(evalContext.Error), + tlog.String("message", "alerting execution attempt failed"), + ) + */ + if attemptID < options.Options.AlertingMaxAttempts { + // span.Finish( + log.Debugf("Job Execution attempt triggered retry, timeMs: %v, alertId: %d", evalContext.GetDurationMs(), attemptID) + attemptChan <- (attemptID + 1) + return + } + } + + // create new context with timeout for notifications + resultHandleCtx, resultHandleCancelFn := context.WithTimeout(context.Background(), time.Duration(options.Options.AlertingNotificationTimeoutSeconds)*time.Second) + cancelChan <- resultHandleCancelFn + + // override the context used for evaluation with a new context for notifications. + // This makes it possible for notifiers to execute when datasources + // don't respond within the timeout limit. We should rewrite this so notifications + // don't reuse the evalContext and get its own context. + evalContext.Ctx = resultHandleCtx + evalContext.Rule.State = evalContext.GetNewState() + if err := e.resultHandler.handle(evalContext); err != nil { + if xerrors.Is(err, context.Canceled) { + log.Debugf("Result handler returned context.Canceled") + } else if xerrors.Is(err, context.DeadlineExceeded) { + log.Debugf("Result handler returned context.DeadlineExceeded") + } else { + log.Errorf("Failed to handle result: %v", err) + } + } + + // span.Finish() + log.Debugf("Job execution completed, timeMs: %v, alertId: %s, attemptId: %d", evalContext.GetDurationMs(), evalContext.Rule.Id, attemptID) + close(attemptChan) + }() +} diff --git a/pkg/monitor/alerting/eval_context.go b/pkg/monitor/alerting/eval_context.go new file mode 100644 index 0000000000..ae5b256a48 --- /dev/null +++ b/pkg/monitor/alerting/eval_context.go @@ -0,0 +1,194 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "context" + "time" + + "yunion.io/x/log" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/mcclient" +) + +// EvalContext is the context object for an alert evaluation. +type EvalContext struct { + Firing bool + IsTestRun bool + IsDebug bool + EvalMatches []*EvalMatch + Logs []*ResultLogEntry + Error error + ConditionEvals string + StartTime time.Time + EndTime time.Time + Rule *Rule + + NoDataFound bool + PrevAlertState monitor.AlertStateType + + Ctx context.Context + UserCred mcclient.TokenCredential +} + +// NewEvalContext is the EvalContext constructor. +func NewEvalContext(alertCtx context.Context, userCred mcclient.TokenCredential, rule *Rule) *EvalContext { + return &EvalContext{ + Ctx: alertCtx, + UserCred: userCred, + StartTime: time.Now(), + Rule: rule, + EvalMatches: make([]*EvalMatch, 0), + PrevAlertState: rule.State, + } +} + +// SateDescription contains visual information about the alert state. +type StateDescription struct { + //Color string + Text string + Data string +} + +// GetStateModel returns the `StateDescription` based on current state. +func (c *EvalContext) GetStateModel() *StateDescription { + switch c.Rule.State { + case monitor.AlertStateOK: + return &StateDescription{ + Text: "OK", + } + case monitor.AlertStateNoData: + return &StateDescription{ + Text: "No Data", + } + case monitor.AlertStateAlerting: + return &StateDescription{ + Text: "Alerting", + } + case monitor.AlertStateUnknown: + return &StateDescription{ + Text: "Unknown", + } + default: + panic("Unknown rule state for alert " + c.Rule.State) + } +} + +func (c *EvalContext) shouldUpdateAlertState() bool { + return c.Rule.State != c.PrevAlertState +} + +// GetDurationMs returns the duration of the alert evaluation. +func (c *EvalContext) GetDurationMs() float64 { + return float64(c.EndTime.Nanosecond()-c.StartTime.Nanosecond()) / float64(1000000) +} + +func (c *EvalContext) GetRuleTitle() string { + rule := c.Rule + if rule.Title != "" { + return rule.Title + } + return rule.Name +} + +// GetNotificationTitle returns the title of the alert rule including alert state. +func (c *EvalContext) GetNotificationTitle() string { + return "[" + c.GetStateModel().Text + "] " + c.GetRuleTitle() +} + +// GetNewState returns the new state from the alert rule evaluation. +func (c *EvalContext) GetNewState() monitor.AlertStateType { + ns := getNewStateInternal(c) + if ns != monitor.AlertStateAlerting || c.Rule.For == 0 { + return ns + } + + since := time.Since(c.Rule.LastStateChange) + if c.PrevAlertState == monitor.AlertStatePending && since > c.Rule.For { + return monitor.AlertStateAlerting + } + + if c.PrevAlertState == monitor.AlertStateAlerting { + return monitor.AlertStateAlerting + } + + return monitor.AlertStatePending +} + +func getNewStateInternal(c *EvalContext) monitor.AlertStateType { + if c.Error != nil { + log.Errorf("Alert Rule Result Error, ruleId: %s, name: %s, error: %v, changing state to %v", + c.Rule.Id, + c.Rule.Name, + c.Error, + c.Rule.ExecutionErrorState.ToAlertState()) + + if c.Rule.ExecutionErrorState == monitor.ExecutionErrorKeepState { + return c.PrevAlertState + } + return c.Rule.ExecutionErrorState.ToAlertState() + } + + if c.Firing { + return monitor.AlertStateAlerting + } + + if c.NoDataFound { + log.Infof("Alert Rule returned no data, ruleId: %s, name: %s, changing state to %v", + c.Rule.Id, + c.Rule.Name, + c.Rule.NoDataState.ToAlertState()) + + if c.Rule.NoDataState == monitor.NoDataKeepState { + return c.PrevAlertState + } + return c.Rule.NoDataState.ToAlertState() + } + + return monitor.AlertStateOK +} + +func (c *EvalContext) GetNotificationTemplateConfig() monitor.NotificationTemplateConfig { + desc := c.Rule.Message + if c.Error != nil { + if desc != "" { + desc += "\n" + } + desc += "Error: " + c.Error.Error() + } + return monitor.NotificationTemplateConfig{ + Title: c.GetNotificationTitle(), + Name: c.Rule.Name, + Matches: c.GetEvalMatches(), + StartTime: c.StartTime.Format(time.RFC3339), + EndTime: c.EndTime.Format(time.RFC3339), + Description: desc, + Level: c.Rule.Level, + } +} + +func (c *EvalContext) GetEvalMatches() []monitor.EvalMatch { + ret := make([]monitor.EvalMatch, 0) + for _, c := range c.EvalMatches { + ret = append(ret, monitor.EvalMatch{ + Condition: c.Condition, + Value: c.Value, + Metric: c.Metric, + Tags: c.Tags, + }) + } + return ret +} diff --git a/pkg/monitor/alerting/eval_context_test.go b/pkg/monitor/alerting/eval_context_test.go new file mode 100644 index 0000000000..10d8aabdc4 --- /dev/null +++ b/pkg/monitor/alerting/eval_context_test.go @@ -0,0 +1,220 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "yunion.io/x/onecloud/pkg/apis/monitor" +) + +func TestStateIsUpdatedWhenNeeded(t *testing.T) { + ctx := NewEvalContext(context.TODO(), nil, &Rule{Conditions: []Condition{&conditionStub{firing: true}}}) + + t.Run("ok -> alerting", func(t *testing.T) { + ctx.PrevAlertState = monitor.AlertStateOK + ctx.Rule.State = monitor.AlertStateAlerting + + if !ctx.shouldUpdateAlertState() { + t.Fatalf("expected should updated to be true") + } + }) + + t.Run("ok -> ok", func(t *testing.T) { + ctx.PrevAlertState = monitor.AlertStateOK + ctx.Rule.State = monitor.AlertStateOK + + if ctx.shouldUpdateAlertState() { + t.Fatalf("expected should updated to be false") + } + }) +} + +func TestGetStateFromEvalContext(t *testing.T) { + tcs := []struct { + name string + expected monitor.AlertStateType + applyFn func(ec *EvalContext) + }{ + { + name: "ok -> alerting", + expected: monitor.AlertStateAlerting, + applyFn: func(ec *EvalContext) { + ec.Firing = true + ec.PrevAlertState = monitor.AlertStateOK + }, + }, + { + name: "ok -> error(alerting)", + expected: monitor.AlertStateAlerting, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStateOK + ec.Error = errors.New("test error") + ec.Rule.ExecutionErrorState = monitor.ExecutionErrorSetAlerting + }, + }, + { + name: "ok -> pending. since its been firing for less than FOR", + expected: monitor.AlertStatePending, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStateOK + ec.Firing = true + ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 2) + ec.Rule.For = time.Minute * 5 + }, + }, + { + name: "ok -> pending. since it has to be pending longer than FOR and prev state is ok", + expected: monitor.AlertStatePending, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStateOK + ec.Firing = true + ec.Rule.LastStateChange = time.Now().Add(-(time.Hour * 5)) + ec.Rule.For = time.Minute * 2 + }, + }, + { + name: "pending -> alerting. since its been firing for more than FOR and prev state is pending", + expected: monitor.AlertStateAlerting, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStatePending + ec.Firing = true + ec.Rule.LastStateChange = time.Now().Add(-(time.Hour * 5)) + ec.Rule.For = time.Minute * 2 + }, + }, + { + name: "alerting -> alerting. should not update regardless of FOR", + expected: monitor.AlertStateAlerting, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStateAlerting + ec.Firing = true + ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 5) + ec.Rule.For = time.Minute * 2 + }, + }, + { + name: "ok -> ok. should not update regardless of FOR", + expected: monitor.AlertStateOK, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStateOK + ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 5) + ec.Rule.For = time.Minute * 2 + }, + }, + { + name: "ok -> error(keep_last)", + expected: monitor.AlertStateOK, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStateOK + ec.Error = errors.New("test error") + ec.Rule.ExecutionErrorState = monitor.ExecutionErrorKeepState + }, + }, + { + name: "pending -> error(keep_last)", + expected: monitor.AlertStatePending, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStatePending + ec.Error = errors.New("test error") + ec.Rule.ExecutionErrorState = monitor.ExecutionErrorKeepState + }, + }, + { + name: "ok -> no_data(alerting)", + expected: monitor.AlertStateAlerting, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStateOK + ec.Rule.NoDataState = monitor.NoDataSetAlerting + ec.NoDataFound = true + }, + }, + { + name: "ok -> no_data(keep_last)", + expected: monitor.AlertStateOK, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStateOK + ec.Rule.NoDataState = monitor.NoDataKeepState + ec.NoDataFound = true + }, + }, + { + name: "pending -> no_data(keep_last)", + expected: monitor.AlertStatePending, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStatePending + ec.Rule.NoDataState = monitor.NoDataKeepState + ec.NoDataFound = true + }, + }, + { + name: "pending -> no_data(alerting) with for duration have not passed", + expected: monitor.AlertStatePending, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStatePending + ec.Rule.NoDataState = monitor.NoDataSetAlerting + ec.NoDataFound = true + ec.Rule.For = time.Minute * 5 + ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 2) + }, + }, + { + name: "pending -> no_data(alerting) should set alerting since time passed FOR", + expected: monitor.AlertStateAlerting, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStatePending + ec.Rule.NoDataState = monitor.NoDataSetAlerting + ec.NoDataFound = true + ec.Rule.For = time.Minute * 2 + ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 5) + }, + }, + { + name: "pending -> error(alerting) with for duration have not passed ", + expected: monitor.AlertStatePending, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStatePending + ec.Rule.ExecutionErrorState = monitor.ExecutionErrorSetAlerting + ec.Error = errors.New("test error") + ec.Rule.For = time.Minute * 5 + ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 2) + }, + }, + { + name: "pending -> error(alerting) should set alerting since time passed FOR", + expected: monitor.AlertStateAlerting, + applyFn: func(ec *EvalContext) { + ec.PrevAlertState = monitor.AlertStatePending + ec.Rule.ExecutionErrorState = monitor.ExecutionErrorSetAlerting + ec.Error = errors.New("test error") + ec.Rule.For = time.Minute * 2 + ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 5) + }, + }, + } + + for _, tc := range tcs { + evalContext := NewEvalContext(context.Background(), nil, &Rule{Conditions: []Condition{&conditionStub{firing: true}}}) + + tc.applyFn(evalContext) + newState := evalContext.GetNewState() + assert.Equal(t, tc.expected, newState, "failed: %s \n expected '%s' have '%s'\n", tc.name, tc.expected, string(newState)) + } +} diff --git a/pkg/monitor/alerting/eval_handler.go b/pkg/monitor/alerting/eval_handler.go new file mode 100644 index 0000000000..869000e390 --- /dev/null +++ b/pkg/monitor/alerting/eval_handler.go @@ -0,0 +1,83 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "strconv" + "strings" + "time" +) + +// DefaultEvalHandler is responsible for evaluating the alert rule. +type DefaultEvalHandler struct { + alertJobTimeout time.Duration +} + +// NewEvalHandler is the `DefaultEvalHandler` constructor. +func NewEvalHandler() *DefaultEvalHandler { + return &DefaultEvalHandler{ + alertJobTimeout: time.Second * 5, + } +} + +// Eval evaluated the alert rule. +func (e *DefaultEvalHandler) Eval(context *EvalContext) { + firing := true + noDataFound := true + conditionEvals := "" + + for i := 0; i < len(context.Rule.Conditions); i++ { + condition := context.Rule.Conditions[i] + cr, err := condition.Eval(context) + if err != nil { + context.Error = err + } + + // break if condition could not be evaluated + if context.Error != nil { + break + } + + if i == 0 { + firing = cr.Firing + noDataFound = cr.NoDataFound + } + + // calculating Firing based on operator + if cr.Operator == "or" { + firing = firing || cr.Firing + noDataFound = noDataFound || cr.NoDataFound + } else { + firing = firing && cr.Firing + noDataFound = noDataFound && cr.NoDataFound + } + + if i > 0 { + conditionEvals = "[" + conditionEvals + " " + strings.ToUpper(cr.Operator) + " " + strconv.FormatBool(cr.Firing) + "]" + } else { + conditionEvals = strconv.FormatBool(firing) + } + + context.EvalMatches = append(context.EvalMatches, cr.EvalMatches...) + } + + context.ConditionEvals = conditionEvals + " = " + strconv.FormatBool(firing) + context.Firing = firing + context.NoDataFound = noDataFound + context.EndTime = time.Now() + + // elapsedTime := ctx.EndTime.Sub(ctx.StartTime).Nanoseconds() / int64(time.Millisecond) + // metrics.MAlertingExecutionTime.Observe(float64(elapsedTime)) +} diff --git a/pkg/monitor/alerting/eval_handler_test.go b/pkg/monitor/alerting/eval_handler_test.go new file mode 100644 index 0000000000..c0a94784a6 --- /dev/null +++ b/pkg/monitor/alerting/eval_handler_test.go @@ -0,0 +1,216 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "context" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +type conditionStub struct { + firing bool + operator string + matches []*EvalMatch + noData bool +} + +func (c *conditionStub) Eval(context *EvalContext) (*ConditionResult, error) { + return &ConditionResult{Firing: c.firing, EvalMatches: c.matches, Operator: c.operator, NoDataFound: c.noData}, nil +} + +func TestAlertingEvaluationHandler(t *testing.T) { + Convey("Test alert evaluation handler", t, func() { + handler := NewEvalHandler() + + Convey("Show return triggered with single passing condition", func() { + ctx := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{&conditionStub{firing: true}}, + }) + + handler.Eval(ctx) + So(ctx.Firing, ShouldEqual, true) + So(ctx.ConditionEvals, ShouldEqual, "true = true") + }) + + Convey("Show return triggered with single passing conditions2", func() { + ctx := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{&conditionStub{firing: true, operator: "and"}}, + }) + + handler.Eval(ctx) + So(ctx.Firing, ShouldEqual, true) + So(ctx.ConditionEvals, ShouldEqual, "true = true") + }) + + Convey("Show return false with not passing asdf", func() { + ctx := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{firing: true, operator: "and", matches: []*EvalMatch{{}, {}}}, + &conditionStub{firing: false, operator: "and"}, + }}) + + handler.Eval(ctx) + So(ctx.Firing, ShouldEqual, false) + So(ctx.ConditionEvals, ShouldEqual, "[true AND false] = false") + }) + + Convey("Show return true if any of condition is passing with OR operator", func() { + ctx := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{firing: true, operator: "and"}, + &conditionStub{firing: false, operator: "or"}, + }, + }) + + handler.Eval(ctx) + So(ctx.Firing, ShouldEqual, true) + So(ctx.ConditionEvals, ShouldEqual, "[true OR false] = true") + }) + + Convey("Show return false if any of the condition is failing with AND operator", func() { + context := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{firing: true, operator: "and"}, + &conditionStub{firing: false, operator: "and"}, + }, + }) + + handler.Eval(context) + So(context.Firing, ShouldEqual, false) + So(context.ConditionEvals, ShouldEqual, "[true AND false] = false") + }) + + Convey("Show return true if one condition is failing with nested OR operator", func() { + context := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{firing: true, operator: "and"}, + &conditionStub{firing: true, operator: "and"}, + &conditionStub{firing: false, operator: "or"}, + }, + }) + + handler.Eval(context) + So(context.Firing, ShouldEqual, true) + So(context.ConditionEvals, ShouldEqual, "[[true AND true] OR false] = true") + }) + + Convey("Show return false if one condition is passing with nested OR operator", func() { + context := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{firing: true, operator: "and"}, + &conditionStub{firing: false, operator: "and"}, + &conditionStub{firing: false, operator: "or"}, + }, + }) + + handler.Eval(context) + So(context.Firing, ShouldEqual, false) + So(context.ConditionEvals, ShouldEqual, "[[true AND false] OR false] = false") + }) + + Convey("Show return false if a condition is failing with nested AND operator", func() { + context := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{firing: true, operator: "and"}, + &conditionStub{firing: false, operator: "and"}, + &conditionStub{firing: true, operator: "and"}, + }, + }) + + handler.Eval(context) + So(context.Firing, ShouldEqual, false) + So(context.ConditionEvals, ShouldEqual, "[[true AND false] AND true] = false") + }) + + Convey("Show return true if a condition is passing with nested OR operator", func() { + context := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{firing: true, operator: "and"}, + &conditionStub{firing: false, operator: "or"}, + &conditionStub{firing: true, operator: "or"}, + }, + }) + + handler.Eval(context) + So(context.Firing, ShouldEqual, true) + So(context.ConditionEvals, ShouldEqual, "[[true OR false] OR true] = true") + }) + + Convey("Should return false if no condition is firing using OR operator", func() { + context := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{firing: false, operator: "or"}, + &conditionStub{firing: false, operator: "or"}, + &conditionStub{firing: false, operator: "or"}, + }, + }) + + handler.Eval(context) + So(context.Firing, ShouldEqual, false) + So(context.ConditionEvals, ShouldEqual, "[[false OR false] OR false] = false") + }) + + Convey("Should retuasdfrn no data if one condition has nodata", func() { + context := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{operator: "or", noData: false}, + &conditionStub{operator: "or", noData: false}, + &conditionStub{operator: "or", noData: false}, + }, + }) + + handler.Eval(context) + So(context.NoDataFound, ShouldBeFalse) + }) + + Convey("Should return no data if one condition has nodata", func() { + context := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{operator: "and", noData: true}, + }, + }) + + handler.Eval(context) + So(context.Firing, ShouldEqual, false) + So(context.NoDataFound, ShouldBeTrue) + }) + + Convey("Should return no data if both conditions have no data and using AND", func() { + context := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{operator: "and", noData: true}, + &conditionStub{operator: "and", noData: false}, + }, + }) + + handler.Eval(context) + So(context.NoDataFound, ShouldBeFalse) + }) + + Convey("Should not return no data if both conditions have no data and using OR", func() { + ctx := NewEvalContext(context.TODO(), nil, &Rule{ + Conditions: []Condition{ + &conditionStub{operator: "or", noData: true}, + &conditionStub{operator: "or", noData: false}, + }, + }) + + handler.Eval(ctx) + So(ctx.NoDataFound, ShouldBeTrue) + }) + }) +} diff --git a/pkg/monitor/alerting/interfaces.go b/pkg/monitor/alerting/interfaces.go new file mode 100644 index 0000000000..9bc8efd5bc --- /dev/null +++ b/pkg/monitor/alerting/interfaces.go @@ -0,0 +1,54 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "context" + "time" + + "yunion.io/x/onecloud/pkg/monitor/models" + "yunion.io/x/onecloud/pkg/monitor/notifydrivers" +) + +type evalHandler interface { + Eval(ctx *EvalContext) +} + +type scheduler interface { + Tick(time time.Time, execQueue chan *Job) + Update(rules []*Rule) +} + +// ConditionResult is the result of a condition evaluation. +type ConditionResult struct { + Firing bool + NoDataFound bool + Operator string + EvalMatches []*EvalMatch +} + +// Condition is responsible for evaluating an alert condition. +type Condition interface { + Eval(result *EvalContext) (*ConditionResult, error) +} + +type Notifier interface { + notifydrivers.Notifier + + Notify(evalContext *EvalContext) error + + // ShouldNotify checks this evaluation should send an alert notification + ShouldNotify(ctx context.Context, evalContext *EvalContext, notificationState *models.SAlertNotificationState) bool +} diff --git a/pkg/monitor/alerting/job.go b/pkg/monitor/alerting/job.go new file mode 100644 index 0000000000..db6d52f079 --- /dev/null +++ b/pkg/monitor/alerting/job.go @@ -0,0 +1,57 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "sync" +) + +// Job holds state about when the alert rule should be evaluated. +type Job struct { + Offset int64 + OffsetWait bool + Delay bool + running bool + Rule *Rule + runningLock sync.Mutex +} + +// GetRunning returns true if the job is running. A lock is taken and released on the Job to ensure atomicity. +func (j *Job) GetRunning() bool { + defer j.runningLock.Unlock() + j.runningLock.Lock() + return j.running +} + +// SetRunning sets the running property on the Job. A lock is taken and released on the Job to ensure atomicity. +func (j *Job) SetRunning(b bool) { + j.runningLock.Lock() + j.running = b + j.runningLock.Unlock() +} + +// ResultLogEntry represents log data for the alert evaluation. +type ResultLogEntry struct { + Message string + Data interface{} +} + +// EvalMatch represents the series violating the threshold. +type EvalMatch struct { + Condition string `json:“condition` + Value *float64 `json:"value"` + Metric string `json:"metric"` + Tags map[string]string `json:"tags"` +} diff --git a/pkg/monitor/alerting/notifier.go b/pkg/monitor/alerting/notifier.go new file mode 100644 index 0000000000..764a9ac9c0 --- /dev/null +++ b/pkg/monitor/alerting/notifier.go @@ -0,0 +1,164 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "time" + + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/monitor/models" + "yunion.io/x/onecloud/pkg/monitor/notifydrivers" +) + +type notificationService struct { +} + +func newNotificationService() *notificationService { + return ¬ificationService{} +} + +func (n *notificationService) SendIfNeeded(evalCtx *EvalContext) error { + notifierStates, err := n.getNeededNotifiers(evalCtx.Rule.Notifications, evalCtx) + if err != nil { + return errors.Wrap(err, "failed to get alert notifiers") + } + + if len(notifierStates) == 0 { + return nil + } + + return n.sendNotifications(evalCtx, notifierStates) +} + +type notifierState struct { + notifier Notifier + state *models.SAlertNotificationState +} + +type notifierStateSlice []*notifierState + +func (n *notificationService) sendNotification(evalCtx *EvalContext, state *notifierState) error { + if !evalCtx.IsTestRun { + if err := state.state.SetToPending(); err != nil { + return err + } + } + return n.sendAndMarkAsComplete(evalCtx, state) +} + +func (n *notificationService) sendAndMarkAsComplete(evalCtx *EvalContext, state *notifierState) error { + notifier := state.notifier + + log.Debugf("Sending notification, type %s, id %s", notifier.GetType(), notifier.GetNotifierId()) + + if err := notifier.Notify(evalCtx); err != nil { + log.Errorf("failed to send notification %s: %v", notifier.GetNotifierId(), err) + return err + } + + if evalCtx.IsTestRun { + return nil + } + + return state.state.SetToCompleted() +} + +func (n *notificationService) sendNotifications(evalCtx *EvalContext, states notifierStateSlice) error { + for _, state := range states { + if err := n.sendNotification(evalCtx, state); err != nil { + log.Errorf("failed to send %s notification: %v", state.notifier.GetNotifierId(), err) + if evalCtx.IsTestRun { + return err + } + } + } + return nil +} + +func (n *notificationService) getNeededNotifiers(nIds []string, evalCtx *EvalContext) (notifierStateSlice, error) { + notis, err := models.AlertNotificationManager.GetNotificationsWithDefault(nIds) + if err != nil { + return nil, err + } + + var result notifierStateSlice + for _, obj := range notis { + not, err := InitNotifier(NotificationConfig{ + Id: obj.GetId(), + Name: obj.GetName(), + Type: obj.Type, + Frequency: time.Duration(obj.Frequency), + SendReminder: obj.SendReminder, + DisableResolveMessage: obj.DisableResolveMessage, + Settings: obj.Settings, + }) + if err != nil { + log.Errorf("Could not creat enotifier %s, error: %v", obj.GetId(), err) + continue + } + state, err := models.AlertNotificationStateManager.GetOrCreateState(evalCtx.Ctx, evalCtx.UserCred, evalCtx.Rule.Id, obj.GetId()) + if err != nil { + log.Errorf("Get alert state: %v, alertId %s, notifierId: %s", err, evalCtx.Rule.Id, obj.GetId()) + continue + } + + if not.ShouldNotify(evalCtx.Ctx, evalCtx, state) { + result = append(result, ¬ifierState{ + notifier: not, + state: state, + }) + } + } + + return result, nil +} + +type NotifierPlugin struct { + Type string + Factory NotifierFactory + ValidateCreateData func(cred mcclient.IIdentityProvider, input monitor.AlertNotificationCreateInput) (monitor.AlertNotificationCreateInput, error) +} + +type NotificationConfig notifydrivers.NotificationConfig + +// NotifierFactory is a signature for creating notifiers +type NotifierFactory func(config NotificationConfig) (Notifier, error) + +func RegisterNotifier(plug *NotifierPlugin) { + notifydrivers.RegisterNotifier(¬ifydrivers.NotifierPlugin{ + Type: plug.Type, + Factory: func(cfg notifydrivers.NotificationConfig) (notifydrivers.Notifier, error) { + ret, err := plug.Factory(NotificationConfig(cfg)) + if err != nil { + return nil, err + } + return ret.(notifydrivers.Notifier), nil + }, + ValidateCreateData: plug.ValidateCreateData, + }) +} + +// InitNotifier construct a new notifier +func InitNotifier(config NotificationConfig) (Notifier, error) { + plug, err := notifydrivers.InitNotifier(notifydrivers.NotificationConfig(config)) + if err != nil { + return nil, err + } + return plug.(Notifier), nil +} diff --git a/pkg/monitor/alerting/notifiers/base.go b/pkg/monitor/alerting/notifiers/base.go new file mode 100644 index 0000000000..235fdb2fab --- /dev/null +++ b/pkg/monitor/alerting/notifiers/base.go @@ -0,0 +1,143 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package notifiers + +import ( + "context" + "time" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/monitor/alerting" + "yunion.io/x/onecloud/pkg/monitor/models" +) + +// NotifierBase is the base implentation of a notifier +type NotifierBase struct { + Name string + Type string + Id string + IsDefault bool + SendReminder bool + DisableResolveMessage bool + Frequency time.Duration +} + +// NewNotifierBase returns a new NotifierBase +func NewNotifierBase(config alerting.NotificationConfig) NotifierBase { + return NotifierBase{ + Id: config.Id, + Name: config.Name, + // IsDefault: config.IsDefault, + Type: config.Type, + SendReminder: config.SendReminder, + DisableResolveMessage: config.DisableResolveMessage, + Frequency: config.Frequency, + } +} + +// ShouldNotify checks this evaluation should send an alert notification +func (n *NotifierBase) ShouldNotify(_ context.Context, evalCtx *alerting.EvalContext, state *models.SAlertNotificationState) bool { + prevState := evalCtx.PrevAlertState + newState := evalCtx.Rule.State + + // Only notify on state change + if prevState == newState && !n.SendReminder { + return false + } + + if prevState == newState && n.SendReminder { + // Do not notify if interval has not elapsed + lastNotify := state.UpdatedAt + // if state.UpdatedAt != 0 && lastNotify.Add(n.Frequency).After(time.Now()) { + if lastNotify.Add(n.Frequency).After(time.Now()) { + return false + } + + // Do not notify if alert state is OK or pending even on repeated notify + if newState == monitor.AlertStateOK || newState == monitor.AlertStatePending { + return false + } + } + + okOrPending := newState == monitor.AlertStatePending || newState == monitor.AlertStateOK + + // Do not notify when new state is ok/pending when previous is unknown + if prevState == monitor.AlertStateUnknown && okOrPending { + return false + } + + // Do not notify when we become Pending for the first + if prevState == monitor.AlertStatePending && newState == monitor.AlertStatePending { + return false + } + + // Do not notify when we become OK from pending + if prevState == monitor.AlertStatePending && newState == monitor.AlertStateOK { + return false + } + + // Do not notify when we OK -> Pending + if prevState == monitor.AlertStateOK && newState == monitor.AlertStatePending { + return false + } + + // Do not notify if state pending and it have been updated last minute + if state.GetState() == monitor.AlertNotificationStatePending { + lastUpdated := state.UpdatedAt + if lastUpdated.Add(1 * time.Minute).After(time.Now()) { + return false + } + } + + // Do not notify when state is OK if DisableResolveMessage is set to true + if newState == monitor.AlertStateOK && n.DisableResolveMessage { + return false + } + + return true +} + +// GetType returns the notifier type. +func (n *NotifierBase) GetType() string { + return n.Type +} + +// GetNotifierId returns the notifier `uid`. +func (n *NotifierBase) GetNotifierId() string { + return n.Id +} + +// GetIsDefault returns true if the notifiers should +// be used for all alerts. +/*func (n *NotifierBase) GetIsDefault() bool { + return n.IsDeault +}*/ + +// GetSendReminder returns true if reminders should be sent. +func (n *NotifierBase) GetSendReminder() bool { + return n.SendReminder +} + +// GetDisableResolveMessage returns true if ok alert notifications +// should be skipped. +func (n *NotifierBase) GetDisableResolveMessage() bool { + return n.DisableResolveMessage +} + +// GetFrequency returns the frequency for how often +// alerts should be evaluated. +func (n *NotifierBase) GetFrequency() time.Duration { + return n.Frequency +} diff --git a/pkg/monitor/alerting/notifiers/dingding.go b/pkg/monitor/alerting/notifiers/dingding.go new file mode 100644 index 0000000000..e6f6c9a2c9 --- /dev/null +++ b/pkg/monitor/alerting/notifiers/dingding.go @@ -0,0 +1,154 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package notifiers + +import ( + "encoding/json" + "net/url" + + "yunion.io/x/jsonutils" + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/utils" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/monitor/alerting" + "yunion.io/x/onecloud/pkg/monitor/alerting/notifiers/templates" +) + +const ( + defaultDingdingMsgType = DingdingMsgTypeMarkdown + DingdingMsgTypeLink = "link" + DingdingMsgTypeMarkdown = "markdown" + DingdingMsgTypeActionCard = "actionCard" +) + +func init() { + alerting.RegisterNotifier(&alerting.NotifierPlugin{ + Type: monitor.AlertNotificationTypeDingding, + Factory: newDingdingNotifier, + ValidateCreateData: func(cred mcclient.IIdentityProvider, input monitor.AlertNotificationCreateInput) (monitor.AlertNotificationCreateInput, error) { + settings := new(monitor.NotificationSettingDingding) + if err := input.Settings.Unmarshal(settings); err != nil { + return input, errors.Wrap(err, "unmarshal setting") + } + if settings.Url == "" { + return input, httperrors.NewInputParameterError("url is empty") + } + if _, err := url.Parse(settings.Url); err != nil { + return input, httperrors.NewInputParameterError("invalid url: %v", err) + } + if settings.MessageType == "" { + settings.MessageType = defaultDingdingMsgType + } + if !utils.IsInStringArray(settings.MessageType, []string{ + DingdingMsgTypeMarkdown, + DingdingMsgTypeLink, + DingdingMsgTypeActionCard, + }) { + return input, httperrors.NewInputParameterError("unsupport type: %s", settings.MessageType) + } + input.Settings = jsonutils.Marshal(settings) + return input, nil + }, + }) +} + +type DingDingNotifier struct { + NotifierBase + MsgType string + Url string +} + +func newDingdingNotifier(config alerting.NotificationConfig) (alerting.Notifier, error) { + settings := new(monitor.NotificationSettingDingding) + if err := config.Settings.Unmarshal(settings); err != nil { + return nil, errors.Wrap(err, "unmarshal setting") + } + return &DingDingNotifier{ + NotifierBase: NewNotifierBase(config), + Url: settings.Url, + MsgType: settings.MessageType, + }, nil +} + +func (dd *DingDingNotifier) Notify(ctx *alerting.EvalContext) error { + log.Infof("Sending alert notification to dingding") + // msgUrl, err := ctx.GetRuleURL() + + body, err := dd.genBody(ctx) + if err != nil { + return err + } + input := &monitor.SendWebhookSync{ + Url: dd.Url, + Body: string(body), + } + return SendWebRequestSync(ctx.Ctx, input) +} + +func (dd *DingDingNotifier) genBody(ctx *alerting.EvalContext) ([]byte, error) { + q := url.Values{ + "pc_slide": {"false"}, + // "url": {messageURL}, + } + + // Use special link to auto open the message url outside of Dingding + // Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=385&articleId=104972&docType=1#s9 + messageURL := "dingtalk://dingtalkclient/page/link?" + q.Encode() + + log.Infof("messageUrl: " + messageURL) + + config := GetNotifyTemplateConfig(ctx) + contentConfig := templates.NewTemplateConfig(config) + content, err := contentConfig.GenerateMarkdown() + if err != nil { + return nil, errors.Wrap(err, "build content") + } + + var bodyMsg map[string]interface{} + switch dd.MsgType { + case DingdingMsgTypeMarkdown: + bodyMsg = map[string]interface{}{ + "msgtype": DingdingMsgTypeMarkdown, + DingdingMsgTypeMarkdown: map[string]string{ + "title": config.Title, + "text": content, + }, + } + case DingdingMsgTypeActionCard: + bodyMsg = map[string]interface{}{ + "msgtype": DingdingMsgTypeActionCard, + DingdingMsgTypeActionCard: map[string]string{ + "text": content, + "title": config.Title, + // "singleTitle": "More", + // "singleURL": messageURL, + }, + } + case DingdingMsgTypeLink: + bodyMsg = map[string]interface{}{ + "msgtype": DingdingMsgTypeLink, + "link": map[string]string{ + "text": content, + "title": config.Title, + // "messageUrl": messageURL, + }, + } + } + return json.Marshal(bodyMsg) +} diff --git a/pkg/monitor/alerting/notifiers/doc.go b/pkg/monitor/alerting/notifiers/doc.go new file mode 100644 index 0000000000..aab0a84ea1 --- /dev/null +++ b/pkg/monitor/alerting/notifiers/doc.go @@ -0,0 +1 @@ +package notifiers // import "yunion.io/x/onecloud/pkg/monitor/alerting/notifiers" diff --git a/pkg/monitor/alerting/notifiers/feishu.go b/pkg/monitor/alerting/notifiers/feishu.go new file mode 100644 index 0000000000..f5bc352950 --- /dev/null +++ b/pkg/monitor/alerting/notifiers/feishu.go @@ -0,0 +1,218 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package notifiers + +import ( + "fmt" + + "golang.org/x/sync/errgroup" + + "yunion.io/x/jsonutils" + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/monitor/alerting" + "yunion.io/x/onecloud/pkg/monitor/notifydrivers/feishu" +) + +func init() { + alerting.RegisterNotifier(&alerting.NotifierPlugin{ + Type: monitor.AlertNotificationTypeFeishu, + Factory: newFeishuNotifier, + ValidateCreateData: func(cred mcclient.IIdentityProvider, input monitor.AlertNotificationCreateInput) (monitor.AlertNotificationCreateInput, error) { + settings := new(monitor.NotificationSettingFeishu) + if err := input.Settings.Unmarshal(settings); err != nil { + return input, errors.Wrap(err, "unmarshal setting") + } + if settings.AppId == "" { + return input, httperrors.NewInputParameterError("app_id is empty") + } + if settings.AppSecret == "" { + return input, httperrors.NewInputParameterError("app_secret is empty") + } + _, err := feishu.NewTenant(settings.AppId, settings.AppSecret) + if err != nil { + return input, httperrors.NewGeneralError(errors.Wrap(err, "test connection")) + } + input.Settings = jsonutils.Marshal(settings) + return input, nil + }, + }) +} + +type FeishuNotifier struct { + NotifierBase + // Settings *monitor.NotificationSettingFeishu + Client *feishu.Tenant + ChatIds []string +} + +func newFeishuNotifier(config alerting.NotificationConfig) (alerting.Notifier, error) { + settings := new(monitor.NotificationSettingFeishu) + if err := config.Settings.Unmarshal(settings); err != nil { + return nil, errors.Wrap(err, "unmarshal setting") + } + cli, err := feishu.NewTenant(settings.AppId, settings.AppSecret) + if err != nil { + return nil, err + } + ret, err := cli.ChatList(0, "") + if err != nil { + return nil, err + } + chatIds := make([]string, 0) + for _, obj := range ret.Data.Groups { + chatIds = append(chatIds, obj.ChatId) + } + return &FeishuNotifier{ + NotifierBase: NewNotifierBase(config), + Client: cli, + ChatIds: chatIds, + }, nil +} + +func (fs *FeishuNotifier) Notify(ctx *alerting.EvalContext) error { + log.Infof("Sending alert notification to feishu") + errGrp := errgroup.Group{} + for _, cId := range fs.ChatIds { + errGrp.Go(func() error { + msg, err := fs.genCard(ctx, cId) + if err != nil { + return err + } + if _, err := fs.Client.SendMessage(*msg); err != nil { + log.Errorf("--feishu send msg error: %s, error: %v", jsonutils.Marshal(msg), err) + return err + } + log.Errorf("--feishu send msg: %s", jsonutils.Marshal(msg)) + return nil + }) + } + return errGrp.Wait() +} + +func (fs *FeishuNotifier) getCommonInfoMod(config monitor.NotificationTemplateConfig) feishu.CardElement { + elem := feishu.CardElement{ + Tag: feishu.TagDiv, + // Text: feishu.NewCardElementText(config.Title), + Fields: []*feishu.CardElementField{ + feishu.NewCardElementTextField(false, fmt.Sprintf("**时间:** %s", config.StartTime)), + feishu.NewCardElementTextField(false, fmt.Sprintf("**级别:** %s", config.Level)), + }, + } + return elem +} + +func (fs *FeishuNotifier) getMetricElem(idx int, m monitor.EvalMatch) *feishu.CardElement { + var val string + if m.Value == nil { + val = "NaN" + } else { + val = fmt.Sprintf("%.2f", *m.Value) + } + + elem := feishu.CardElement{ + Tag: feishu.TagDiv, + Fields: []*feishu.CardElementField{ + feishu.NewCardElementTextField(false, + fmt.Sprintf("**指标 %d:** %s", idx, m.Metric)), + feishu.NewCardElementTextField(false, + fmt.Sprintf("**当前值:** %s", val)), + feishu.NewCardElementTextField(true, + fmt.Sprintf("**触发条件:**\n%s", m.Condition)), + }, + } + return &elem +} + +func (fs *FeishuNotifier) getMetricTagElem(m monitor.EvalMatch) *feishu.CardElement { + inElems := make([]*feishu.CardElement, 0) + for val, key := range m.Tags { + inElems = append(inElems, feishu.NewCardElementText(fmt.Sprintf("%s: %s", val, key))) + } + elem := feishu.CardElement{ + Tag: feishu.TagNote, + Elements: inElems, + } + return &elem +} + +func (fs *FeishuNotifier) getMetricsMod(config monitor.NotificationTemplateConfig) []*feishu.CardElement { + inElems := make([]*feishu.CardElement, 0) + for idx, m := range config.Matches { + hrE := feishu.NewCardElementHR() + mE := fs.getMetricElem(idx+1, m) + mTE := fs.getMetricTagElem(m) + inElems = append(inElems, hrE, mE, mTE) + } + return inElems +} + +func (fs *FeishuNotifier) genCard(ctx *alerting.EvalContext, chatId string) (*feishu.MsgReq, error) { + config := GetNotifyTemplateConfig(ctx) + commonElem := fs.getCommonInfoMod(config) + + msElems := fs.getMetricsMod(config) + // 消息卡片: https://open.feishu.cn/document/ukTMukTMukTM/uYTNwUjL2UDM14iN1ATN + msg := &feishu.MsgReq{ + ChatId: chatId, + MsgType: feishu.MsgTypeInteractive, + Card: &feishu.Card{ + Config: &feishu.CardConfig{WideScreenMode: false}, + CardLink: nil, + Header: &feishu.CardHeader{ + Title: &feishu.CardHeaderTitle{ + Tag: feishu.TagPlainText, + Content: config.Title, + }, + }, + Elements: []interface{}{ + commonElem, + }, + }, + } + for _, elem := range msElems { + msg.Card.Elements = append(msg.Card.Elements, elem) + } + return msg, nil +} + +func (fs *FeishuNotifier) genMsg(ctx *alerting.EvalContext, chatId string) (*feishu.MsgReq, error) { + config := GetNotifyTemplateConfig(ctx) + // 富文本: https://open.feishu.cn/document/ukTMukTMukTM/uMDMxEjLzATMx4yMwETM + return &feishu.MsgReq{ + ChatId: chatId, + MsgType: feishu.MsgTypePost, + Content: &feishu.MsgContent{ + Post: &feishu.MsgPost{ + ZhCn: &feishu.MsgPostValue{ + Title: config.Title, + Content: []interface{}{ + []interface{}{ + feishu.MsgPostContentText{ + Tag: "text", + UnEscape: true, + Text: "first line", + }, + }, + }, + }, + }, + }, + }, nil +} diff --git a/pkg/monitor/alerting/notifiers/onecloud.go b/pkg/monitor/alerting/notifiers/onecloud.go new file mode 100644 index 0000000000..ebbbf98132 --- /dev/null +++ b/pkg/monitor/alerting/notifiers/onecloud.go @@ -0,0 +1,137 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package notifiers + +import ( + "context" + "fmt" + + "yunion.io/x/jsonutils" + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/auth" + "yunion.io/x/onecloud/pkg/mcclient/modules/notify" + "yunion.io/x/onecloud/pkg/monitor/alerting" + "yunion.io/x/onecloud/pkg/monitor/alerting/notifiers/templates" + "yunion.io/x/onecloud/pkg/monitor/options" +) + +func init() { + alerting.RegisterNotifier(&alerting.NotifierPlugin{ + Type: monitor.AlertNotificationTypeOneCloud, + Factory: newOneCloudNotifier, + ValidateCreateData: func(cred mcclient.IIdentityProvider, input monitor.AlertNotificationCreateInput) (monitor.AlertNotificationCreateInput, error) { + settings := new(monitor.NotificationSettingOneCloud) + if err := input.Settings.Unmarshal(settings); err != nil { + return input, errors.Wrap(err, "unmarshal setting") + } + if settings.Channel == "" { + return input, httperrors.NewInputParameterError("channel is empty") + } + ids := make([]string, 0) + for _, uid := range settings.UserIds { + obj, err := db.UserCacheManager.FetchUserByIdOrName(context.TODO(), uid) + if err != nil { + return input, errors.Wrapf(err, "fetch setting uid %s", uid) + } + ids = append(ids, obj.GetId()) + } + settings.UserIds = ids + input.Settings = jsonutils.Marshal(settings) + return input, nil + }, + }) +} + +// OneCloudNotifier is responsible for sending +// alert notifications over onecloud notify service. +type OneCloudNotifier struct { + NotifierBase + Setting *monitor.NotificationSettingOneCloud + session *mcclient.ClientSession +} + +func newOneCloudNotifier(config alerting.NotificationConfig) (alerting.Notifier, error) { + setting := new(monitor.NotificationSettingOneCloud) + if err := config.Settings.Unmarshal(setting); err != nil { + return nil, errors.Wrapf(err, "unmarshal onecloud setting %s", config.Settings) + } + return &OneCloudNotifier{ + NotifierBase: NewNotifierBase(config), + Setting: setting, + session: auth.GetAdminSession(context.Background(), options.Options.Region, ""), + }, nil +} + +func GetNotifyTemplateConfig(ctx *alerting.EvalContext) monitor.NotificationTemplateConfig { + priority := notify.NotifyPriorityNormal + level := "普通" + switch ctx.Rule.Level { + case "", "normal": + priority = notify.NotifyPriorityNormal + case "important": + priority = notify.NotifyPriorityImportant + level = "重要" + case "fatal", "critical": + priority = notify.NotifyPriorityCritical + level = "严重" + } + topic := fmt.Sprintf("[%s]", level) + + isRecovery := false + if ctx.Rule.State == monitor.AlertStateOK { + isRecovery = true + topic = fmt.Sprintf("%s %s 告警已恢复", topic, ctx.GetRuleTitle()) + } else { + topic = fmt.Sprintf("%s %s 发生告警", topic, ctx.GetRuleTitle()) + } + config := ctx.GetNotificationTemplateConfig() + config.Title = topic + config.Level = level + config.Priority = string(priority) + config.IsRecovery = isRecovery + return config +} + +// Notify sends the alert notification. +func (oc *OneCloudNotifier) Notify(ctx *alerting.EvalContext) error { + log.Infof("Sending alert notification %s to onecloud", ctx.GetRuleTitle()) + config := GetNotifyTemplateConfig(ctx) + contentConfig := oc.buildContent(config) + content, err := contentConfig.GenerateMarkdown() + if err != nil { + return errors.Wrap(err, "build content") + } + + msg := notify.SNotifyMessage{ + Uid: oc.Setting.UserIds, + ContactType: notify.TNotifyChannel(oc.Setting.Channel), + Topic: config.Title, + Priority: notify.TNotifyPriority(config.Priority), + Msg: content, + } + + log.Errorf("---send msg: %s", jsonutils.Marshal(msg)) + return notify.Notifications.Send(oc.session, msg) +} + +func (oc *OneCloudNotifier) buildContent(config monitor.NotificationTemplateConfig) *templates.TemplateConfig { + return templates.NewTemplateConfig(config) +} diff --git a/pkg/monitor/alerting/notifiers/templates/doc.go b/pkg/monitor/alerting/notifiers/templates/doc.go new file mode 100644 index 0000000000..cb2af4cc1b --- /dev/null +++ b/pkg/monitor/alerting/notifiers/templates/doc.go @@ -0,0 +1 @@ +package templates // import "yunion.io/x/onecloud/pkg/monitor/alerting/notifiers/templates" diff --git a/pkg/monitor/alerting/notifiers/templates/template.go b/pkg/monitor/alerting/notifiers/templates/template.go new file mode 100644 index 0000000000..4a3db78e34 --- /dev/null +++ b/pkg/monitor/alerting/notifiers/templates/template.go @@ -0,0 +1,54 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package templates + +import "yunion.io/x/onecloud/pkg/apis/monitor" + +type TemplateConfig struct { + monitor.NotificationTemplateConfig +} + +func NewTemplateConfig(c monitor.NotificationTemplateConfig) *TemplateConfig { + return &TemplateConfig{ + NotificationTemplateConfig: c, + } +} + +const MarkdownTemplate = ` +## {{.Title}} + +- 时间: {{.StartTime}} +- 级别: {{.Level}} + +{{range .Matches}} + +- 指标: {{.Metric}} +- 当前值: {{.Value}} + +### 触发条件: + +> {{.Condition}} + +### 标签 + +{{range $key, $value := .Tags}} +> {{ $key }}: {{ $value}} +{{end}} +{{end}} +` + +func (c TemplateConfig) GenerateMarkdown() (string, error) { + return CompileTEmplateFromMap(MarkdownTemplate, c) +} diff --git a/pkg/monitor/alerting/notifiers/templates/util.go b/pkg/monitor/alerting/notifiers/templates/util.go new file mode 100644 index 0000000000..07fab9814a --- /dev/null +++ b/pkg/monitor/alerting/notifiers/templates/util.go @@ -0,0 +1,29 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package templates + +import ( + "bytes" + "text/template" +) + +func CompileTEmplateFromMap(tmplt string, configMap interface{}) (string, error) { + out := new(bytes.Buffer) + t := template.Must(template.New("commpiled_template").Parse(tmplt)) + if err := t.Execute(out, configMap); err != nil { + return "", err + } + return out.String(), nil +} diff --git a/pkg/monitor/alerting/notifiers/util.go b/pkg/monitor/alerting/notifiers/util.go new file mode 100644 index 0000000000..896316ffd4 --- /dev/null +++ b/pkg/monitor/alerting/notifiers/util.go @@ -0,0 +1,130 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package notifiers + +import ( + "bytes" + "context" + "crypto/tls" + "encoding/base64" + "fmt" + "io" + "io/ioutil" + "net" + "net/http" + "strings" + "time" + + "github.com/moul/http2curl" + "golang.org/x/net/context/ctxhttp" + + "yunion.io/x/log" + + "yunion.io/x/onecloud/pkg/apis/monitor" +) + +// GetBasicAuthHeader returns a base64 encoded string from user and password. +func GetBasicAuthHeader(user string, password string) string { + var userAndPass = user + ":" + password + return "Basic " + base64.StdEncoding.EncodeToString([]byte(userAndPass)) +} + +// DecodeBasicAuthHeader decodes user and password from a basic auth header. +func DecodeBasicAuthHeader(header string) (string, string, error) { + var code string + parts := strings.SplitN(header, " ", 2) + if len(parts) == 2 && parts[0] == "Basic" { + code = parts[1] + } + + decoded, err := base64.StdEncoding.DecodeString(code) + if err != nil { + return "", "", err + } + + userAndPass := strings.SplitN(string(decoded), ":", 2) + if len(userAndPass) != 2 { + return "", "", fmt.Errorf("Invalid basic auth header") + } + + return userAndPass[0], userAndPass[1], nil +} + +var netTransport = &http.Transport{ + TLSClientConfig: &tls.Config{ + Renegotiation: tls.RenegotiateFreelyAsClient, + }, + Proxy: http.ProxyFromEnvironment, + Dial: (&net.Dialer{ + Timeout: 30 * time.Second, + }).Dial, + TLSHandshakeTimeout: 5 * time.Second, +} +var netClient = &http.Client{ + Timeout: time.Second * 30, + Transport: netTransport, +} + +func SendWebRequestSync(ctx context.Context, webhook *monitor.SendWebhookSync) error { + if webhook.HttpMethod == "" { + webhook.HttpMethod = http.MethodPost + } + + request, err := http.NewRequest(webhook.HttpMethod, webhook.Url, bytes.NewReader([]byte(webhook.Body))) + if err != nil { + return err + } + + if webhook.ContentType == "" { + webhook.ContentType = "application/json" + } + + request.Header.Add("Content-Type", webhook.ContentType) + request.Header.Add("User-Agent", "OneCloud Monitor") + + if webhook.User != "" && webhook.Password != "" { + request.Header.Add("Authorization", GetBasicAuthHeader(webhook.User, webhook.Password)) + } + + for k, v := range webhook.HttpHeader { + request.Header.Set(k, v) + } + + curlCmd, _ := http2curl.GetCurlCommand(request) + log.Debugf("webhook curl: %s", curlCmd) + + resp, err := ctxhttp.Do(ctx, netClient, request) + if err != nil { + return err + } + + defer resp.Body.Close() + + if resp.StatusCode/100 == 2 { + // flushing the body enables the transport to reuse the same connection + if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil { + log.Errorf("Failed to copy resp.Body to ioutil.Discard: %v", err) + } + return nil + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + log.Errorf("Webhook failed statuscode: %s, body: %s", resp.Status, string(body)) + return fmt.Errorf("Webhook response status %v", resp.Status) +} diff --git a/pkg/monitor/alerting/reader.go b/pkg/monitor/alerting/reader.go new file mode 100644 index 0000000000..c7e2480acc --- /dev/null +++ b/pkg/monitor/alerting/reader.go @@ -0,0 +1,54 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "sync" + + "yunion.io/x/log" + + "yunion.io/x/onecloud/pkg/monitor/models" +) + +type ruleReader interface { + fetch() []*Rule +} + +type defaultRuleReader struct { + sync.RWMutex +} + +func newRuleReader() *defaultRuleReader { + ruleReader := &defaultRuleReader{} + return ruleReader +} + +func (arr *defaultRuleReader) fetch() []*Rule { + alerts, err := models.AlertManager.FetchAllAlerts() + if err != nil { + log.Errorf("fetch alerts from db: %v", err) + return nil + } + res := make([]*Rule, 0) + for _, alert := range alerts { + obj, err := NewRuleFromDBAlert(&alert) + if err != nil { + log.Errorf("Build alert rule %s from db error: %v", alert.GetId(), err) + continue + } + res = append(res, obj) + } + return res +} diff --git a/pkg/monitor/alerting/result_handler.go b/pkg/monitor/alerting/result_handler.go new file mode 100644 index 0000000000..0f89c2591d --- /dev/null +++ b/pkg/monitor/alerting/result_handler.go @@ -0,0 +1,84 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "time" + + "yunion.io/x/jsonutils" + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/monitor/models" +) + +type resultHandler interface { + handle(ctx *EvalContext) error +} + +type defaultResultHandler struct { + notifier *notificationService +} + +func newResultHandler() *defaultResultHandler { + return &defaultResultHandler{ + notifier: newNotificationService(), + } +} + +func (handler *defaultResultHandler) handle(evalCtx *EvalContext) error { + execErr := "" + annotationData := jsonutils.NewDict() + if len(evalCtx.EvalMatches) > 0 { + annotationData.Add(jsonutils.Marshal(evalCtx.EvalMatches), "evalMatches") + } + + if evalCtx.Error != nil { + execErr = evalCtx.Error.Error() + annotationData.Add(jsonutils.NewString(evalCtx.Error.Error()), "error") + } else if evalCtx.NoDataFound { + annotationData.Add(jsonutils.JSONTrue, "noData") + } + if evalCtx.shouldUpdateAlertState() { + log.Infof("New state change, alertId %s, prevState %s, newState %s", evalCtx.Rule.Id, evalCtx.PrevAlertState, evalCtx.Rule.State) + alert, err := models.AlertManager.GetAlert(evalCtx.Rule.Id) + if err != nil { + log.Errorf("get alert %s error: %v", evalCtx.Rule.Id, err) + return errors.Wrapf(err, "result get alert %s", evalCtx.Rule.Id) + } + input := models.AlertSetStateInput{ + State: evalCtx.Rule.State, + ExecutionError: execErr, + EvalData: annotationData, + } + if err := alert.SetState(input); err != nil { + log.Errorf("Failed to set alert %s state: %v", evalCtx.Rule.Name, err) + } else { + // StateChanges is used for de duping alert notifications + // when two servers are raising. This makes sure that the server + // with the last state change always sends a notification + evalCtx.Rule.StateChanges = alert.StateChanges + + // Update the last state change of the alert rule in memory + evalCtx.Rule.LastStateChange = time.Now() + } + // TODO: save opslog + } + + if err := handler.notifier.SendIfNeeded(evalCtx); err != nil { + return err + } + return nil +} diff --git a/pkg/monitor/alerting/rule.go b/pkg/monitor/alerting/rule.go new file mode 100644 index 0000000000..4d43d663e3 --- /dev/null +++ b/pkg/monitor/alerting/rule.go @@ -0,0 +1,155 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "regexp" + "strconv" + "time" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/monitor/models" + "yunion.io/x/onecloud/pkg/monitor/validators" +) + +var ( + // ErrFrequencyCannotBeZeroOrLess frequency cannot be below zero + ErrFrequencyCannotBeZeroOrLess = errors.Error(`"evaluate every" cannot be zero or below`) + + // ErrFrequencyCouldNotBeParsed frequency cannot be parsed + ErrFrequencyCouldNotBeParsed = errors.Error(`"evaluate every" field could not be parsed`) +) + +// Rule is the in-memory version of an alert rule. +type Rule struct { + Id string + Frequency int64 + Title string + Name string + Message string + LastStateChange time.Time + For time.Duration + NoDataState monitor.NoDataOption + ExecutionErrorState monitor.ExecutionErrorOption + State monitor.AlertStateType + Conditions []Condition + Notifications []string + // AlertRuleTags []*models.AlertRuleTag + Level string + + StateChanges int +} + +var ( + valueFormatRegex = regexp.MustCompile(`^\d+`) + unitFormatRegex = regexp.MustCompile(`\w{1}$`) +) + +var unitMultiplier = map[string]int{ + "s": 1, + "m": 60, + "h": 3600, + "d": 86400, +} + +func getTimeDurationStringToSeconds(str string) (int64, error) { + multiplier := 1 + + matches := valueFormatRegex.FindAllString(str, 1) + + if len(matches) <= 0 { + return 0, ErrFrequencyCouldNotBeParsed + } + + value, err := strconv.Atoi(matches[0]) + if err != nil { + return 0, err + } + + if value == 0 { + return 0, ErrFrequencyCannotBeZeroOrLess + } + + unit := unitFormatRegex.FindAllString(str, 1)[0] + + if val, ok := unitMultiplier[unit]; ok { + multiplier = val + } + + return int64(value * multiplier), nil +} + +// NewRuleFromDBAlert maps an db version of +// alert to an in-memory version +func NewRuleFromDBAlert(ruleDef *models.SAlert) (*Rule, error) { + model := &Rule{} + model.Id = ruleDef.Id + model.Title = ruleDef.GetTitle() + model.Name = ruleDef.Name + model.Message = ruleDef.Message + model.State = monitor.AlertStateType(ruleDef.State) + model.LastStateChange = ruleDef.LastStateChange + model.For = time.Duration(ruleDef.For) + model.NoDataState = monitor.NoDataOption(ruleDef.NoDataState) + model.ExecutionErrorState = monitor.ExecutionErrorOption(ruleDef.ExecutionErrorState) + model.StateChanges = ruleDef.StateChanges + + model.Frequency = ruleDef.Frequency + // frequency cannot be zero since that would not execute the alert rule. + // so we fallback to 60 seconds if `Frequency` is missing + if model.Frequency == 0 { + model.Frequency = 60 + } + + settings, err := ruleDef.GetSettings() + if err != nil { + return nil, err + } + + model.Level = settings.Level + model.Notifications = settings.Notifications + // model.AlertRuleTags = ruleDef.GetTagsFromSettings() + + for index, condition := range settings.Conditions { + condType := condition.Type + factory, exist := conditionFactories[condType] + if !exist { + return nil, errors.Wrapf(validators.ErrAlertConditionUnknown, "condition type %s", condType) + } + queryCond, err := factory(&condition, index) + if err != nil { + return nil, errors.Wrapf(err, "construct query condition %s", jsonutils.Marshal(condition)) + } + model.Conditions = append(model.Conditions, queryCond) + } + + if len(model.Conditions) == 0 { + return nil, validators.ErrAlertConditionEmpty + } + return model, nil +} + +// ConditionFactory is the function signature for creating `Conditions` +type ConditionFactory func(model *monitor.AlertCondition, index int) (Condition, error) + +var conditionFactories = make(map[string]ConditionFactory) + +// RegisterCondition adds support for alerting conditions. +func RegisterCondition(typeName string, factory ConditionFactory) { + conditionFactories[typeName] = factory +} diff --git a/pkg/monitor/alerting/scheduler.go b/pkg/monitor/alerting/scheduler.go new file mode 100644 index 0000000000..f713bfeadc --- /dev/null +++ b/pkg/monitor/alerting/scheduler.go @@ -0,0 +1,98 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "math" + "time" + + "yunion.io/x/log" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/monitor/options" +) + +type schedulerImpl struct { + jobs map[string]*Job +} + +func newScheduler() scheduler { + return &schedulerImpl{ + jobs: make(map[string]*Job), + } +} + +func (s *schedulerImpl) Update(rules []*Rule) { + log.Debugf("Scheduling update, rule count %d", len(rules)) + + jobs := make(map[string]*Job) + + for i, rule := range rules { + var job *Job + if s.jobs[rule.Id] != nil { + job = s.jobs[rule.Id] + } else { + job = &Job{} + job.SetRunning(false) + } + + job.Rule = rule + + offset := ((rule.Frequency * 1000) / int64(len(rules))) * int64(i) + job.Offset = int64(math.Floor(float64(offset) / 1000)) + if job.Offset == 0 { + // zero offset causes division with 0 panics + job.Offset = 1 + } + jobs[rule.Id] = job + } + + s.jobs = jobs +} + +func (s *schedulerImpl) Tick(tickTime time.Time, execQueue chan *Job) { + now := tickTime.Unix() + + for _, job := range s.jobs { + if job.GetRunning() || job.Rule.State == monitor.AlertStatePaused { + continue + } + + if job.OffsetWait && now%job.Offset == 0 { + job.OffsetWait = false + s.enqueue(job, execQueue) + continue + } + + // Check the job frequency against the minium interval required + interval := job.Rule.Frequency + if interval < options.Options.AlertingMinIntervalSeconds { + interval = options.Options.AlertingMinIntervalSeconds + } + + if now%interval == 0 { + if job.Offset > 0 { + job.OffsetWait = true + } else { + s.enqueue(job, execQueue) + } + } + } +} + +func (s *schedulerImpl) enqueue(job *Job, execQueue chan *Job) { + log.Debugf("Scheduler: putting job into exec queue, name %s:%s", job.Rule.Name, job.Rule.Id) + execQueue <- job +} diff --git a/pkg/monitor/alerting/ticker.go b/pkg/monitor/alerting/ticker.go new file mode 100644 index 0000000000..6b44a55c3e --- /dev/null +++ b/pkg/monitor/alerting/ticker.go @@ -0,0 +1,70 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alerting + +import ( + "time" + + "github.com/benbjohnson/clock" +) + +// Ticker is a ticker to power the alerting scheduler. it's like a time.Ticker, except: +// * it doesn't drop ticks for slow receivers, rather, it queues up. so that callers are in control to instrument what's going on. +// * it automatically ticks every second, which is the right thing in our current design +// * it ticks on second marks or very shortly after. this provides a predictable load pattern +// (this shouldn't cause too much load contention issues because the next steps in the pipeline just process at their own pace) +// * the timestamps are used to mark "last datapoint to query for" and as such, are a configurable amount of seconds in the past +// * because we want to allow: +// - a clean "resume where we left off" and "don't yield ticks we already did" +// - adjusting offset over time to compensate for storage backing up or getting fast and providing lower latency +// you specify a lastProcessed timestamp as well as an offset at creation, or runtime +type Ticker struct { + C chan time.Time + clock clock.Clock + last time.Time + offset time.Duration + newOffset chan time.Duration +} + +// NewTicker returns a ticker that ticks on second marks or very shortly after, and never drops ticks +func NewTicker(last time.Time, initialOffset time.Duration, c clock.Clock) *Ticker { + t := &Ticker{ + C: make(chan time.Time), + clock: c, + last: last, + offset: initialOffset, + newOffset: make(chan time.Duration), + } + go t.run() + return t +} + +func (t *Ticker) run() { + for { + next := t.last.Add(time.Duration(1) * time.Second) + diff := t.clock.Now().Add(-t.offset).Sub(next) + if diff >= 0 { + t.C <- next + t.last = next + continue + } + // tick is too young. try again when ... + select { + case <-t.clock.After(-diff): // ...it'll definitely be old enough + case offset := <-t.newOffset: // ...it might be old enough + t.offset = offset + } + } +} diff --git a/pkg/monitor/bus/bus.go b/pkg/monitor/bus/bus.go new file mode 100644 index 0000000000..e4386bed68 --- /dev/null +++ b/pkg/monitor/bus/bus.go @@ -0,0 +1,238 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package bus + +import ( + "context" + "reflect" + + "yunion.io/x/pkg/errors" +) + +// HandlerFunc defines a handler function interface. +type HandlerFunc interface{} + +// CtxHandlerFunc defines a context handler function. +type CtxHandlerFunc func() + +// Msg defines a message interface. +type Msg interface{} + +// ErrHandlerNotFound defines an error if a handler is not found +var ErrHandlerNotFound = errors.Error("handler not found") + +// TransactionManager defines a transaction interface +type TransactionManager interface { + InTransaction(ctx context.Context, fn func(ctx context.Context) error) error +} + +// Bus type defines the bus interface structure +type Bus interface { + Dispatch(msg Msg) error + DispatchCtx(ctx context.Context, msg Msg) error + Publish(msg Msg) error + + // InTransaction starts a transaction and store it in the context. + // The caller can then pass a function with multiple DispatchCtx calls that + // all will be executed in the same transaction. InTransaction will rollback if the + // callback returns an error. + InTransaction(ctx context.Context, fn func(ctx context.Context) error) error + + AddHandler(handler HandlerFunc) + AddHandlerCtx(handler HandlerFunc) + AddEventListener(handler HandlerFunc) + + // SetTransactionManager allows the user to replace the internal + // noop TransactionManager that is responsible for managing + // transactions in `InTransaction` + SetTransactionManager(tm TransactionManager) +} + +type noopTransactionManager struct{} + +func (*noopTransactionManager) InTransaction(ctx context.Context, fn func(ctx context.Context) error) error { + return fn(ctx) +} + +// InProcBus defines the bus structure +type InProcBus struct { + handlers map[string]HandlerFunc + handlersWithCtx map[string]HandlerFunc + listeners map[string][]HandlerFunc + txMng TransactionManager +} + +// InTransaction defines an in transaction function +func (b *InProcBus) InTransaction(ctx context.Context, fn func(ctx context.Context) error) error { + return b.txMng.InTransaction(ctx, fn) +} + +// temp stuff, not sure how to handle bus instance, and init yet +var globalBus = New() + +// New initialize the bus +func New() Bus { + bus := &InProcBus{} + bus.handlers = make(map[string]HandlerFunc) + bus.handlersWithCtx = make(map[string]HandlerFunc) + bus.listeners = make(map[string][]HandlerFunc) + bus.txMng = &noopTransactionManager{} + + return bus +} + +// Want to get rid of global bus +func GetBus() Bus { + return globalBus +} + +// SetTransactionManager function assign a transaction manager to the bus. +func (b *InProcBus) SetTransactionManager(tm TransactionManager) { + b.txMng = tm +} + +// DispatchCtx function dispatch a message to the bus context. +func (b *InProcBus) DispatchCtx(ctx context.Context, msg Msg) error { + var msgName = reflect.TypeOf(msg).Elem().Name() + + var handler = b.handlersWithCtx[msgName] + if handler == nil { + return ErrHandlerNotFound + } + + var params = []reflect.Value{} + params = append(params, reflect.ValueOf(ctx)) + params = append(params, reflect.ValueOf(msg)) + + ret := reflect.ValueOf(handler).Call(params) + err := ret[0].Interface() + if err == nil { + return nil + } + return err.(error) +} + +// Dispatch function dispatch a message to the bus. +func (b *InProcBus) Dispatch(msg Msg) error { + var msgName = reflect.TypeOf(msg).Elem().Name() + + var handler = b.handlersWithCtx[msgName] + withCtx := true + + if handler == nil { + withCtx = false + handler = b.handlers[msgName] + } + + if handler == nil { + return ErrHandlerNotFound + } + + var params = []reflect.Value{} + if withCtx { + params = append(params, reflect.ValueOf(context.Background())) + } + params = append(params, reflect.ValueOf(msg)) + + ret := reflect.ValueOf(handler).Call(params) + err := ret[0].Interface() + if err == nil { + return nil + } + return err.(error) +} + +// Publish function publish a message to the bus listener. +func (b *InProcBus) Publish(msg Msg) error { + var msgName = reflect.TypeOf(msg).Elem().Name() + var listeners = b.listeners[msgName] + + var params = make([]reflect.Value, 1) + params[0] = reflect.ValueOf(msg) + + for _, listenerHandler := range listeners { + ret := reflect.ValueOf(listenerHandler).Call(params) + err := ret[0].Interface() + if err != nil { + return err.(error) + } + } + + return nil +} + +func (b *InProcBus) AddHandler(handler HandlerFunc) { + handlerType := reflect.TypeOf(handler) + queryTypeName := handlerType.In(0).Elem().Name() + b.handlers[queryTypeName] = handler +} + +func (b *InProcBus) AddHandlerCtx(handler HandlerFunc) { + handlerType := reflect.TypeOf(handler) + queryTypeName := handlerType.In(1).Elem().Name() + b.handlersWithCtx[queryTypeName] = handler +} + +func (b *InProcBus) AddEventListener(handler HandlerFunc) { + handlerType := reflect.TypeOf(handler) + eventName := handlerType.In(0).Elem().Name() + _, exists := b.listeners[eventName] + if !exists { + b.listeners[eventName] = make([]HandlerFunc, 0) + } + b.listeners[eventName] = append(b.listeners[eventName], handler) +} + +// AddHandler attach a handler function to the global bus +// Package level function +func AddHandler(implName string, handler HandlerFunc) { + globalBus.AddHandler(handler) +} + +// AddHandlerCtx attach a handler function to the global bus context +// Package level functions +func AddHandlerCtx(implName string, handler HandlerFunc) { + globalBus.AddHandlerCtx(handler) +} + +// AddEventListener attach a handler function to the event listener +// Package level functions +func AddEventListener(handler HandlerFunc) { + globalBus.AddEventListener(handler) +} + +func Dispatch(msg Msg) error { + return globalBus.Dispatch(msg) +} + +func DispatchCtx(ctx context.Context, msg Msg) error { + return globalBus.DispatchCtx(ctx, msg) +} + +func Publish(msg Msg) error { + return globalBus.Publish(msg) +} + +// InTransaction starts a transaction and store it in the context. +// The caller can then pass a function with multiple DispatchCtx calls that +// all will be executed in the same transaction. InTransaction will rollback if the +// callback returns an error. +func InTransaction(ctx context.Context, fn func(ctx context.Context) error) error { + return globalBus.InTransaction(ctx, fn) +} + +func ClearBusHandlers() { + globalBus = New() +} diff --git a/pkg/monitor/bus/bus_test.go b/pkg/monitor/bus/bus_test.go new file mode 100644 index 0000000000..93f6f2c423 --- /dev/null +++ b/pkg/monitor/bus/bus_test.go @@ -0,0 +1,144 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package bus + +import ( + "context" + "errors" + "testing" + + "github.com/stretchr/testify/require" +) + +type testQuery struct { + ID int64 + Resp string +} + +func TestDispatch(t *testing.T) { + bus := New() + + var invoked bool + + bus.AddHandler(func(query *testQuery) error { + invoked = true + return nil + }) + + err := bus.Dispatch(&testQuery{}) + require.NoError(t, err) + + require.True(t, invoked, "expected handler to be called") +} + +func TestDispatch_NoRegisteredHandler(t *testing.T) { + bus := New() + + err := bus.Dispatch(&testQuery{}) + require.Equal(t, err, ErrHandlerNotFound, + "expected bus to return HandlerNotFound since no handler is registered") +} + +func TestDispatch_ContextHandler(t *testing.T) { + bus := New() + + var invoked bool + + bus.AddHandlerCtx(func(ctx context.Context, query *testQuery) error { + invoked = true + return nil + }) + + err := bus.Dispatch(&testQuery{}) + require.NoError(t, err) + + require.True(t, invoked, "expected handler to be called") +} + +func TestDispatchCtx(t *testing.T) { + bus := New() + + var invoked bool + + bus.AddHandlerCtx(func(ctx context.Context, query *testQuery) error { + invoked = true + return nil + }) + + err := bus.DispatchCtx(context.Background(), &testQuery{}) + require.NoError(t, err) + + require.True(t, invoked, "expected handler to be called") +} + +func TestDispatchCtx_NoRegisteredHandler(t *testing.T) { + bus := New() + + err := bus.DispatchCtx(context.Background(), &testQuery{}) + require.Equal(t, err, ErrHandlerNotFound, + "expected bus to return HandlerNotFound since no handler is registered") +} + +func TestQuery(t *testing.T) { + bus := New() + + want := "hello from handler" + + bus.AddHandler(func(q *testQuery) error { + q.Resp = want + return nil + }) + + q := &testQuery{} + + err := bus.Dispatch(q) + require.NoError(t, err, "unable to dispatch query") + + require.Equal(t, want, q.Resp) +} + +func TestQuery_HandlerReturnsError(t *testing.T) { + bus := New() + + bus.AddHandler(func(query *testQuery) error { + return errors.New("handler error") + }) + + err := bus.Dispatch(&testQuery{}) + require.Error(t, err, "expected error but got none") +} + +func TestEvent(t *testing.T) { + bus := New() + + var invoked bool + + bus.AddEventListener(func(query *testQuery) error { + invoked = true + return nil + }) + + err := bus.Publish(&testQuery{}) + require.NoError(t, err, "unable to publish event") + + require.True(t, invoked) +} + +func TestEvent_NoRegisteredListener(t *testing.T) { + bus := New() + + err := bus.Publish(&testQuery{}) + require.NoError(t, err, "unable to publish event") +} diff --git a/pkg/monitor/bus/doc.go b/pkg/monitor/bus/doc.go new file mode 100644 index 0000000000..7414593d11 --- /dev/null +++ b/pkg/monitor/bus/doc.go @@ -0,0 +1 @@ +package bus // import "yunion.io/x/onecloud/pkg/monitor/bus" diff --git a/pkg/monitor/expressions/doc.go b/pkg/monitor/expressions/doc.go new file mode 100644 index 0000000000..2a606b9cda --- /dev/null +++ b/pkg/monitor/expressions/doc.go @@ -0,0 +1 @@ +package expressions // import "yunion.io/x/onecloud/pkg/monitor/expressions" diff --git a/pkg/monitor/expressions/expressions.go b/pkg/monitor/expressions/expressions.go new file mode 100644 index 0000000000..7221d50670 --- /dev/null +++ b/pkg/monitor/expressions/expressions.go @@ -0,0 +1,80 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package expressions + +type PrimitveType string + +const ( + Bool PrimitveType = "Bool" + DateTime PrimitveType = "DateTime" + Double PrimitveType = "Double" + String PrimitveType = "String" + Null PrimitveType = "NULL" +) + +/*type ConstExp struct { + Bool bool + DateTime DateTime + Double Double +}*/ + +type ConstExp interface{} + +type PropertyExp struct { + Property string `json:"property"` + Type PrimitveType `json:"type"` +} + +type PrimitiveObject struct { + PropertyExp + ConstExp +} + +type OperatorExp struct { + Left *PropertyExp `json:"left"` + Right *PrimitiveObject `json:"right"` +} + +type LogicalExp struct { + EQ *OperatorExp `json:"eq"` + IN *OperatorExp `json:"in"` + LT *OperatorExp `json:"lt"` + GT *OperatorExp `json:"gt"` + AND []*LogicalExp `json:"and"` + OR []*LogicalExp `json:"or"` + NOT *LogicalExp `json:"not"` +} + +type ArithmeticExp struct { + ADD *OperatorExp `json:"add"` + SUB *OperatorExp `json:"sub"` +} + +type FilterExp struct { + LogicalExp +} + +type AlignerExp struct { + Input *PropertyExp `json:"input"` +} + +type MeasureExp struct { + Mean *AlignerExp `json:"mean"` + Min *AlignerExp `json:"min"` +} + +type AggregateExp struct { + MeasureExps []MeasureExp +} diff --git a/pkg/monitor/models/alert.go b/pkg/monitor/models/alert.go new file mode 100644 index 0000000000..7f452962c8 --- /dev/null +++ b/pkg/monitor/models/alert.go @@ -0,0 +1,308 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package models + +import ( + "context" + "database/sql" + "strings" + "time" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" + "yunion.io/x/sqlchemy" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/monitor/validators" + "yunion.io/x/onecloud/pkg/util/rbacutils" +) + +const ( + AlertMetadataTitle = "alert_title" +) + +var ( + AlertManager *SAlertManager +) + +func init() { + AlertManager = NewAlertManager(SAlert{}, "alert", "alerts") +} + +type SAlertManager struct { + db.SVirtualResourceBaseManager +} + +func NewAlertManager(dt interface{}, keyword, keywordPlural string) *SAlertManager { + man := &SAlertManager{ + SVirtualResourceBaseManager: db.NewVirtualResourceBaseManager( + dt, + "alerts_tbl", + keyword, + keywordPlural), + } + man.SetVirtualObject(man) + return man +} + +func (man *SAlertManager) FetchAllAlerts() ([]SAlert, error) { + objs := make([]SAlert, 0) + q := man.Query() + err := db.FetchModelObjects(man, q, &objs) + if err != nil && err != sql.ErrNoRows { + return nil, errors.Wrap(err, "db.FetchModelObjects") + } + return objs, nil +} + +type SAlert struct { + db.SVirtualResourceBase + + Frequency int64 `nullable:"false" list:"user" create:"required" update:"user"` + Settings jsonutils.JSONObject `nullable:"false" list:"user" create:"required" update:"user"` + Enabled bool `nullable:"false" default:"false" list:"user" create:"optional"` + + Message string `charset:"utf8" list:"user" update:"user"` + State string `width:"36" charset:"ascii" list:"user"` + // Silenced bool + ExecutionError string `charset:"utf8" list:"user"` + For int64 `nullable:"false" list:"user"` + + EvalData jsonutils.JSONObject `list:"user"` + LastStateChange time.Time `json:"last_state_change" list:"user"` + StateChanges int `default:"0" nullable:"false" list:"user" json:"state_changes"` + + NoDataState string `charset:"utf8" list:"user"` + ExecutionErrorState string `charset:"utf8" list:"user"` +} + +func (alert *SAlert) IsEnable() bool { + return alert.Enabled +} + +func (alert *SAlert) SetEnable() error { + alert.Enabled = true + return nil +} + +func (alert *SAlert) SetDisable() error { + alert.Enabled = false + return nil +} + +func (alert *SAlert) SetTitle(ctx context.Context, t string) error { + return alert.SetMetadata(ctx, AlertMetadataTitle, t, nil) +} + +func (alert *SAlert) GetTitle() string { + return alert.GetMetadata(AlertMetadataTitle, nil) +} + +func (alert *SAlert) ShouldUpdateState(newState monitor.AlertStateType) bool { + return monitor.AlertStateType(alert.State) != newState +} + +func (alert *SAlert) GetSettings() (*monitor.AlertSetting, error) { + setting := new(monitor.AlertSetting) + if alert.Settings == nil { + return setting, nil + } + if err := alert.Settings.Unmarshal(setting); err != nil { + return nil, errors.Wrapf(err, "alert %s unmarshal", alert.GetId()) + } + return setting, nil +} + +type AlertRuleTags map[string]AlertRuleTag + +type AlertRuleTag struct { + Key string + Value string +} + +func setAlertDefaultSetting(setting *monitor.AlertSetting, dsId string) *monitor.AlertSetting { + for idx, cond := range setting.Conditions { + cond = setAlertDefaultCondition(cond, dsId) + setting.Conditions[idx] = cond + } + return setting +} + +func setAlertDefaultCreateData(data monitor.AlertCreateInput, dsId string) monitor.AlertCreateInput { + setting := setAlertDefaultSetting(&data.Settings, dsId) + data.Settings = *setting + enable := true + if data.Enabled == nil { + data.Enabled = &enable + } + return data +} + +func setAlertDefaultCondition(cond monitor.AlertCondition, dsId string) monitor.AlertCondition { + if cond.Type == "" { + cond.Type = "query" + } + if cond.Query.To == "" { + cond.Query.To = "now" + } + if cond.Operator == "" { + cond.Operator = "and" + } + if cond.Query.DataSourceId == "" { + cond.Query.DataSourceId = dsId + } + return cond +} + +func (man *SAlertManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, _ jsonutils.JSONObject, data monitor.AlertCreateInput) (monitor.AlertCreateInput, error) { + ds, err := DataSourceManager.GetDefaultSource() + if err != nil { + return data, errors.Wrap(err, "get default data source") + } + data = setAlertDefaultCreateData(data, ds.GetId()) + if err := validators.ValidateAlertCreateInput(data); err != nil { + return data, err + } + return data, nil +} + +func (man *SAlertManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, input monitor.AlertListInput) (*sqlchemy.SQuery, error) { + q, err := man.SVirtualResourceBaseManager.ListItemFilter(ctx, q, userCred, input.VirtualResourceListInput) + if err != nil { + return nil, err + } + q, err = db.ListEnableItemFilter(q, input.Enabled) + if err != nil { + return nil, err + } + return q, nil +} + +func (man *SAlertManager) GetAlert(id string) (*SAlert, error) { + obj, err := man.FetchById(id) + if err != nil { + if errors.Cause(err) == sql.ErrNoRows { + return nil, nil + } + return nil, err + } + return obj.(*SAlert), nil +} + +func GetMeasurementField(metric string) (string, string, error) { + parts := strings.Split(metric, ".") + if len(parts) != 2 { + return "", "", httperrors.NewInputParameterError("metric %s is invalid format, usage .", metric) + } + measurement, field := parts[0], parts[1] + return measurement, field, nil +} + +func IsQuerySelectHasField(selects monitor.MetricQuerySelect, field string) bool { + for _, s := range selects { + if s.Type == "field" && len(s.Params) == 1 { + if s.Params[0] == field { + return true + } + } + } + return false +} + +func (man *SAlertManager) CustomizeFilterList( + ctx context.Context, q *sqlchemy.SQuery, + userCred mcclient.TokenCredential, query jsonutils.JSONObject) ( + *db.CustomizeListFilters, error) { + filters := db.NewCustomizeListFilters() + return filters, nil +} + +func (alert *SAlert) CustomizeCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error { + alert.LastStateChange = time.Now() + alert.State = string(monitor.AlertStateUnknown) + return alert.SVirtualResourceBase.CustomizeCreate(ctx, userCred, ownerId, query, data) +} + +func (alert *SAlert) AllowPerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool { + return db.AllowPerformEnable(alert, rbacutils.ScopeProject, userCred) +} + +func (alert *SAlert) PerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { + return db.PerformEnable(alert, userCred) +} + +func (alert *SAlert) AllowPerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool { + return db.AllowPerformDisable(alert, rbacutils.ScopeProject, userCred) +} + +func (alert *SAlert) PerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { + return db.PerformDisable(alert, userCred) +} + +func (alert *SAlert) GetNotifications() ([]SAlertNotification, error) { + settings, err := alert.GetSettings() + if err != nil { + return nil, errors.Wrap(err, "get settings") + } + nIds := settings.Notifications + notis, err := AlertNotificationManager.GetNotifications(nIds) + if err != nil { + if errors.Cause(err) == sql.ErrNoRows { + return nil, nil + } + return nil, err + } + return notis, nil +} + +const ( + ErrAlertChannotChangeStateOnPaused = errors.Error("Cannot change state on pause alert") +) + +type AlertSetStateInput struct { + State monitor.AlertStateType + EvalData jsonutils.JSONObject + ExecutionError string +} + +func (alert *SAlert) SetState(input AlertSetStateInput) error { + if alert.State == string(monitor.AlertStatePaused) { + return ErrAlertChannotChangeStateOnPaused + } + if alert.State == string(input.State) { + return nil + } + _, err := db.Update(alert, func() error { + alert.State = string(input.State) + alert.LastStateChange = time.Now() + alert.EvalData = input.EvalData + alert.ExecutionError = input.ExecutionError + alert.StateChanges = alert.StateChanges + 1 + return nil + }) + return err +} + +func (alert *SAlert) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input monitor.AlertUpdateInput) (*jsonutils.JSONDict, error) { + if input.Enabled == nil { + enable := true + input.Enabled = &enable + } + input.Settings = setAlertDefaultSetting(input.Settings, "") + return alert.SVirtualResourceBase.ValidateUpdateData(ctx, userCred, query, input.JSON(input)) +} diff --git a/pkg/monitor/models/datasource.go b/pkg/monitor/models/datasource.go new file mode 100644 index 0000000000..76ad9d2f44 --- /dev/null +++ b/pkg/monitor/models/datasource.go @@ -0,0 +1,169 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package models + +import ( + "context" + "database/sql" + "time" + + "golang.org/x/sync/errgroup" + + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/tristate" + "yunion.io/x/pkg/util/wait" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/mcclient/auth" + "yunion.io/x/onecloud/pkg/monitor/options" + "yunion.io/x/onecloud/pkg/monitor/registry" + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +var ( + DataSourceManager *SDataSourceManager +) + +const ( + DefaultDataSource = "default" +) + +const ( + ErrDataSourceDefaultNotFound = errors.Error("Default data source not found") +) + +func init() { + DataSourceManager = &SDataSourceManager{ + SStandaloneResourceBaseManager: db.NewStandaloneResourceBaseManager( + SDataSource{}, + "datasources_tbl", + "datasource", + "datasources", + ), + } + DataSourceManager.SetVirtualObject(DataSourceManager) + registry.RegisterService(DataSourceManager) +} + +type SDataSourceManager struct { + db.SStandaloneResourceBaseManager +} + +func (_ *SDataSourceManager) IsDisabled() bool { + return false +} + +func (_ *SDataSourceManager) Init() error { + return nil +} + +func (man *SDataSourceManager) Run(ctx context.Context) error { + errgrp, ctx := errgroup.WithContext(ctx) + errgrp.Go(func() error { return man.initDefaultDataSource(ctx) }) + return errgrp.Wait() +} + +func (man *SDataSourceManager) initDefaultDataSource(ctx context.Context) error { + region := options.Options.Region + initF := func() { + ds, err := man.GetDefaultSource() + if err != nil && err != ErrDataSourceDefaultNotFound { + log.Errorf("Get default datasource: %v", err) + return + } + if ds != nil { + return + } + s := auth.GetAdminSessionWithPublic(ctx, region, "") + if s == nil { + log.Errorf("get empty public session for region %s", region) + return + } + url, err := s.GetServiceURL("influxdb", auth.PublicEndpointType) + if err != nil { + log.Errorf("get influxdb public url: %v", err) + return + } + ds = &SDataSource{ + Type: monitor.DataSourceTypeInfluxdb, + Url: url, + } + ds.Name = DefaultDataSource + if err := man.TableSpec().Insert(ds); err != nil { + log.Errorf("insert default influxdb: %v", err) + } + } + wait.Forever(initF, 30*time.Second) + return nil +} + +func (man *SDataSourceManager) GetDefaultSource() (*SDataSource, error) { + obj, err := man.FetchByName(nil, DefaultDataSource) + if err != nil { + if err == sql.ErrNoRows { + return nil, ErrDataSourceDefaultNotFound + } else { + return nil, err + } + } + return obj.(*SDataSource), nil +} + +type SDataSource struct { + db.SStandaloneResourceBase + + Type string `nullable:"false" list:"user"` + Url string `nullable:"false" list:"user"` + User string `width:"64" charset:"utf8" nullable:"true"` + Password string `width:"64" charset:"utf8" nullable:"true"` + Database string `width:"64" charset:"utf8" nullable:"true"` + IsDefault tristate.TriState `nullable:"false" default:"false" create:"optional"` + /* + TimeInterval string + BasicAuth bool + BasicAuthUser string + BasicAuthPassword string + */ +} + +func (m *SDataSourceManager) GetSource(id string) (*SDataSource, error) { + ret, err := m.FetchById(id) + if err != nil { + return nil, err + } + return ret.(*SDataSource), nil +} + +func (ds *SDataSource) ToTSDBDataSource(db string) *tsdb.DataSource { + if db == "" { + db = ds.Database + } + return &tsdb.DataSource{ + Id: ds.GetId(), + Name: ds.GetName(), + Type: ds.Type, + Url: ds.Url, + User: ds.User, + Password: ds.Password, + Database: db, + Updated: ds.UpdatedAt, + /*BasicAuth: ds.BasicAuth, + BasicAuthUser: ds.BasicAuthUser, + BasicAuthPassword: ds.BasicAuthPassword, + TimeInterval: ds.TimeInterval,*/ + } +} diff --git a/pkg/monitor/models/doc.go b/pkg/monitor/models/doc.go new file mode 100644 index 0000000000..71c24a40de --- /dev/null +++ b/pkg/monitor/models/doc.go @@ -0,0 +1 @@ +package models // import "yunion.io/x/onecloud/pkg/monitor/models" diff --git a/pkg/monitor/models/initdb.go b/pkg/monitor/models/initdb.go new file mode 100644 index 0000000000..ba3d1a7b2d --- /dev/null +++ b/pkg/monitor/models/initdb.go @@ -0,0 +1,39 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package models + +import ( + "yunion.io/x/log" + + "yunion.io/x/onecloud/pkg/cloudcommon/db" +) + +func InitDB() error { + for _, manager := range []db.IModelManager{ + /* + * Important!!! + * initialization order matters, do not change the order + */ + DataSourceManager, + AlertManager, + } { + err := manager.InitializeData() + if err != nil { + log.Errorf("Manager %s initializeData fail %s", manager.Keyword(), err) + // return err skip error table + } + } + return nil +} diff --git a/pkg/monitor/models/meteralert.go b/pkg/monitor/models/meteralert.go new file mode 100644 index 0000000000..bb493c3bf1 --- /dev/null +++ b/pkg/monitor/models/meteralert.go @@ -0,0 +1,478 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package models + +import ( + "context" + "time" + + "yunion.io/x/jsonutils" + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + "yunion.io/x/sqlchemy" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/auth" + "yunion.io/x/onecloud/pkg/mcclient/modules" + "yunion.io/x/onecloud/pkg/monitor/options" +) + +const ( + MeterAlertMetadataType = "type" + MeterAlertMetadataProjectId = "project_id" + MeterAlertMetadataAccountId = "account_id" + MeterAlertMetadataProvider = "provider" +) + +var MeterAlertManager *SMeterAlertManager + +func init() { + MeterAlertManager = NewMeterAlertManager() +} + +type IMeterAlertDriver interface { + GetType() string + GetName() string + ToAlertCreateInput(input monitor.MeterAlertCreateInput, notificatoins []string, allAccountIds []string) monitor.AlertCreateInput +} + +type SMeterAlertManager struct { + SV1AlertManager + + drivers map[string]IMeterAlertDriver +} + +func NewMeterAlertManager() *SMeterAlertManager { + man := &SMeterAlertManager{ + SV1AlertManager: SV1AlertManager{ + *NewAlertManager(SMeterAlert{}, "meteralert", "meteralerts"), + }, + } + man.SetVirtualObject(man) + man.registerDriver(man.newDailyFeeDriver()) + man.registerDriver(man.newMonthFeeDriver()) + return man +} + +type SMeterAlert struct { + SV1Alert +} + +func (man *SMeterAlertManager) newDailyFeeDriver() IMeterAlertDriver { + return new(sMeterDailyFee) +} + +func (man *SMeterAlertManager) newMonthFeeDriver() IMeterAlertDriver { + return new(sMeterMonthFee) +} + +func (man *SMeterAlertManager) registerDriver(drv IMeterAlertDriver) { + if man.drivers == nil { + man.drivers = make(map[string]IMeterAlertDriver, 0) + } + man.drivers[drv.GetType()] = drv +} + +func (man *SMeterAlertManager) GetDriver(typ string) IMeterAlertDriver { + return man.drivers[typ] +} + +func (man *SMeterAlertManager) genName(ownerId mcclient.IIdentityProvider, hint string) (string, error) { + return db.GenerateName(man, ownerId, hint) +} + +func (man *SMeterAlertManager) getAllBillAccounts(ctx context.Context) ([]jsonutils.JSONObject, error) { + s := auth.GetAdminSession(ctx, options.Options.Region, "") + q := jsonutils.NewDict() + q.Add(jsonutils.NewString("accountList"), "account_id") + q.Add(jsonutils.NewInt(-1), "limit") + ret, err := modules.BillBalances.List(s, q) + if err != nil { + return nil, err + } + return ret.Data, nil +} + +func (man *SMeterAlertManager) getAllBillAccountIds(ctx context.Context) ([]string, error) { + objs, err := man.getAllBillAccounts(ctx) + if err != nil { + return nil, err + } + ids := make([]string, len(objs)) + for idx, obj := range objs { + id, err := obj.GetString("id") + if err != nil { + return nil, err + } + ids[idx] = id + } + return ids, nil +} + +func (man *SMeterAlertManager) ValidateCreateData( + ctx context.Context, userCred mcclient.TokenCredential, + ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, + data monitor.MeterAlertCreateInput) (*monitor.MeterAlertCreateInput, error) { + if data.Period == "" { + // default 30 minutes + data.Period = "30m" + } + if data.Window == "" { + // default 5 minutes + data.Window = "5m" + } + if _, err := time.ParseDuration(data.Period); err != nil { + return nil, httperrors.NewInputParameterError("Invalid period format: %s", data.Period) + } + if data.Recipients == "" { + return nil, httperrors.NewInputParameterError("recipients is empty") + } + notification, err := man.CreateNotification(ctx, userCred, data.Type, data.Channel, data.Recipients) + if err != nil { + return nil, errors.Wrap(err, "create notification") + } + + if data.ProjectId == "" { + return nil, httperrors.NewInputParameterError("project_id is empty") + } + + drv := man.GetDriver(data.Type) + if drv == nil { + return nil, httperrors.NewInputParameterError("not support type %q", data.Type) + } + name, err := man.genName(ownerId, drv.GetName()) + if err != nil { + return nil, err + } + allAccountIds := []string{} + if data.AccountId == "" { + allAccountIds, err = man.getAllBillAccountIds(ctx) + if err != nil { + return nil, err + } + } + alertInput := drv.ToAlertCreateInput( + data, []string{notification.GetId()}, + allAccountIds) + alertInput, err = AlertManager.ValidateCreateData(ctx, userCred, ownerId, query, alertInput) + if err != nil { + return nil, err + } + data.Name = name + data.AlertCreateInput = &alertInput + return &data, nil +} + +type sMeterDailyFee struct{} + +func (_ *sMeterDailyFee) GetType() string { + return monitor.MeterAlertTypeDailyResFee +} + +func (_ *sMeterDailyFee) GetName() string { + return "日消费" +} + +func (f *sMeterDailyFee) ToAlertCreateInput( + input monitor.MeterAlertCreateInput, + notifications []string, + allAccountIds []string, +) monitor.AlertCreateInput { + freq, _ := time.ParseDuration(input.Window) + ret := monitor.AlertCreateInput{ + Name: f.GetName(), + Frequency: int64(freq / time.Second), + Settings: GetMeterAlertSetting(input, notifications, + "account_daily_resfee", + "meter_db", allAccountIds, "sumDate"), + } + return ret +} + +type sMeterMonthFee struct{} + +func (_ *sMeterMonthFee) GetType() string { + return monitor.MeterAlertTypeMonthResFee +} + +func (_ *sMeterMonthFee) GetName() string { + return "月消费" +} + +func (f *sMeterMonthFee) ToAlertCreateInput( + input monitor.MeterAlertCreateInput, + notifications []string, + allAccountIds []string, +) monitor.AlertCreateInput { + freq, _ := time.ParseDuration(input.Window) + ret := monitor.AlertCreateInput{ + Name: f.GetName(), + Frequency: int64(freq / time.Second), + Settings: GetMeterAlertSetting(input, notifications, + "account_month_resfee", + "meter_db", allAccountIds, "sumMonth"), + } + return ret +} + +func GetMeterAlertSetting( + input monitor.MeterAlertCreateInput, + ns []string, + measurement string, + db string, + accountIds []string, + groupByStr string, +) monitor.AlertSetting { + q, reducer, eval := GetMeterAlertQuery(input, measurement, db, accountIds, groupByStr) + return monitor.AlertSetting{ + Level: input.Level, + Notifications: ns, + Conditions: []monitor.AlertCondition{ + { + Type: "query", + Operator: "and", + Query: monitor.AlertQuery{ + Model: q, + From: input.Period, + To: "now", + }, + Reducer: reducer, + Evaluator: eval, + }, + }, + } +} + +func GetMeterAlertQuery( + input monitor.MeterAlertCreateInput, + measurement string, + db string, + allAccountIds []string, + groupByStr string, +) ( + monitor.MetricQuery, + monitor.Condition, + monitor.Condition) { + var ( + evaluator, reducer monitor.Condition + alertType, field string + filters []monitor.MetricQueryTag + ) + groupBy := []monitor.MetricQueryPart{} + evaluator = monitor.GetNodeAlertEvaluator(input.Comparator, input.Threshold) + + if input.AccountId == "" { + reducer = monitor.Condition{Type: "sum"} + alertType = "overview" + field = "sum" + for _, aId := range allAccountIds { + filters = append(filters, monitor.MetricQueryTag{ + Key: "accountId", + Value: aId, + Condition: "or", + }) + } + } else { + reducer = monitor.Condition{Type: "avg"} + alertType = "account" + field = input.Type + groupBy = append(groupBy, monitor.MetricQueryPart{ + Type: "field", + Params: []string{field}, + }) + filters = append(filters, monitor.MetricQueryTag{ + Key: "accountId", + Value: input.AccountId, + Condition: "and", + }) + filters = append(filters, monitor.MetricQueryTag{ + Key: "provider", + Value: input.Provider, + }) + } + + log.Debugf("==alertType: %s", alertType) + + if input.ProjectId != "" { + filters = append(filters, monitor.MetricQueryTag{ + Key: "projectId", + Value: input.ProjectId, + }) + } + + groupBy = append(groupBy, monitor.MetricQueryPart{ + Type: "field", + Params: []string{groupByStr}, + }) + + sels := make([]monitor.MetricQuerySelect, 0) + sels = append(sels, monitor.NewMetricQuerySelect( + monitor.MetricQueryPart{ + Type: "field", + Params: []string{input.Type}, + })) + q := monitor.MetricQuery{ + Selects: sels, + Tags: filters, + GroupBy: groupBy, + Measurement: measurement, + Database: db, + } + return q, reducer, evaluator +} + +func (man *SMeterAlertManager) GetAlert(id string) (*SMeterAlert, error) { + obj, err := man.FetchById(id) + if err != nil { + return nil, err + } + return obj.(*SMeterAlert), nil +} + +func (man *SMeterAlertManager) CustomizeFilterList( + ctx context.Context, q *sqlchemy.SQuery, + userCred mcclient.TokenCredential, query jsonutils.JSONObject) ( + *db.CustomizeListFilters, error) { + filters, err := man.SV1AlertManager.CustomizeFilterList(ctx, q, userCred, query) + if err != nil { + return nil, err + } + input := new(monitor.MeterAlertListInput) + if err := query.Unmarshal(input); err != nil { + return nil, err + } + wrapF := func(f func(obj *SMeterAlert) (bool, error)) func(object jsonutils.JSONObject) (bool, error) { + return func(data jsonutils.JSONObject) (bool, error) { + id, err := data.GetString("id") + if err != nil { + return false, err + } + obj, err := man.GetAlert(id) + if err != nil { + return false, err + } + return f(obj) + } + } + + if input.Type != "" { + filters.Append(wrapF(func(obj *SMeterAlert) (bool, error) { + return obj.getType() == input.Type, nil + })) + } + + if input.AccountId != "" { + filters.Append(wrapF(func(obj *SMeterAlert) (bool, error) { + return obj.getAccountId() == input.AccountId, nil + })) + } + + if input.Provider != "" { + filters.Append(wrapF(func(obj *SMeterAlert) (bool, error) { + return obj.getProvider() == input.Provider, nil + })) + } + + if input.ProjectId != "" { + filters.Append(wrapF(func(obj *SMeterAlert) (bool, error) { + return obj.getProjectId() == input.ProjectId, nil + })) + } + + return filters, nil +} + +func (alert *SMeterAlert) setType(ctx context.Context, userCred mcclient.TokenCredential, t string) error { + return alert.SetMetadata(ctx, MeterAlertMetadataType, t, userCred) +} + +func (alert *SMeterAlert) getType() string { + return alert.GetMetadata(MeterAlertMetadataType, nil) +} + +func (alert *SMeterAlert) setProjectId(ctx context.Context, userCred mcclient.TokenCredential, id string) error { + return alert.SetMetadata(ctx, MeterAlertMetadataProjectId, id, userCred) +} + +func (alert *SMeterAlert) getProjectId() string { + return alert.GetMetadata(MeterAlertMetadataProjectId, nil) +} + +func (alert *SMeterAlert) setAccountId(ctx context.Context, userCred mcclient.TokenCredential, id string) error { + return alert.SetMetadata(ctx, MeterAlertMetadataAccountId, id, userCred) +} + +func (alert *SMeterAlert) getAccountId() string { + return alert.GetMetadata(MeterAlertMetadataAccountId, nil) +} + +func (alert *SMeterAlert) setProvider(ctx context.Context, userCred mcclient.TokenCredential, p string) error { + return alert.SetMetadata(ctx, MeterAlertMetadataProvider, p, userCred) +} + +func (alert *SMeterAlert) getProvider() string { + return alert.GetMetadata(MeterAlertMetadataProvider, nil) +} + +func (alert *SMeterAlert) PostCreate(ctx context.Context, + userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, + query jsonutils.JSONObject, data jsonutils.JSONObject) { + alert.SVirtualResourceBase.PostCreate(ctx, userCred, ownerId, query, data) + input := new(monitor.MeterAlertCreateInput) + if err := data.Unmarshal(input); err != nil { + log.Errorf("post create unmarshal input: %v", err) + return + } + if input.Type != "" { + if err := alert.setType(ctx, userCred, input.Type); err != nil { + log.Errorf("set type: %v", err) + } + } + if input.Provider != "" { + if err := alert.setProvider(ctx, userCred, input.Provider); err != nil { + log.Errorf("set proider: %v", err) + } + } + if input.AccountId != "" { + if err := alert.setAccountId(ctx, userCred, input.AccountId); err != nil { + log.Errorf("set account_id: %v", err) + } + } + if input.ProjectId != "" { + if err := alert.setProjectId(ctx, userCred, input.ProjectId); err != nil { + log.Errorf("set project_id: %v", err) + } + } +} + +func (alert *SMeterAlert) GetExtraDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, isList bool) (monitor.MeterAlertDetails, error) { + var err error + out := monitor.MeterAlertDetails{} + commonDetails, err := alert.SV1Alert.GetExtraDetails(ctx, userCred, query, isList) + if err != nil { + return out, err + } + out.AlertV1Details = commonDetails + + out.Type = alert.getType() + out.ProjectId = alert.getProjectId() + out.Provider = alert.getProvider() + out.AccountId = alert.getAccountId() + + return out, nil +} diff --git a/pkg/monitor/models/nodealert.go b/pkg/monitor/models/nodealert.go new file mode 100644 index 0000000000..e8723e35d7 --- /dev/null +++ b/pkg/monitor/models/nodealert.go @@ -0,0 +1,611 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package models + +import ( + "context" + "fmt" + "strings" + "time" + + "yunion.io/x/jsonutils" + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + "yunion.io/x/sqlchemy" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/auth" + "yunion.io/x/onecloud/pkg/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" + "yunion.io/x/onecloud/pkg/monitor/options" +) + +const ( + NodeAlertMetadataType = "type" + NodeAlertMetadataNodeId = "node_id" + NodeAlertMetadataNodeName = "node_name" +) + +var NodeAlertManager *SNodeAlertManager + +func init() { + NodeAlertManager = NewNodeAlertManager() +} + +type SV1AlertManager struct { + SAlertManager +} + +type SNodeAlertManager struct { + SV1AlertManager +} + +func NewNodeAlertManager() *SNodeAlertManager { + man := &SNodeAlertManager{ + SV1AlertManager: SV1AlertManager{ + *NewAlertManager(SNodeAlert{}, "nodealert", "nodealerts"), + }, + } + man.SetVirtualObject(man) + return man +} + +type SV1Alert struct { + SAlert +} + +type SNodeAlert struct { + SV1Alert +} + +func (v1man *SV1AlertManager) CreateNotification( + ctx context.Context, + userCred mcclient.TokenCredential, + alertName string, + channel string, + recipients string) (*SAlertNotification, error) { + userIds := strings.Split(recipients, ",") + return AlertNotificationManager.CreateOneCloudNotification(ctx, userCred, alertName, channel, userIds) +} + +func (man *SNodeAlertManager) ValidateCreateData( + ctx context.Context, userCred mcclient.TokenCredential, + ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, + data monitor.NodeAlertCreateInput) (*monitor.NodeAlertCreateInput, error) { + if data.Period == "" { + data.Period = "5m" + } + if _, err := time.ParseDuration(data.Period); err != nil { + return nil, httperrors.NewInputParameterError("Invalid period format: %s", data.Period) + } + if data.Metric == "" { + return nil, httperrors.NewInputParameterError("metric is missing") + } + parts := strings.Split(data.Metric, ".") + if len(parts) != 2 { + return nil, httperrors.NewInputParameterError("metric %s is invalid format, usage .", data.Metric) + } + measurement, field, err := GetMeasurementField(data.Metric) + if err != nil { + return nil, err + } + if data.Recipients == "" { + return nil, httperrors.NewInputParameterError("recipients is empty") + } + notification, err := man.CreateNotification(ctx, userCred, data.Metric, data.Channel, data.Recipients) + if err != nil { + return nil, errors.Wrap(err, "create notification") + } + if data.NodeId == "" { + return nil, httperrors.NewInputParameterError("node_id is empty") + } + nodeName, resType, err := man.validateResourceId(ctx, data.Type, data.NodeId) + if err != nil { + return nil, err + } + data.NodeName = nodeName + name, err := man.genName(ownerId, resType, nodeName, data.Metric) + if err != nil { + return nil, err + } + alertInput := data.ToAlertCreateInput(name, field, measurement, "telegraf", []string{notification.GetId()}) + alertInput, err = AlertManager.ValidateCreateData(ctx, userCred, ownerId, query, alertInput) + if err != nil { + return nil, err + } + data.AlertCreateInput = &alertInput + return &data, nil +} + +func (man *SNodeAlertManager) genName(ownerId mcclient.IIdentityProvider, resType string, nodeName string, metric string) (string, error) { + nameHint := fmt.Sprintf("%s %s %s", resType, nodeName, metric) + name, err := db.GenerateName(man, ownerId, nameHint) + if err != nil { + return "", err + } + return name, nil +} + +func (man *SNodeAlertManager) validateResourceId(ctx context.Context, nodeType, nodeId string) (string, string, error) { + var ( + retType string + nodeName string + err error + ) + switch nodeType { + case monitor.NodeAlertTypeHost: + retType = "宿主机" + nodeName, err = man.validateHostResource(ctx, nodeId) + case monitor.NodeAlertTypeGuest: + retType = "虚拟机" + nodeName, err = man.validateGuestResource(ctx, nodeId) + default: + return "", "", httperrors.NewInputParameterError("unsupported resource type %s", nodeType) + } + return nodeName, retType, err +} + +func (man *SNodeAlertManager) validateGuestResource(ctx context.Context, id string) (string, error) { + return man.validateResourceByMod(ctx, &modules.Servers, id) +} + +func (man *SNodeAlertManager) validateHostResource(ctx context.Context, id string) (string, error) { + return man.validateResourceByMod(ctx, &modules.Hosts, id) +} + +func (man *SNodeAlertManager) validateResourceByMod(ctx context.Context, mod modulebase.Manager, id string) (string, error) { + s := auth.GetAdminSession(ctx, options.Options.Region, "") + ret, err := mod.Get(s, id, nil) + if err != nil { + return "", err + } + name, err := ret.GetString("name") + if err != nil { + return "", err + } + return name, nil +} + +func (man *SNodeAlertManager) ValidateListConditions(ctx context.Context, userCred mcclient.TokenCredential, query *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { + // hack: always use details in query to get more details + query.Set("details", jsonutils.JSONTrue) + return query, nil +} + +func (man *SV1AlertManager) ListItemFilter( + ctx context.Context, q *sqlchemy.SQuery, + userCred mcclient.TokenCredential, + query monitor.NodeAlertListInput) (*sqlchemy.SQuery, error) { + return AlertManager.ListItemFilter(ctx, q, userCred, query.ToAlertListInput()) +} + +func (man *SNodeAlertManager) GetAlert(id string) (*SNodeAlert, error) { + obj, err := man.FetchById(id) + if err != nil { + return nil, err + } + return obj.(*SNodeAlert), nil +} + +func (man *SNodeAlertManager) CustomizeFilterList( + ctx context.Context, q *sqlchemy.SQuery, + userCred mcclient.TokenCredential, query jsonutils.JSONObject) ( + *db.CustomizeListFilters, error) { + filters, err := man.SV1AlertManager.CustomizeFilterList(ctx, q, userCred, query) + if err != nil { + return nil, err + } + input := new(monitor.NodeAlertListInput) + if err := query.Unmarshal(input); err != nil { + return nil, err + } + wrapF := func(f func(obj *SNodeAlert) (bool, error)) func(object jsonutils.JSONObject) (bool, error) { + return func(data jsonutils.JSONObject) (bool, error) { + id, err := data.GetString("id") + if err != nil { + return false, err + } + obj, err := man.GetAlert(id) + if err != nil { + return false, err + } + return f(obj) + } + } + + if input.Metric != "" { + metric := input.Metric + meaurement, field, err := GetMeasurementField(metric) + if err != nil { + return nil, err + } + mF := func(obj *SNodeAlert) (bool, error) { + settings := new(monitor.AlertSetting) + if err := obj.Settings.Unmarshal(settings, "settings"); err != nil { + return false, errors.Wrapf(err, "alert %s unmarshal", obj.GetId()) + } + for _, s := range settings.Conditions { + if s.Query.Model.Measurement == meaurement && len(s.Query.Model.Selects) == 1 { + if IsQuerySelectHasField(s.Query.Model.Selects[0], field) { + return true, nil + } + } + } + return false, nil + } + filters.Append(wrapF(mF)) + } + + if input.NodeName != "" { + nf := func(obj *SNodeAlert) (bool, error) { + return obj.getNodeName() == input.NodeName, nil + } + filters.Append(wrapF(nf)) + } + + if input.NodeId != "" { + filters.Append(wrapF(func(obj *SNodeAlert) (bool, error) { + return obj.getNodeId() == input.NodeId, nil + })) + } + + if input.Type != "" { + filters.Append(wrapF(func(obj *SNodeAlert) (bool, error) { + return obj.getType() == input.Type, nil + })) + } + + return filters, nil +} + +func (alert *SNodeAlert) getNodeId() string { + return alert.GetMetadata(NodeAlertMetadataNodeId, nil) +} + +func (alert *SNodeAlert) setNodeId(ctx context.Context, userCred mcclient.TokenCredential, id string) error { + return alert.SetMetadata(ctx, NodeAlertMetadataNodeId, id, userCred) +} + +func (alert *SNodeAlert) getNodeName() string { + return alert.GetMetadata(NodeAlertMetadataNodeName, nil) +} + +func (alert *SNodeAlert) setNodeName(ctx context.Context, userCred mcclient.TokenCredential, name string) error { + return alert.SetMetadata(ctx, NodeAlertMetadataNodeName, name, userCred) +} + +func (alert *SNodeAlert) getType() string { + return alert.GetMetadata(NodeAlertMetadataType, nil) +} + +func (alert *SNodeAlert) setType(ctx context.Context, userCred mcclient.TokenCredential, typ string) error { + return alert.SetMetadata(ctx, NodeAlertMetadataType, typ, userCred) +} + +func (alert *SNodeAlert) PostCreate(ctx context.Context, + userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, + query jsonutils.JSONObject, data jsonutils.JSONObject) { + alert.SVirtualResourceBase.PostCreate(ctx, userCred, ownerId, query, data) + input := new(monitor.NodeAlertCreateInput) + if err := data.Unmarshal(input); err != nil { + log.Errorf("post create unmarshal input: %v", err) + return + } + if err := alert.setNodeId(ctx, userCred, input.NodeId); err != nil { + log.Errorf("set node id: %v", err) + return + } + if err := alert.setNodeName(ctx, userCred, input.NodeName); err != nil { + log.Errorf("set node name: %v", err) + return + } + if err := alert.setType(ctx, userCred, input.Type); err != nil { + log.Errorf("set type: %v", err) + return + } +} + +func (alert *SV1Alert) GetExtraDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, isList bool) (monitor.AlertV1Details, error) { + var err error + out := monitor.AlertV1Details{} + out.VirtualResourceDetails, err = alert.SVirtualResourceBase.GetExtraDetails(ctx, userCred, query, isList) + if err != nil { + return out, err + } + out.Name = alert.GetName() + if alert.Frequency < 60 { + out.Window = fmt.Sprintf("%ds", alert.Frequency) + } else { + out.Window = fmt.Sprintf("%dm", alert.Frequency/60) + } + + setting, err := alert.GetSettings() + if err != nil { + return out, err + } + if len(setting.Conditions) == 0 { + return out, nil + } + cond := setting.Conditions[0] + cmp := "" + switch cond.Evaluator.Type { + case "gt": + cmp = ">=" + case "lt": + cmp = "<=" + } + out.Level = setting.Level + out.Comparator = cmp + out.Threshold = cond.Evaluator.Params[0] + out.Period = cond.Query.From + + notification := alert.GetNotificationBySetting(setting) + if notification != nil { + out.Recipients = strings.Join(notification.UserIds, ",") + out.Channel = notification.Channel + } + + q := cond.Query + measurement := q.Model.Measurement + field := q.Model.Selects[0][0].Params[0] + db := q.Model.Database + out.Measurement = measurement + out.Field = field + out.DB = db + noti, err := alert.GetNotification() + if err != nil { + return out, err + } + if noti != nil { + out.NotifierId = noti.GetId() + } + return out, nil +} + +func (alert *SNodeAlert) GetExtraDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, isList bool) (monitor.NodeAlertDetails, error) { + var err error + out := monitor.NodeAlertDetails{} + commonDetails, err := alert.SV1Alert.GetExtraDetails(ctx, userCred, query, isList) + if err != nil { + return out, err + } + out.AlertV1Details = commonDetails + + out.Type = alert.getType() + out.NodeId = alert.getNodeId() + out.NodeName = alert.getNodeName() + + setting, err := alert.GetSettings() + if err != nil { + return out, err + } + if len(setting.Conditions) == 0 { + return out, nil + } + out.Metric = fmt.Sprintf("%s.%s", out.Measurement, out.Field) + + return out, nil +} + +func (alert *SV1Alert) GetNotification() (*SAlertNotification, error) { + setting, err := alert.GetSettings() + if err != nil { + return nil, err + } + nIds := setting.Notifications + if len(nIds) == 0 { + return nil, nil + } + // only get first notification setting + nId := nIds[0] + obj, err := AlertNotificationManager.GetNotification(nId) + if err != nil { + return nil, errors.Wrapf(err, "Get notificatoin %s", nId) + } + return obj, nil +} + +func (alert *SV1Alert) UpdateNotification(channel, recipients *string) error { + obj, err := alert.GetNotification() + if err != nil { + return errors.Wrap(err, "Get notification when update") + } + if obj == nil { + return nil + } + setting := new(monitor.NotificationSettingOneCloud) + if err := obj.Settings.Unmarshal(setting); err != nil { + return errors.Wrap(err, "unmarshal onecloud notification setting") + } + if channel != nil { + setting.Channel = *channel + } + if recipients != nil { + setting.UserIds = strings.Split(*recipients, ",") + } + _, err = db.Update(obj, func() error { + obj.Settings = jsonutils.Marshal(setting) + return nil + }) + return err +} + +func (alert *SV1Alert) GetNotificationBySetting(setting *monitor.AlertSetting) *monitor.NotificationSettingOneCloud { + nIds := setting.Notifications + if len(nIds) == 0 { + return nil + } + // only get first notification setting + nId := nIds[0] + obj, err := AlertNotificationManager.GetNotification(nId) + if err != nil { + log.Errorf("Get notification by %s: %v", nId, err) + return nil + } + if obj == nil { + return nil + } + ocSetting := new(monitor.NotificationSettingOneCloud) + if err := obj.Settings.Unmarshal(ocSetting); err != nil { + log.Errorf("Unmarshal notification %s setting: %v", nId, err) + return nil + } + return ocSetting +} + +func (alert *SNodeAlert) CustomizeDelete( + ctx context.Context, userCred mcclient.TokenCredential, + query jsonutils.JSONObject, data jsonutils.JSONObject) error { + notis, err := alert.GetNotifications() + if err != nil { + return err + } + for _, noti := range notis { + if err := noti.CustomizeDelete(ctx, userCred, query, data); err != nil { + return err + } + if err := noti.Delete(ctx, userCred); err != nil { + return err + } + } + return nil +} + +func (alert *SNodeAlert) ValidateUpdateData( + ctx context.Context, userCred mcclient.TokenCredential, + query jsonutils.JSONObject, input monitor.NodeAlertUpdateInput) (*jsonutils.JSONDict, error) { + ret := monitor.AlertUpdateInput{} + details, err := alert.GetExtraDetails(context.TODO(), nil, nil, false) + if err != nil { + return nil, err + } + + nameChange := false + if input.NodeId != nil && *input.NodeId != details.NodeId { + nameChange = true + ret.ResourceId = input.NodeId + details.NodeId = *input.NodeId + if err := alert.setNodeId(ctx, userCred, details.NodeId); err != nil { + return nil, err + } + } + if input.Type != nil && *input.Type != details.Type { + nameChange = true + ret.ResourceType = input.Type + details.Type = *input.Type + if err := alert.setType(ctx, userCred, details.Type); err != nil { + return nil, err + } + } + nodeName, resType, err := NodeAlertManager.validateResourceId(ctx, details.Type, details.NodeId) + if err != nil { + return nil, err + } + if details.NodeName != nodeName { + nameChange = true + if err := alert.setNodeName(ctx, userCred, nodeName); err != nil { + return nil, err + } + details.NodeName = nodeName + } + if input.Level != nil && *input.Level != details.Level { + details.Level = *input.Level + } + + if input.Window != nil && *input.Window != details.Window { + details.Window = *input.Window + freq, err := time.ParseDuration(details.Window) + if err != nil { + return nil, err + } + freqSec := int64(freq / time.Second) + ret.Frequency = &freqSec + } + + if input.Threshold != nil && *input.Threshold != details.Threshold { + details.Threshold = *input.Threshold + } + + if input.Comparator != nil && *input.Comparator != details.Comparator { + details.Comparator = *input.Comparator + } + + if input.Period != nil && *input.Period != details.Period { + details.Period = *input.Period + } + + if input.Metric != nil && *input.Metric != details.Metric { + details.Metric = *input.Metric + measurement, field, err := GetMeasurementField(*input.Metric) + if err != nil { + return nil, err + } + details.Measurement = measurement + details.Field = field + } + + name := alert.Name + if nameChange { + name, err = NodeAlertManager.genName(userCred, resType, details.NodeName, details.Metric) + if err != nil { + return nil, err + } + ret.Name = &name + } + + ds, err := DataSourceManager.GetDefaultSource() + if err != nil { + return nil, errors.Wrap(err, "get default data source") + } + // hack: update notification here + if err := alert.UpdateNotification(input.Channel, input.Recipients); err != nil { + return nil, errors.Wrap(err, "update notification") + } + tmpS := alert.getUpdateSetting(name, details, ds.GetId()) + os, err := alert.GetSettings() + if err != nil { + return nil, errors.Wrap(err, "get origin setting") + } + tmpS.Notifications = os.Notifications + ret.Settings = &tmpS + return alert.SAlert.ValidateUpdateData(ctx, userCred, query, ret) +} + +func (alert *SNodeAlert) getUpdateSetting( + name string, + details monitor.NodeAlertDetails, + dsId string, +) monitor.AlertSetting { + data := monitor.NodeAlertCreateInput{ + ResourceAlertV1CreateInput: monitor.ResourceAlertV1CreateInput{ + Period: details.Period, + Window: details.Window, + Comparator: details.Comparator, + Threshold: details.Threshold, + Level: details.Level, + Channel: details.Channel, + Recipients: details.Recipients, + }, + Metric: details.Metric, + Type: details.Type, + NodeId: details.NodeId, + } + out := data.ToAlertCreateInput(name, details.Field, details.Measurement, details.DB, []string{details.NotifierId}) + out.Settings = *setAlertDefaultSetting(&out.Settings, dsId) + return out.Settings +} diff --git a/pkg/monitor/models/notification.go b/pkg/monitor/models/notification.go new file mode 100644 index 0000000000..433137a60b --- /dev/null +++ b/pkg/monitor/models/notification.go @@ -0,0 +1,313 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package models + +import ( + "context" + "database/sql" + "fmt" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" + "yunion.io/x/sqlchemy" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/monitor/notifydrivers" +) + +var ( + AlertNotificationManager *SAlertNotificationManager + AlertNotificationStateManager *SAlertNotificationStateManager +) + +func init() { + AlertNotificationManager = NewAlertNotificationManager() + AlertNotificationStateManager = NewAlertNotificationStateManager() +} + +type SAlertNotificationManager struct { + db.SVirtualResourceBaseManager +} + +type SAlertNotificationStateManager struct { + db.SStandaloneResourceBaseManager +} + +func NewAlertNotificationManager() *SAlertNotificationManager { + man := &SAlertNotificationManager{ + SVirtualResourceBaseManager: db.NewVirtualResourceBaseManager( + SAlertNotification{}, + "alert_notifications_tbl", + "alert_notification", + "alert_notifications", + ), + } + man.SetVirtualObject(man) + return man +} + +func NewAlertNotificationStateManager() *SAlertNotificationStateManager { + man := &SAlertNotificationStateManager{ + SStandaloneResourceBaseManager: db.NewStandaloneResourceBaseManager( + SAlertNotificationState{}, + "alert_notification_states_tbl", + "alert_notification_state", + "alert_notification_states", + ), + } + man.SetVirtualObject(man) + return man +} + +type SAlertNotification struct { + db.SVirtualResourceBase + + Type string `nullable:"false" list:"user" create:"required"` + IsDefault bool `nullable:"false" default:"false" list:"user" create:"optional" update:"user"` + SendReminder bool `nullable:"false" default:"false" list:"user" create:"optional" update:"user"` + DisableResolveMessage bool `nullable:"false" default:"false" list:"user" create:"optional" update:"user"` + Frequency int64 `nullable:"false" default:"0" list:"user" create:"optional" update:"user"` + Settings jsonutils.JSONObject `nullable:"false" list:"user" create:"required" update:"user"` +} + +type SAlertNotificationState struct { + db.SStandaloneResourceBase + + AlertId string `nullable:"false" list:"user" create:"required"` + NotifierId string `nullable:"false" list:"user" create:"required"` + State string `nullable:"false" list:"user" create:"required"` +} + +func (man *SAlertNotificationManager) GetPlugin(typ string) (*notifydrivers.NotifierPlugin, error) { + drv, err := notifydrivers.GetPlugin(typ) + if err != nil { + if errors.Cause(err) == notifydrivers.ErrUnsupportedNotificationType { + return nil, httperrors.NewInputParameterError("unsupported notification type %s", typ) + } else { + return nil, err + } + } + return drv, nil +} + +func (man *SAlertNotificationManager) GetNotification(id string) (*SAlertNotification, error) { + obj, err := man.FetchById(id) + if err != nil { + if errors.Cause(err) == sql.ErrNoRows { + return nil, nil + } + return nil, err + } + return obj.(*SAlertNotification), nil +} + +func (man *SAlertNotificationManager) GetNotifications(ids []string) ([]SAlertNotification, error) { + objs := make([]SAlertNotification, 0) + notis := man.Query().SubQuery() + q := notis.Query().Filter(sqlchemy.In(notis.Field("id"), ids)) + if err := db.FetchModelObjects(man, q, &objs); err != nil { + if err == sql.ErrNoRows { + return nil, nil + } + return nil, err + } + return objs, nil +} + +func (man *SAlertNotificationManager) GetNotificationsWithDefault(ids []string) ([]SAlertNotification, error) { + objs := make([]SAlertNotification, 0) + notis := man.Query().SubQuery() + q := notis.Query().Filter( + sqlchemy.OR( + sqlchemy.IsTrue(notis.Field("is_default")), + sqlchemy.In(notis.Field("id"), ids))) + if err := db.FetchModelObjects(man, q, &objs); err != nil { + if err == sql.ErrNoRows { + return nil, nil + } + return nil, err + } + return objs, nil +} + +func (man *SAlertNotificationManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, _ jsonutils.JSONObject, input monitor.AlertNotificationCreateInput) (monitor.AlertNotificationCreateInput, error) { + if input.Type == "" { + return input, httperrors.NewInputParameterError("notification type is empty") + } + if input.SendReminder == nil { + sendReminder := true + input.SendReminder = &sendReminder + } + if input.DisableResolveMessage == nil { + dr := false + input.DisableResolveMessage = &dr + } + plug, err := man.GetPlugin(input.Type) + if err != nil { + return input, err + } + return plug.ValidateCreateData(userCred, input) +} + +func (man *SAlertNotificationManager) CreateOneCloudNotification( + ctx context.Context, + userCred mcclient.TokenCredential, + alertName string, + channel string, + userIds []string) (*SAlertNotification, error) { + settings := &monitor.NotificationSettingOneCloud{ + Channel: channel, + UserIds: userIds, + } + newName, err := db.GenerateName(man, userCred, alertName) + if err != nil { + return nil, errors.Wrapf(err, "generate name: %s", alertName) + } + input := &monitor.AlertNotificationCreateInput{ + Name: newName, + Type: monitor.AlertNotificationTypeOneCloud, + Settings: jsonutils.Marshal(settings), + } + obj, err := db.DoCreate(man, ctx, userCred, nil, input.JSON(input), userCred) + if err != nil { + return nil, errors.Wrapf(err, "create notification input: %s", input.JSON(input)) + } + return obj.(*SAlertNotification), nil +} + +func (n *SAlertNotification) GetStates() ([]SAlertNotificationState, error) { + states := AlertNotificationStateManager.Query().SubQuery() + q := states.Query().Filter(sqlchemy.Equals(states.Field("notifier_id"), n.GetId())) + objs := make([]SAlertNotificationState, 0) + if err := db.FetchModelObjects(AlertNotificationStateManager, q, &objs); err != nil { + return nil, err + } + return objs, nil +} + +func (n *SAlertNotification) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error { + stats, err := n.GetStates() + if err != nil { + return err + } + for _, stat := range stats { + if err := stat.Delete(ctx, userCred); err != nil { + return err + } + } + return nil +} + +func (man *SAlertNotificationStateManager) ValidateCreateData( + ctx context.Context, + userCred mcclient.TokenCredential, + ownerId mcclient.IIdentityProvider, + _ jsonutils.JSONObject, + input monitor.AlertNotificationStateCreateInput) (monitor.AlertNotificationStateCreateInput, error) { + if input.AlertId == "" { + return input, httperrors.NewNotEmptyError("alert_id is empty") + } + if input.NotifierId == "" { + return input, httperrors.NewNotEmptyError("notifier_id is empty") + } + var name string + if obj, err := AlertManager.FetchById(input.AlertId); err != nil { + return input, err + } else { + name = obj.GetName() + } + if obj, err := AlertNotificationManager.FetchById(input.NotifierId); err != nil { + return input, err + } else { + name = fmt.Sprintf("%s_%s", name, obj.GetName()) + } + name, err := db.GenerateName(man, ownerId, name) + if err != nil { + return input, err + } + input.Name = name + return input, nil +} + +func (man *SAlertNotificationStateManager) CreateState( + ctx context.Context, + userCred mcclient.TokenCredential, + input monitor.AlertNotificationStateCreateInput) (*SAlertNotificationState, error) { + obj, err := db.DoCreate(man, ctx, userCred, nil, input.JSON(input), userCred) + if err != nil { + return nil, errors.Wrapf(err, "create notification state: %s", input.JSON(input)) + } + return obj.(*SAlertNotificationState), nil +} + +func (man *SAlertNotificationStateManager) GetState(alertId, notifierId string) (*SAlertNotificationState, error) { + state := man.Query().SubQuery() + q := state.Query().Filter(sqlchemy.AND( + sqlchemy.Equals(state.Field("alert_id"), alertId), + sqlchemy.Equals(state.Field("notifier_id"), notifierId))) + obj := new(SAlertNotificationState) + err := q.First(obj) + if err != nil { + if errors.Cause(err) == sql.ErrNoRows { + return nil, nil + } else { + return nil, err + } + } + return obj, nil +} + +func (man *SAlertNotificationStateManager) GetOrCreateState( + ctx context.Context, + userCred mcclient.TokenCredential, + alertId string, + notifierId string) (*SAlertNotificationState, error) { + state, err := man.GetState(alertId, notifierId) + if err != nil { + return nil, err + } + if state == nil { + return man.CreateState(ctx, userCred, monitor.AlertNotificationStateCreateInput{ + AlertId: alertId, + NotifierId: notifierId, + State: monitor.AlertNotificationStateUnknown, + }) + } + state.SetModelManager(man, state) + return state, nil +} + +func (state *SAlertNotificationState) SetToPending() error { + return state.setState(monitor.AlertNotificationStatePending) +} + +func (state *SAlertNotificationState) SetToCompleted() error { + return state.setState(monitor.AlertNotificationStateCompleted) +} + +func (state *SAlertNotificationState) setState(changeState monitor.AlertNotificationStateType) error { + _, err := db.Update(state, func() error { + state.State = string(changeState) + return nil + }) + return err +} + +func (state *SAlertNotificationState) GetState() monitor.AlertNotificationStateType { + return monitor.AlertNotificationStateType(state.State) +} diff --git a/pkg/monitor/notifydrivers/doc.go b/pkg/monitor/notifydrivers/doc.go new file mode 100644 index 0000000000..4f93eab572 --- /dev/null +++ b/pkg/monitor/notifydrivers/doc.go @@ -0,0 +1 @@ +package notifydrivers // import "yunion.io/x/onecloud/pkg/monitor/notifydrivers" diff --git a/pkg/monitor/notifydrivers/drivers.go b/pkg/monitor/notifydrivers/drivers.go new file mode 100644 index 0000000000..ae3d38a908 --- /dev/null +++ b/pkg/monitor/notifydrivers/drivers.go @@ -0,0 +1,91 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package notifydrivers + +import ( + "time" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/mcclient" +) + +const ( + ErrUnsupportedNotificationType = errors.Error("Unsupported notification type") +) + +// Notifier is responsible for sending alert notifications. +type Notifier interface { + GetType() string + + GetNotifierId() string + // GetIsDefault() bool + GetSendReminder() bool + GetDisableResolveMessage() bool + GetFrequency() time.Duration +} + +type NotificationConfig struct { + Id string + Name string + Type string + SendReminder bool + DisableResolveMessage bool + Frequency time.Duration + Settings jsonutils.JSONObject +} + +type NotifierFactory func(notification NotificationConfig) (Notifier, error) + +var notifierFactories = make(map[string]*NotifierPlugin) + +type NotifierPlugin struct { + Type string + Factory NotifierFactory + ValidateCreateData func(cred mcclient.IIdentityProvider, input monitor.AlertNotificationCreateInput) (monitor.AlertNotificationCreateInput, error) +} + +func RegisterNotifier(plugin *NotifierPlugin) { + notifierFactories[plugin.Type] = plugin +} + +func GetNotifiers() []*NotifierPlugin { + list := make([]*NotifierPlugin, 0) + + for _, value := range notifierFactories { + list = append(list, value) + } + + return list +} + +func GetPlugin(typ string) (*NotifierPlugin, error) { + plugin, found := notifierFactories[typ] + if !found { + return nil, errors.Wrapf(ErrUnsupportedNotificationType, "type %s", typ) + } + return plugin, nil +} + +// InitNotifier instantiate a new notifier based on the model +func InitNotifier(config NotificationConfig) (Notifier, error) { + plugin, err := GetPlugin(config.Type) + if err != nil { + return nil, err + } + return plugin.Factory(config) +} diff --git a/pkg/monitor/notifydrivers/feishu/client.go b/pkg/monitor/notifydrivers/feishu/client.go new file mode 100644 index 0000000000..12d29edc83 --- /dev/null +++ b/pkg/monitor/notifydrivers/feishu/client.go @@ -0,0 +1,132 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package feishu + +import ( + "context" + "fmt" + "net/http" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/util/httputils" +) + +const ( + // 获取 tenant_access_token(企业自建应用) + ApiTenantAccessTokenInternal = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/" + // 获取群列表 + ApiChatList = "https://open.feishu.cn/open-apis/chat/v4/list" + // 机器人发送消息 + ApiRobotSendMessage = "https://open.feishu.cn/open-apis/message/v4/send/" +) + +var ( + cli = &http.Client{ + Transport: httputils.GetTransport(true), + } + ctx = context.Background() +) + +func Request(method httputils.THttpMethod, url string, header http.Header, body jsonutils.JSONObject) (jsonutils.JSONObject, error) { + _, resp, err := httputils.JSONRequest(cli, ctx, method, url, header, body, false) + return resp, err +} + +func checkErr(resp CommonResponser) error { + if resp.GetCode() != 0 { + return errors.Error(fmt.Sprintf("response error, code: %d, msg: %s", resp.GetCode(), resp.GetMsg())) + } + return nil +} + +func unmarshal(resp jsonutils.JSONObject, obj CommonResponser) error { + if err := resp.Unmarshal(obj); err != nil { + return errors.Wrap(err, "unmarshal json") + } + return checkErr(obj) +} + +// 获取 tenant_access_token(企业自建应用)https://open.feishu.cn/document/ukTMukTMukTM/uIjNz4iM2MjLyYzM +func GetTenantAccessTokenInternal(appId string, appSecret string) (*TenantAccesstokenResp, error) { + body := jsonutils.NewDict() + body.Add(jsonutils.NewString(appId), "app_id") + body.Add(jsonutils.NewString(appSecret), "app_secret") + ret, err := Request(httputils.POST, ApiTenantAccessTokenInternal, http.Header{}, body) + if err != nil { + return nil, err + } + obj := new(TenantAccesstokenResp) + err = unmarshal(ret, obj) + return obj, err +} + +type Tenant struct { + AccessToken string +} + +func BuildTokenHeader(token string) http.Header { + h := http.Header{} + h.Add("Authorization", fmt.Sprintf("Bearer "+token)) + return h +} + +func NewTenant(appId, appSecret string) (*Tenant, error) { + resp, err := GetTenantAccessTokenInternal(appId, appSecret) + if err != nil { + return nil, err + } + return &Tenant{ + AccessToken: resp.TenantAccessToken, + }, nil +} + +func (t *Tenant) request(method httputils.THttpMethod, url string, data jsonutils.JSONObject, out CommonResponser) error { + obj, err := Request(method, url, BuildTokenHeader(t.AccessToken), data) + if err != nil { + return err + } + err = unmarshal(obj, out) + return err +} + +func (t *Tenant) get(url string, query jsonutils.JSONObject, out CommonResponser) error { + return t.request(httputils.GET, url, query, out) +} + +func (t *Tenant) post(url string, body jsonutils.JSONObject, out CommonResponser) error { + return t.request(httputils.POST, url, body, out) +} + +func (t *Tenant) ChatList(pageSize int, pageToken string) (*GroupListResp, error) { + query := jsonutils.NewDict() + if pageSize > 0 { + query.Add(jsonutils.NewInt(int64(pageSize)), "page_size") + } + if pageToken != "" { + query.Add(jsonutils.NewString(pageToken), "page_token") + } + resp := new(GroupListResp) + err := t.get(ApiChatList, query, resp) + return resp, err +} + +func (t *Tenant) SendMessage(msg MsgReq) (*MsgResp, error) { + body := jsonutils.Marshal(msg) + resp := new(MsgResp) + err := t.post(ApiRobotSendMessage, body, resp) + return resp, err +} diff --git a/pkg/monitor/notifydrivers/feishu/doc.go b/pkg/monitor/notifydrivers/feishu/doc.go new file mode 100644 index 0000000000..2607ffac10 --- /dev/null +++ b/pkg/monitor/notifydrivers/feishu/doc.go @@ -0,0 +1 @@ +package feishu // import "yunion.io/x/onecloud/pkg/monitor/notifydrivers/feishu" diff --git a/pkg/monitor/notifydrivers/feishu/types.go b/pkg/monitor/notifydrivers/feishu/types.go new file mode 100644 index 0000000000..d2fa517ab9 --- /dev/null +++ b/pkg/monitor/notifydrivers/feishu/types.go @@ -0,0 +1,226 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package feishu + +type CommonResp struct { + Code int `json:"code"` + Msg string `json:"msg"` +} + +func (r CommonResp) GetCode() int { + return r.Code +} + +func (r CommonResp) GetMsg() string { + return r.Msg +} + +type CommonResponser interface { + GetCode() int + GetMsg() string +} + +type TenantAccesstokenResp struct { + CommonResp + TenantAccessToken string `json:"tenant_access_token"` + Expire int64 `json:"expire"` +} + +type GroupListResp struct { + CommonResp + Data *UserGroupListData `json:"data"` +} + +type UserGroupListData struct { + HasMore bool `json:"has_more"` + PageToken string `json:"page_token"` + Groups []GroupData `json:"groups"` +} + +type GroupData struct { + Avatar string `json:"avatar"` + ChatId string `json:"chat_id"` + Description string `json:"description"` + Name string `json:"name"` + OwnerOpenId string `json:"owner_open_id"` + OwnerUserId string `json:"owner_user_id"` +} + +type ChatMembersResp struct { + CommonResp + Data *ChatGroupData `json:"data"` +} + +type ChatGroupData struct { + ChatId string `json:"chat_id"` + HasMore bool `json:"has_more"` + Members []MemberData `json:"members"` +} + +type MemberData struct { + OpenId string `json:"open_id"` + UserId string `json:"user_id"` + Name string `json:"name"` +} + +const ( + MsgTypePost = "post" + MsgTypeInteractive = "interactive" +) + +//定义参照: https://open.feishu.cn/open-apis/message/v4/send/ +type MsgReq struct { + OpenId string `json:"open_id,omitempty"` + UserId string `json:"user_id,omitempty"` + Email string `json:"email,omitempty"` + ChatId string `json:"chat_id,omitempty"` + MsgType string `json:"msg_type"` + RootId string `json:"root_id,omitempty"` + UpdateMulti bool `json:"update_multi"` + + Card *Card `json:"card,omitempty"` + Content *MsgContent `json:"content,omitempty"` +} + +type MsgContent struct { + Text string `json:"text"` + ImageKey string `json:"image_key"` + Post *MsgPost `json:"post,omitempty"` +} + +type MsgPost struct { + ZhCn *MsgPostValue `json:"zh_cn,omitempty"` + EnUs *MsgPostValue `json:"en_us,omitempty"` + JaJp *MsgPostValue `json:"ja_jp,omitempty"` +} + +type MsgPostValue struct { + Title string `json:"title"` + Content interface{} `json:"content"` +} + +type MsgPostContentText struct { + Tag string `json:"tag"` + UnEscape bool `json:"un_escape"` + Text string `json:"text"` +} + +type MsgPostContentA struct { + Tag string `json:"tag"` + Text string `json:"text"` + Href string `json:"href"` +} + +type MsgPostContentAt struct { + Tag string `json:"tag"` + UserId string `json:"user_id"` +} + +type MsgPostContentImage struct { + Tag string `json:"tag"` + ImageKey string `json:"image_key"` + Width float64 `json:"width"` + Height float64 `json:"height"` +} + +//机器人消息Card字段数据格式定义 +type Card struct { + Config *CardConfig `json:"config,omitempty"` + CardLink *CardElementUrl `json:"card_link,omitempty"` + Header *CardHeader `json:"header,omitempty"` + I18nElements *I18nElement `json:"i18n_elements"` + Elements []interface{} `json:"elements"` +} + +type CardConfig struct { + WideScreenMode bool `json:"wide_screen_mode"` +} + +type CardHeader struct { + Title *CardHeaderTitle `json:"title,omitempty"` +} + +type CardHeaderTitle struct { + Tag string `json:"tag"` + Content string `json:"content"` + Lines int `json:"lines,omitempty"` + I18n *CardI18n `json:"i18n,omitempty"` +} + +type CardI18n struct { + ZhCn string `json:"zh_cn"` + EnUs string `json:"en_us"` + JaJp string `json:"ja_jp"` +} + +type CardElementUrl struct { + Url string `json:"url"` + AndroidUrl string `json:"android_url"` + IosUrl string `json:"ios_url"` + PcUrl string `json:"pc_url"` +} + +const ( + TagDiv = "div" + TagPlainText = "plain_text" + TagImg = "img" + TagNote = "note" + TagLarkMd = "lark_md" + TagHR = "hr" +) + +type CardElement struct { + Tag string `json:"tag"` + Content string `json:"content"` + Text *CardElement `json:"text"` + Fields []*CardElementField `json:"fields"` + Elements []*CardElement `json:"elements"` +} + +type CardElementField struct { + IsShort bool `json:"is_short"` + Text *CardElement `json:"text"` +} + +func NewCardElementTextField(isShort bool, content string) *CardElementField { + return &CardElementField{ + IsShort: isShort, + Text: &CardElement{Tag: TagLarkMd, Content: content}, + } +} + +func NewCardElementHR() *CardElement { + return &CardElement{Tag: TagHR} +} + +func NewCardElementText(content string) *CardElement { + return &CardElement{Tag: TagPlainText, Content: content} +} + +type I18nElement struct { + ZhCn []interface{} `json:"zh_cn"` + EnUs []interface{} `json:"en_us"` + JaJp []interface{} `json:"ja_jp"` +} + +type MsgResp struct { + CommonResp + + Data MsgRespData `json:"data"` +} + +type MsgRespData struct { + MessageId string `json:"message_id"` +} diff --git a/pkg/monitor/options/doc.go b/pkg/monitor/options/doc.go new file mode 100644 index 0000000000..3605a59b40 --- /dev/null +++ b/pkg/monitor/options/doc.go @@ -0,0 +1 @@ +package options // import "yunion.io/x/onecloud/pkg/monitor/options" diff --git a/pkg/monitor/options/options.go b/pkg/monitor/options/options.go new file mode 100644 index 0000000000..e778b4d13c --- /dev/null +++ b/pkg/monitor/options/options.go @@ -0,0 +1,34 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package options + +import ( + common_options "yunion.io/x/onecloud/pkg/cloudcommon/options" +) + +type AlerterOptions struct { + common_options.CommonOptions + common_options.DBOptions + + DataProxyTimeout int `help:"query data source proxy timeout" default:"30"` + AlertingMinIntervalSeconds int64 `help:"alerting min schedule frequency" default:"10"` + AlertingMaxAttempts int `help:"alerting engine max attempt" default:"3"` + AlertingEvaluationTimeoutSeconds int64 `help:"alerting evaluation timeout" default:"5"` + AlertingNotificationTimeoutSeconds int64 `help:"alerting notification timeout" default:"30"` +} + +var ( + Options AlerterOptions +) diff --git a/pkg/monitor/registry/doc.go b/pkg/monitor/registry/doc.go new file mode 100644 index 0000000000..577ef894bd --- /dev/null +++ b/pkg/monitor/registry/doc.go @@ -0,0 +1 @@ +package registry // import "yunion.io/x/onecloud/pkg/monitor/registry" diff --git a/pkg/monitor/registry/registry.go b/pkg/monitor/registry/registry.go new file mode 100644 index 0000000000..663640c6b3 --- /dev/null +++ b/pkg/monitor/registry/registry.go @@ -0,0 +1,118 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package registry + +import ( + "context" + "reflect" + "sort" +) + +type Descriptor struct { + Name string + Instance Service + InitPriority Priority +} + +var services []*Descriptor + +func RegisterService(instance Service) { + services = append(services, &Descriptor{ + Name: reflect.TypeOf(instance).Elem().Name(), + Instance: instance, + InitPriority: Low, + }) +} + +func Register(descriptor *Descriptor) { + services = append(services, descriptor) +} + +func GetServices() []*Descriptor { + slice := getServicesWithOverrides() + + sort.Slice(slice, func(i, j int) bool { + return slice[i].InitPriority > slice[j].InitPriority + }) + + return slice +} + +type OverrideServiceFunc func(descriptor Descriptor) (*Descriptor, bool) + +var overrides []OverrideServiceFunc + +func getServicesWithOverrides() []*Descriptor { + slice := []*Descriptor{} + for _, s := range services { + var descriptor *Descriptor + for _, fn := range overrides { + if newDescriptor, override := fn(*s); override { + descriptor = newDescriptor + break + } + } + + if descriptor != nil { + slice = append(slice, descriptor) + } else { + slice = append(slice, s) + } + } + + return slice +} + +// Service interface is the lowest common shape that services +// are expected to forfill to be started within monitor. +type Service interface { + + // Init is called by monitor main process which gives the service + // the possibility do some initial work before its started. Things + // like adding routes, bus handlers should be done in the Init function + Init() error +} + +// CanBeDisabled allows the services to decide if it should +// be started or not by itself. This is useful for services +// that might not always be started, ex alerting. +// This will be called after `Init()`. +type CanBeDisabled interface { + + // IsDisabled should return a bool saying if it can be started or not. + IsDisabled() bool +} + +// BackgroundService should be implemented for services that have +// long running tasks in the background. +type BackgroundService interface { + // Run starts the background process of the service after `Init` have been called + // on all services. The `context.Context` passed into the function should be used + // to subscribe to ctx.Done() so the service can be notified when monitor shuts down. + Run(ctx context.Context) error +} + +// IsDisabled takes an service and return true if its disabled +func IsDisabled(srv Service) bool { + canBeDisabled, ok := srv.(CanBeDisabled) + return ok && canBeDisabled.IsDisabled() +} + +type Priority int + +const ( + High Priority = 100 + Low Priority = 0 +) diff --git a/pkg/monitor/service/doc.go b/pkg/monitor/service/doc.go new file mode 100644 index 0000000000..3b3463a554 --- /dev/null +++ b/pkg/monitor/service/doc.go @@ -0,0 +1 @@ +package service // import "yunion.io/x/onecloud/pkg/monitor/service" diff --git a/pkg/monitor/service/handlers.go b/pkg/monitor/service/handlers.go new file mode 100644 index 0000000000..3e80f2460b --- /dev/null +++ b/pkg/monitor/service/handlers.go @@ -0,0 +1,43 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package service + +import ( + "yunion.io/x/onecloud/pkg/appsrv" + "yunion.io/x/onecloud/pkg/appsrv/dispatcher" + "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/monitor/models" +) + +func InitHandlers(app *appsrv.Application) { + db.InitAllManagers() + + db.RegisterModelManager(db.UserCacheManager) + db.RegisterModelManager(db.TenantCacheManager) + for _, manager := range []db.IModelManager{ + db.OpsLog, + db.Metadata, + models.DataSourceManager, + models.AlertManager, + models.NodeAlertManager, + models.MeterAlertManager, + models.AlertNotificationManager, + models.AlertNotificationStateManager, + } { + db.RegisterModelManager(manager) + handler := db.NewModelHandler(manager) + dispatcher.AddModelDispatcher("", app, handler) + } +} diff --git a/pkg/monitor/service/service.go b/pkg/monitor/service/service.go new file mode 100644 index 0000000000..fc778b3672 --- /dev/null +++ b/pkg/monitor/service/service.go @@ -0,0 +1,106 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package service + +import ( + "context" + "os" + + _ "github.com/go-sql-driver/mysql" + "golang.org/x/sync/errgroup" + + "yunion.io/x/log" + + "yunion.io/x/onecloud/pkg/cloudcommon" + common_app "yunion.io/x/onecloud/pkg/cloudcommon/app" + "yunion.io/x/onecloud/pkg/cloudcommon/db" + common_options "yunion.io/x/onecloud/pkg/cloudcommon/options" + _ "yunion.io/x/onecloud/pkg/monitor/alerting" + _ "yunion.io/x/onecloud/pkg/monitor/alerting/conditions" + _ "yunion.io/x/onecloud/pkg/monitor/alerting/notifiers" + "yunion.io/x/onecloud/pkg/monitor/models" + _ "yunion.io/x/onecloud/pkg/monitor/notifydrivers" + "yunion.io/x/onecloud/pkg/monitor/options" + "yunion.io/x/onecloud/pkg/monitor/registry" + _ "yunion.io/x/onecloud/pkg/monitor/tsdb/driver/influxdb" +) + +func StartService() { + opts := &options.Options + common_options.ParseOptions(opts, os.Args, "alerter.conf", "alerter") + + commonOpts := &opts.CommonOptions + common_app.InitAuth(commonOpts, func() { + log.Infof("Auth complete") + }) + + dbOpts := &opts.DBOptions + baseOpts := &opts.BaseOptions + + app := common_app.InitApp(baseOpts, false) + InitHandlers(app) + + db.EnsureAppInitSyncDB(app, dbOpts, models.InitDB) + defer cloudcommon.CloseDB() + + go startServices() + + common_app.ServeForever(app, baseOpts) +} + +func startServices() { + services := registry.GetServices() + // Initialize services + for _, svc := range services { + if registry.IsDisabled(svc.Instance) { + continue + } + + log.Infof("Initializing " + svc.Name) + if err := svc.Instance.Init(); err != nil { + log.Fatalf("Service %s init failed", svc.Name) + } + } + + childRoutines, ctx := errgroup.WithContext(context.Background()) + // Start background services + for _, svc := range services { + service, ok := svc.Instance.(registry.BackgroundService) + if !ok { + continue + } + + if registry.IsDisabled(svc.Instance) { + continue + } + + // Variable is needed for accessing loop variable in callback + descriptor := svc + childRoutines.Go(func() error { + if err := service.Run(ctx); err != nil { + log.Errorf("Stopped %s: %v", descriptor.Name, err) + return err + } + return nil + }) + } + + defer func() { + log.Debugf("Waiting on services...") + if waitErr := childRoutines.Wait(); waitErr != nil { + log.Errorf("A service failed: %v", waitErr) + } + }() +} diff --git a/pkg/monitor/tsdb/datasource.go b/pkg/monitor/tsdb/datasource.go new file mode 100644 index 0000000000..96efcf7bd1 --- /dev/null +++ b/pkg/monitor/tsdb/datasource.go @@ -0,0 +1,170 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tsdb + +import ( + "crypto/tls" + "net" + "net/http" + "sync" + "time" + + "yunion.io/x/onecloud/pkg/monitor/options" +) + +type DataSource struct { + Id string + Name string + Type string + Url string + User string + Password string + Database string + BasicAuth bool + BasicAuthUser string + BasicAuthPassword string + TimeInterval string + Updated time.Time +} + +type proxyTransportCache struct { + cache map[string]cachedTransport + sync.Mutex +} + +// dataSourceTransport implements http.RoundTripper (https://golang.org/pkg/net/http/#RoundTripper) +type dataSourceTransport struct { + headers map[string]string + transport *http.Transport +} + +// RoundTrip executes a single HTTP transaction, returning a Response for the provided Request. +func (d *dataSourceTransport) RoundTrip(req *http.Request) (*http.Response, error) { + for key, value := range d.headers { + req.Header.Set(key, value) + } + + return d.transport.RoundTrip(req) +} + +type cachedTransport struct { + updated time.Time + + *dataSourceTransport +} + +var ptc = proxyTransportCache{ + cache: make(map[string]cachedTransport), +} + +func (ds *DataSource) GetHttpClient() (*http.Client, error) { + transport, err := ds.GetHttpTransport() + + if err != nil { + return nil, err + } + + return &http.Client{ + Timeout: 30 * time.Second, + Transport: transport, + }, nil +} + +// getCustomHeaders returns a map with all the to be set headers +// The map key represents the HeaderName and the value represetns this header's value +func (ds *DataSource) getCustomHeaders() map[string]string { + headers := make(map[string]string) + // TODO: datasource support config customize headers + return headers +} + +func (ds *DataSource) GetHttpTransport() (*dataSourceTransport, error) { + ptc.Lock() + defer ptc.Unlock() + + if t, present := ptc.cache[ds.Id]; present && ds.Updated.Equal(t.updated) { + return t.dataSourceTransport, nil + } + + tlsConfig, err := ds.GetTLSConfig() + if err != nil { + return nil, err + } + + tlsConfig.Renegotiation = tls.RenegotiateFreelyAsClient + + // Create transport which adds all + customHeaders := ds.getCustomHeaders() + transport := &http.Transport{ + TLSClientConfig: tlsConfig, + Proxy: http.ProxyFromEnvironment, + Dial: (&net.Dialer{ + Timeout: time.Duration(options.Options.DataProxyTimeout) * time.Second, + KeepAlive: 30 * time.Second, + }).Dial, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + } + + dsTransport := &dataSourceTransport{ + headers: customHeaders, + transport: transport, + } + + ptc.cache[ds.Id] = cachedTransport{ + dataSourceTransport: dsTransport, + updated: ds.Updated, + } + + return dsTransport, nil +} + +func (ds *DataSource) GetTLSConfig() (*tls.Config, error) { + tlsConfig := &tls.Config{ + InsecureSkipVerify: true, + } + + return tlsConfig, nil +} + +/* +func (ds *DataSource) DecryptedBasicAuthPassword() string { + return ds.decryptedValue("basicAuthPassword", ds.BasicAuthPassword) +} + +func (ds *DataSource) DecryptedPassword() string { + return ds.decryptedValue("password", ds.Password) +} + +func (ds *DataSource) decryptedValue(field string, fallback string) string { + if value, ok := ds.DecryptedValue(field); ok { + return value + } + return fallback +} + +// DecryptedValue returns cached decrypted value from cached data +func (ds *DataSource) DecryptedValue(key string) (string, bool) { + value, exists := ds.DecryptedValues()[key] + return value, exists +} + +var dsDescryptionCache = + +func (ds *DataSource) DecryptedValues() map[string]string { + +}*/ diff --git a/pkg/monitor/tsdb/doc.go b/pkg/monitor/tsdb/doc.go new file mode 100644 index 0000000000..c60baf872e --- /dev/null +++ b/pkg/monitor/tsdb/doc.go @@ -0,0 +1 @@ +package tsdb // import "yunion.io/x/onecloud/pkg/monitor/tsdb" diff --git a/pkg/monitor/tsdb/driver/influxdb/doc.go b/pkg/monitor/tsdb/driver/influxdb/doc.go new file mode 100644 index 0000000000..6193d147f9 --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/doc.go @@ -0,0 +1 @@ +package influxdb // import "yunion.io/x/onecloud/pkg/monitor/tsdb/driver/influxdb" diff --git a/pkg/monitor/tsdb/driver/influxdb/influxdb.go b/pkg/monitor/tsdb/driver/influxdb/influxdb.go new file mode 100644 index 0000000000..15e2f58e9f --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/influxdb.go @@ -0,0 +1,165 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package influxdb + +import ( + "context" + "encoding/json" + "net/http" + "net/url" + "path" + "strings" + + "github.com/moul/http2curl" + "golang.org/x/net/context/ctxhttp" + + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +const ( + ErrInfluxdbInvalidResponse = errors.Error("Influxdb invalid status") +) + +func init() { + tsdb.RegisterTsdbQueryEndpoint("influxdb", NewInfluxdbExecutor) +} + +type InfluxdbExecutor struct { + QueryParser *InfluxdbQueryParser + ResponseParser *ResponseParser +} + +func NewInfluxdbExecutor(datasource *tsdb.DataSource) (tsdb.TsdbQueryEndpoint, error) { + return &InfluxdbExecutor{ + QueryParser: &InfluxdbQueryParser{}, + ResponseParser: &ResponseParser{}, + }, nil +} + +func (e *InfluxdbExecutor) Query(ctx context.Context, dsInfo *tsdb.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) { + result := &tsdb.Response{} + + query, err := e.getQuery(dsInfo, tsdbQuery.Queries, tsdbQuery) + if err != nil { + return nil, err + } + + rawQuery, err := query.Build(tsdbQuery) + if err != nil { + return nil, err + } + + db := dsInfo.Database + if db == "" { + db = tsdbQuery.Queries[0].Database + } + dsInfo.Database = db + + req, err := e.createRequest(dsInfo, rawQuery) + if err != nil { + return nil, err + } + + httpClient, err := dsInfo.GetHttpClient() + if err != nil { + return nil, err + } + + resp, err := ctxhttp.Do(ctx, httpClient, req) + if err != nil { + return nil, err + } + + defer resp.Body.Close() + if resp.StatusCode/100 != 2 { + // TODO: convert status code err + return nil, errors.Wrapf(ErrInfluxdbInvalidResponse, "status code: %v", resp.Status) + } + + var response Response + dec := json.NewDecoder(resp.Body) + dec.UseNumber() + if err := dec.Decode(&response); err != nil { + return nil, err + } + + if response.Err != nil { + return nil, response.Err + } + + // log.Errorf("==influxdb response: %s", jsonutils.Marshal(response).PrettyString()) + + result.Results = make(map[string]*tsdb.QueryResult) + ret := e.ResponseParser.Parse(&response, query) + ret.Meta = tsdb.QueryResultMeta{ + RawQuery: rawQuery, + } + result.Results["A"] = ret + + return result, nil +} + +func (e *InfluxdbExecutor) getQuery(dsInfo *tsdb.DataSource, queries []*tsdb.Query, context *tsdb.TsdbQuery) (*Query, error) { + // The model supports multiple queries, but right now this is only used from + // alerting so we only need to support batch executing 1 query at a time. + if len(queries) > 0 { + query, err := e.QueryParser.Parse(queries[0], dsInfo) + if err != nil { + return nil, err + } + return query, nil + } + return nil, errors.Error("query request contains no queries") +} + +func (e *InfluxdbExecutor) createRequest(dsInfo *tsdb.DataSource, query string) (*http.Request, error) { + u, _ := url.Parse(dsInfo.Url) + u.Path = path.Join(u.Path, "query") + req, err := func() (*http.Request, error) { + // use POST mode + bodyValues := url.Values{} + bodyValues.Add("q", query) + body := bodyValues.Encode() + return http.NewRequest(http.MethodPost, u.String(), strings.NewReader(body)) + }() + + if err != nil { + return nil, err + } + + req.Header.Set("User-Agent", "OneCloud Monitor") + + params := req.URL.Query() + params.Set("db", dsInfo.Database) + params.Set("epoch", "s") + + req.Header.Set("Content-type", "application/x-www-form-urlencoded") + + req.URL.RawQuery = params.Encode() + + /*if dsInfo.BasicAuth { + req.SetBasicAuth(dsinfo.BasicAuthUser, dsInfo.DecryptedBasicAuthPassword()) + } + + if !dsInfo.BasicAuth && dsInfo.User != "" { + req.SetBasicAuth(dsInfo.User, dsInfo.DecryptedPassword()) + }*/ + curlCmd, _ := http2curl.GetCurlCommand(req) + log.Debugf("Influxdb raw query: %q from db %s, curl: %s", query, dsInfo.Database, curlCmd) + return req, nil +} diff --git a/pkg/monitor/tsdb/driver/influxdb/models.go b/pkg/monitor/tsdb/driver/influxdb/models.go new file mode 100644 index 0000000000..833c6aec92 --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/models.go @@ -0,0 +1,58 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package influxdb + +import ( + "time" + + api "yunion.io/x/onecloud/pkg/apis/monitor" +) + +type Query struct { + Measurement string + Policy string + ResultFormat string + Tags []api.MetricQueryTag + GroupBy []*QueryPart + Selects []*Select + Alias string + Tz string + Interval time.Duration +} + +type Select []QueryPart + +type Response struct { + Results []Result + Err error +} + +type Result struct { + Series []Row + Message []*Message + Err error +} + +type Message struct { + Level string `json:"level,omitempty"` + Text string `json:"text,omitempty"` +} + +type Row struct { + Name string `json:"name,omitempty"` + Tags map[string]string `json:"tags,omitempty"` + Columns []string `json:"columns,omitempty"` + Values [][]interface{} `json:"values,omitempty"` +} diff --git a/pkg/monitor/tsdb/driver/influxdb/query.go b/pkg/monitor/tsdb/driver/influxdb/query.go new file mode 100644 index 0000000000..21bb92d1e3 --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/query.go @@ -0,0 +1,172 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package influxdb + +import ( + "fmt" + "regexp" + "strconv" + "strings" + + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +var ( + regexpOperatorPattern = regexp.MustCompile(`^\/.*\/$`) + regexpMeasurementPattern = regexp.MustCompile(`^\/.*\/$`) +) + +func (query *Query) Build(queryCtx *tsdb.TsdbQuery) (string, error) { + var res string + res = query.renderSelectors(queryCtx) + res += query.renderMeasurement() + res += query.renderWhereClause() + res += query.renderTimeFilter(queryCtx) + res += query.renderGroupBy(queryCtx) + res += query.renderTz() + + calculator := tsdb.NewIntervalCalculator(&tsdb.IntervalOptions{}) + interval := calculator.Calculate(queryCtx.TimeRange, query.Interval) + + res = strings.Replace(res, "$timeFilter", query.renderTimeFilter(queryCtx), -1) + res = strings.Replace(res, "$interval", interval.Text, -1) + res = strings.Replace(res, "$__interval_ms", strconv.FormatInt(interval.Milliseconds(), 10), -1) + res = strings.Replace(res, "$__interval", interval.Text, -1) + return res, nil +} + +func (query *Query) renderTags() []string { + var res []string + for i, tag := range query.Tags { + str := "" + + if i > 0 { + if tag.Condition == "" { + str += "AND" + } else { + str += tag.Condition + } + str += " " + } + + // If the operator is missing we fall back to sensible defaults + if tag.Operator == "" { + if regexpOperatorPattern.Match([]byte(tag.Value)) { + tag.Operator = "=~" + } else { + tag.Operator = "=" + } + } + + // quote value unless regex or number + var textValue string + if tag.Operator == "=~" || tag.Operator == "!~" { + textValue = tag.Value + } else if tag.Operator == "<" || tag.Operator == ">" { + textValue = tag.Value + } else { + textValue = fmt.Sprintf("'%s'", strings.Replace(tag.Value, `\`, `\\`, -1)) + } + + res = append(res, fmt.Sprintf(`%s"%s" %s %s`, str, tag.Key, tag.Operator, textValue)) + } + + return res +} + +func (query *Query) renderTimeFilter(queryCtx *tsdb.TsdbQuery) string { + from := "now() - " + queryCtx.TimeRange.From + to := "" + + if queryCtx.TimeRange.To != "now" && queryCtx.TimeRange.To != "" { + to = " and time < now() - " + strings.Replace(queryCtx.TimeRange.To, "now-", "", 1) + } + + return fmt.Sprintf("time > %s%s", from, to) +} + +func (query *Query) renderSelectors(queryCtx *tsdb.TsdbQuery) string { + res := "SELECT " + + var selectors []string + for _, sel := range query.Selects { + stk := "" + for _, s := range *sel { + stk = s.Render(query, queryCtx, stk) + } + selectors = append(selectors, stk) + } + + return res + strings.Join(selectors, ", ") +} + +func (query *Query) renderMeasurement() string { + var policy string + if query.Policy == "" || query.Policy == "default" { + policy = "" + } else { + policy = `"` + query.Policy + `".` + } + + measurement := query.Measurement + + if !regexpMeasurementPattern.Match([]byte(measurement)) { + measurement = fmt.Sprintf(`"%s"`, measurement) + } + + return fmt.Sprintf(` FROM %s%s`, policy, measurement) +} + +func (query *Query) renderWhereClause() string { + res := " WHERE " + conditions := query.renderTags() + if len(conditions) > 0 { + if len(conditions) > 1 { + res += "(" + strings.Join(conditions, " ") + ")" + } else { + res += conditions[0] + } + res += " AND " + } + + return res +} + +func (query *Query) renderGroupBy(queryContext *tsdb.TsdbQuery) string { + groupBy := "" + for i, group := range query.GroupBy { + if i == 0 { + groupBy += " GROUP BY" + } + + if i > 0 && group.Type != "fill" { + groupBy += ", " //fill is so very special. fill is a creep, fill is a weirdo + } else { + groupBy += " " + } + + groupBy += group.Render(query, queryContext, "") + } + + return groupBy +} + +func (query *Query) renderTz() string { + tz := query.Tz + if tz == "" { + return "" + } + return fmt.Sprintf(" tz('%s')", tz) +} diff --git a/pkg/monitor/tsdb/driver/influxdb/query_parser.go b/pkg/monitor/tsdb/driver/influxdb/query_parser.go new file mode 100644 index 0000000000..65da87d5af --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/query_parser.go @@ -0,0 +1,97 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package influxdb + +import ( + "time" + + api "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +type InfluxdbQueryParser struct{} + +func (qp *InfluxdbQueryParser) Parse(model *tsdb.Query, dsInfo *tsdb.DataSource) (*Query, error) { + policy := "default" + if model.Policy != "" { + policy = model.Policy + } + alias := model.Alias + tz := model.Tz + measurement := model.Measurement + resultFormat := model.ResultFormat + + tags := model.Tags + groupBys, err := qp.parseGroupBy(model.GroupBy) + if err != nil { + return nil, err + } + + selects, err := qp.parseSelects(model.Selects) + if err != nil { + return nil, err + } + + parsedInterval, err := tsdb.GetIntervalFrom(dsInfo, model, time.Millisecond*1) + if err != nil { + return nil, err + } + return &Query{ + Measurement: measurement, + Policy: policy, + ResultFormat: resultFormat, + GroupBy: groupBys, + Tags: tags, + Selects: selects, + Interval: parsedInterval, + Alias: alias, + Tz: tz, + }, nil +} + +func (qp *InfluxdbQueryParser) parseSelects(selects []api.MetricQuerySelect) ([]*Select, error) { + var result []*Select + + for _, selectObj := range selects { + var parts Select + for _, part := range selectObj { + queryPart, err := qp.parseQueryPart(part) + if err != nil { + return nil, err + } + parts = append(parts, *queryPart) + } + result = append(result, &parts) + } + return result, nil +} + +func (qp *InfluxdbQueryParser) parseGroupBy(groupBy []api.MetricQueryPart) ([]*QueryPart, error) { + var result []*QueryPart + + for _, gb := range groupBy { + queryPart, err := qp.parseQueryPart(gb) + if err != nil { + return nil, err + } + result = append(result, queryPart) + } + + return result, nil +} + +func (qp *InfluxdbQueryParser) parseQueryPart(part api.MetricQueryPart) (*QueryPart, error) { + return NewQueryPart(part.Type, part.Params) +} diff --git a/pkg/monitor/tsdb/driver/influxdb/query_parser_test.go b/pkg/monitor/tsdb/driver/influxdb/query_parser_test.go new file mode 100644 index 0000000000..e5e96f46af --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/query_parser_test.go @@ -0,0 +1,111 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package influxdb + +import ( + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" + + "yunion.io/x/jsonutils" + + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +func TestInfluxdbQueryParser(t *testing.T) { + Convey("Influxdb query parser", t, func() { + parser := &InfluxdbQueryParser{} + Convey("can parse influxdb json model", func() { + json := ` +{ +"group_by": [ + { + "params": ["$interval"], + "type": "time" + }, + { + "params": ["datacenter"], + "type": "tag" + }, + { + "params": ["none"], + "type": "fill" + } + ], + "measurement": "logins.count", + "tz": "Asia/Shanghai", + "policy": "default", + "refId": "B", + "result_format": "time_series", + "select": [ + [ + { + "type": "field", + "params": ["value"] + }, + { + "type": "count", + "params": [] + } + ], + [ + { + "type": "field", + "params": ["value"] + }, + { + "type": "bottom", + "params": ["3"] + } + ], + [ + { + "type": "field", + "params": ["value"] + }, + { + "type": "mean", + "params": [] + }, + { + "type": "math", + "params": [" / 100"] + } + ] + ], + "alias": "serie alias", + "tags": [ + {"key": "datacenter", "operator": "=", "value": "America"}, + {"condition": "OR", "key": "hostname", "operator": "=", "value": "server1"} + ] +} +` + obj, err := jsonutils.Parse([]byte(json)) + So(err, ShouldBeNil) + apiQuery := new(tsdb.Query) + So(obj.Unmarshal(apiQuery), ShouldBeNil) + dsInfo := &tsdb.DataSource{TimeInterval: ">20s"} + res, err := parser.Parse(apiQuery, dsInfo) + So(err, ShouldBeNil) + So(len(res.GroupBy), ShouldEqual, 3) + So(len(res.Selects), ShouldEqual, 3) + So(len(res.Tags), ShouldEqual, 2) + So(res.Tz, ShouldEqual, "Asia/Shanghai") + So(res.Interval, ShouldEqual, time.Second*20) + So(res.Alias, ShouldEqual, "serie alias") + }) + }) +} diff --git a/pkg/monitor/tsdb/driver/influxdb/query_part.go b/pkg/monitor/tsdb/driver/influxdb/query_part.go new file mode 100644 index 0000000000..2e46975def --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/query_part.go @@ -0,0 +1,182 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package influxdb + +import ( + "fmt" + "strings" + + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +var renders map[string]QueryDefinition + +type DefinitionParameters struct { + Name string + Type string +} + +type QueryDefinition struct { + Renderer func(query *Query, queryCtx *tsdb.TsdbQuery, part *QueryPart, innerExpr string) string + Params []DefinitionParameters +} + +func init() { + renders = make(map[string]QueryDefinition) + + renders["field"] = QueryDefinition{Renderer: fieldRenderer} + + renders["spread"] = QueryDefinition{Renderer: functionRenderer} + renders["count"] = QueryDefinition{Renderer: functionRenderer} + renders["distinct"] = QueryDefinition{Renderer: functionRenderer} + renders["integral"] = QueryDefinition{Renderer: functionRenderer} + renders["mean"] = QueryDefinition{Renderer: functionRenderer} + renders["median"] = QueryDefinition{Renderer: functionRenderer} + renders["sum"] = QueryDefinition{Renderer: functionRenderer} + renders["mode"] = QueryDefinition{Renderer: functionRenderer} + renders["cumulative_sum"] = QueryDefinition{Renderer: functionRenderer} + renders["non_negative_difference"] = QueryDefinition{Renderer: functionRenderer} + + renders["holt_winters"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "number", Type: "number"}, {Name: "season", Type: "number"}}, + } + renders["holt_winters_with_fit"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "number", Type: "number"}, {Name: "season", Type: "number"}}, + } + + renders["derivative"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "duration", Type: "interval"}}, + } + + renders["non_negative_derivative"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "duration", Type: "interval"}}, + } + renders["difference"] = QueryDefinition{Renderer: functionRenderer} + renders["moving_average"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "window", Type: "number"}}, + } + renders["stddev"] = QueryDefinition{Renderer: functionRenderer} + renders["time"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "interval", Type: "time"}, {Name: "offset", Type: "time"}}, + } + renders["fill"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "fill", Type: "string"}}, + } + renders["elapsed"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "duration", Type: "interval"}}, + } + renders["bottom"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "count", Type: "int"}}, + } + + renders["first"] = QueryDefinition{Renderer: functionRenderer} + renders["last"] = QueryDefinition{Renderer: functionRenderer} + renders["max"] = QueryDefinition{Renderer: functionRenderer} + renders["min"] = QueryDefinition{Renderer: functionRenderer} + renders["percentile"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "nth", Type: "int"}}, + } + renders["top"] = QueryDefinition{ + Renderer: functionRenderer, + Params: []DefinitionParameters{{Name: "count", Type: "int"}}, + } + renders["tag"] = QueryDefinition{ + Renderer: tagRenderer, + Params: []DefinitionParameters{{Name: "tag", Type: "string"}}, + } + + renders["math"] = QueryDefinition{Renderer: suffixRenderer} + renders["alias"] = QueryDefinition{Renderer: aliasRenderer} +} + +func fieldRenderer(query *Query, queryCtx *tsdb.TsdbQuery, part *QueryPart, innerExpr string) string { + if part.Params[0] == "*" { + // return "*::field" + return "*" + } + // return fmt.Sprintf(`"%s"::field`, part.Params[0]) + return fmt.Sprintf(`"%s"`, part.Params[0]) +} + +func tagRenderer(query *Query, queryCtx *tsdb.TsdbQuery, part *QueryPart, innerExpr string) string { + if part.Params[0] == "*" { + // return "*::tag" + return "*" + } + // return fmt.Sprintf(`"%s"::tag`, part.Params[0]) + return fmt.Sprintf(`"%s"`, part.Params[0]) +} + +func functionRenderer(query *Query, queryCtx *tsdb.TsdbQuery, part *QueryPart, innerExpr string) string { + for i, param := range part.Params { + if part.Type == "time" && param == "auto" { + part.Params[i] = "$__interval" + } + } + + if innerExpr != "" { + part.Params = append([]string{innerExpr}, part.Params...) + } + + params := strings.Join(part.Params, ", ") + + return fmt.Sprintf("%s(%s)", part.Type, params) +} + +func suffixRenderer(query *Query, queryCtx *tsdb.TsdbQuery, part *QueryPart, innerExpr string) string { + return fmt.Sprintf("%s %s", innerExpr, part.Params[0]) +} + +func aliasRenderer(query *Query, queryCtx *tsdb.TsdbQuery, part *QueryPart, innerExpr string) string { + return fmt.Sprintf(`%s AS "%s"`, innerExpr, part.Params[0]) +} + +func (r QueryDefinition) Render(query *Query, queryCtx *tsdb.TsdbQuery, part *QueryPart, innerExpr string) string { + return r.Renderer(query, queryCtx, part, innerExpr) +} + +func NewQueryPart(typ string, params []string) (*QueryPart, error) { + def, exist := renders[typ] + + if !exist { + return nil, fmt.Errorf("Missing query definition for %s", typ) + } + + return &QueryPart{ + Def: def, + Type: typ, + Params: params, + }, nil +} + +type QueryPart struct { + Def QueryDefinition + Type string + Params []string +} + +func (qp *QueryPart) Render(query *Query, queryCtx *tsdb.TsdbQuery, expr string) string { + return qp.Def.Renderer(query, queryCtx, qp, expr) +} diff --git a/pkg/monitor/tsdb/driver/influxdb/query_part_test.go b/pkg/monitor/tsdb/driver/influxdb/query_part_test.go new file mode 100644 index 0000000000..496e125834 --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/query_part_test.go @@ -0,0 +1,58 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package influxdb + +import ( + "testing" + + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +func TestInfluxdbQueryPart(t *testing.T) { + tcs := []struct { + mode string + input string + params []string + expected string + }{ + {mode: "field", params: []string{"value"}, input: "value", expected: `"value"`}, + {mode: "derivative", params: []string{"10s"}, input: "mean(value)", expected: `derivative(mean(value), 10s)`}, + {mode: "bottom", params: []string{"3"}, input: "value", expected: `bottom(value, 3)`}, + {mode: "time", params: []string{"$interval"}, input: "", expected: `time($interval)`}, + {mode: "time", params: []string{"auto"}, input: "", expected: `time($__interval)`}, + {mode: "spread", params: []string{}, input: "value", expected: `spread(value)`}, + {mode: "math", params: []string{"/ 100"}, input: "mean(value)", expected: `mean(value) / 100`}, + {mode: "alias", params: []string{"test"}, input: "mean(value)", expected: `mean(value) AS "test"`}, + {mode: "count", params: []string{}, input: "distinct(value)", expected: `count(distinct(value))`}, + {mode: "mode", params: []string{}, input: "value", expected: `mode(value)`}, + {mode: "cumulative_sum", params: []string{}, input: "mean(value)", expected: `cumulative_sum(mean(value))`}, + {mode: "non_negative_difference", params: []string{}, input: "max(value)", expected: `non_negative_difference(max(value))`}, + } + + queryCtx := &tsdb.TsdbQuery{TimeRange: tsdb.NewTimeRange("5m", "now")} + query := &Query{} + + for _, tc := range tcs { + part, err := NewQueryPart(tc.mode, tc.params) + if err != nil { + t.Errorf("Expected NewQueryPart to not return an error. error: %v", err) + } + + res := part.Render(query, queryCtx, tc.input) + if res != tc.expected { + t.Errorf("expected %v to render into %s", tc, tc.expected) + } + } +} diff --git a/pkg/monitor/tsdb/driver/influxdb/query_test.go b/pkg/monitor/tsdb/driver/influxdb/query_test.go new file mode 100644 index 0000000000..4d3b12b0aa --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/query_test.go @@ -0,0 +1,205 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package influxdb + +import ( + "strings" + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" + + api "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +func TestInfluxdbQueryBuilder(t *testing.T) { + + Convey("Influxdb query builder", t, func() { + + qp1, _ := NewQueryPart("field", []string{"value"}) + qp2, _ := NewQueryPart("mean", []string{}) + + mathPartDivideBy100, _ := NewQueryPart("math", []string{"/ 100"}) + mathPartDivideByIntervalMs, _ := NewQueryPart("math", []string{"/ $__interval_ms"}) + + groupBy1, _ := NewQueryPart("time", []string{"$__interval"}) + groupBy2, _ := NewQueryPart("tag", []string{"datacenter"}) + groupBy3, _ := NewQueryPart("fill", []string{"null"}) + + groupByOldInterval, _ := NewQueryPart("time", []string{"$interval"}) + + tag1 := api.MetricQueryTag{Key: "hostname", Value: "server1", Operator: "="} + tag2 := api.MetricQueryTag{Key: "hostname", Value: "server2", Operator: "=", Condition: "OR"} + + queryContext := &tsdb.TsdbQuery{ + TimeRange: tsdb.NewTimeRange("5m", "now"), + } + + Convey("can build simple query", func() { + query := &Query{ + Selects: []*Select{{*qp1, *qp2}}, + Measurement: "cpu", + Policy: "policy", + GroupBy: []*QueryPart{groupBy1, groupBy3}, + Interval: time.Second * 10, + } + + rawQuery, err := query.Build(queryContext) + So(err, ShouldBeNil) + So(rawQuery, ShouldEqual, `SELECT mean("value") FROM "policy"."cpu" WHERE time > now() - 5m GROUP BY time(10s) fill(null)`) + }) + + Convey("can build query with tz", func() { + query := &Query{ + Selects: []*Select{{*qp1, *qp2}}, + Measurement: "cpu", + GroupBy: []*QueryPart{groupBy1}, + Tz: "Europe/Paris", + Interval: time.Second * 5, + } + + rawQuery, err := query.Build(queryContext) + So(err, ShouldBeNil) + So(rawQuery, ShouldEqual, `SELECT mean("value") FROM "cpu" WHERE time > now() - 5m GROUP BY time(5s) tz('Europe/Paris')`) + }) + + Convey("can build query with group bys", func() { + query := &Query{ + Selects: []*Select{{*qp1, *qp2}}, + Measurement: "cpu", + GroupBy: []*QueryPart{groupBy1, groupBy2, groupBy3}, + Tags: []api.MetricQueryTag{tag1, tag2}, + Interval: time.Second * 5, + } + + rawQuery, err := query.Build(queryContext) + So(err, ShouldBeNil) + So(rawQuery, ShouldEqual, `SELECT mean("value") FROM "cpu" WHERE ("hostname" = 'server1' OR "hostname" = 'server2') AND time > now() - 5m GROUP BY time(5s), "datacenter" fill(null)`) + }) + + Convey("can build query with math part", func() { + query := &Query{ + Selects: []*Select{{*qp1, *qp2, *mathPartDivideBy100}}, + Measurement: "cpu", + Interval: time.Second * 5, + } + + rawQuery, err := query.Build(queryContext) + So(err, ShouldBeNil) + So(rawQuery, ShouldEqual, `SELECT mean("value") / 100 FROM "cpu" WHERE time > now() - 5m`) + }) + + Convey("can build query with math part using $__interval_ms variable", func() { + query := &Query{ + Selects: []*Select{{*qp1, *qp2, *mathPartDivideByIntervalMs}}, + Measurement: "cpu", + Interval: time.Second * 5, + } + + rawQuery, err := query.Build(queryContext) + So(err, ShouldBeNil) + So(rawQuery, ShouldEqual, `SELECT mean("value") / 5000 FROM "cpu" WHERE time > now() - 5m`) + }) + + Convey("can build query with old $interval variable", func() { + query := &Query{ + Selects: []*Select{{*qp1, *qp2}}, + Measurement: "cpu", + Policy: "", + GroupBy: []*QueryPart{groupByOldInterval}, + } + + rawQuery, err := query.Build(queryContext) + So(err, ShouldBeNil) + So(rawQuery, ShouldEqual, `SELECT mean("value") FROM "cpu" WHERE time > now() - 5m GROUP BY time(200ms)`) + }) + + Convey("can render time range", func() { + query := Query{} + Convey("render from: 2h to now-1h", func() { + query := Query{} + queryContext := &tsdb.TsdbQuery{TimeRange: tsdb.NewTimeRange("2h", "now-1h")} + So(query.renderTimeFilter(queryContext), ShouldEqual, "time > now() - 2h and time < now() - 1h") + }) + + Convey("render from: 10m", func() { + queryContext := &tsdb.TsdbQuery{TimeRange: tsdb.NewTimeRange("10m", "now")} + So(query.renderTimeFilter(queryContext), ShouldEqual, "time > now() - 10m") + }) + }) + + Convey("can render normal tags without operator", func() { + query := &Query{Tags: []api.MetricQueryTag{{Operator: "", Value: `value`, Key: "key"}}} + + So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" = 'value'`) + }) + + Convey("can render regex tags without operator", func() { + query := &Query{Tags: []api.MetricQueryTag{{Operator: "", Value: `/value/`, Key: "key"}}} + + So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" =~ /value/`) + }) + + Convey("can render regex tags", func() { + query := &Query{Tags: []api.MetricQueryTag{{Operator: "=~", Value: `/value/`, Key: "key"}}} + + So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" =~ /value/`) + }) + + Convey("can render number tags", func() { + query := &Query{Tags: []api.MetricQueryTag{{Operator: "=", Value: "10001", Key: "key"}}} + + So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" = '10001'`) + }) + + Convey("can render numbers less then condition tags", func() { + query := &Query{Tags: []api.MetricQueryTag{{Operator: "<", Value: "10001", Key: "key"}}} + + So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" < 10001`) + }) + + Convey("can render number greater then condition tags", func() { + query := &Query{Tags: []api.MetricQueryTag{{Operator: ">", Value: "10001", Key: "key"}}} + + So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" > 10001`) + }) + + Convey("can render string tags", func() { + query := &Query{Tags: []api.MetricQueryTag{{Operator: "=", Value: "value", Key: "key"}}} + + So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" = 'value'`) + }) + + Convey("can escape backslashes when rendering string tags", func() { + query := &Query{Tags: []api.MetricQueryTag{{Operator: "=", Value: `C:\test\`, Key: "key"}}} + + So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" = 'C:\\test\\'`) + }) + + Convey("can render regular measurement", func() { + query := &Query{Measurement: `apa`, Policy: "policy"} + + So(query.renderMeasurement(), ShouldEqual, ` FROM "policy"."apa"`) + }) + + Convey("can render regexp measurement", func() { + query := &Query{Measurement: `/apa/`, Policy: "policy"} + + So(query.renderMeasurement(), ShouldEqual, ` FROM "policy"./apa/`) + }) + }) + +} diff --git a/pkg/monitor/tsdb/driver/influxdb/response_parser.go b/pkg/monitor/tsdb/driver/influxdb/response_parser.go new file mode 100644 index 0000000000..77b60ecc67 --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/response_parser.go @@ -0,0 +1,160 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package influxdb + +import ( + "encoding/json" + "fmt" + "regexp" + "strconv" + "strings" + + "yunion.io/x/onecloud/pkg/monitor/tsdb" +) + +type ResponseParser struct{} + +var ( + legendFormat *regexp.Regexp +) + +func init() { + legendFormat = regexp.MustCompile(`\[\[(\w+)(\.\w+)*\]\]*|\$\s*(\w+?)*`) +} + +func (rp *ResponseParser) Parse(response *Response, query *Query) *tsdb.QueryResult { + queryRes := tsdb.NewQueryResult() + + for _, result := range response.Results { + queryRes.Series = append(queryRes.Series, rp.transformRows(result.Series, queryRes, query)...) + } + + return queryRes +} + +func (rp *ResponseParser) transformRows(rows []Row, queryResult *tsdb.QueryResult, query *Query) tsdb.TimeSeriesSlice { + var result tsdb.TimeSeriesSlice + for _, row := range rows { + for columnIndex, column := range row.Columns { + if column == "time" { + continue + } + + var points tsdb.TimeSeriesPoints + for _, valuePair := range row.Values { + point, err := rp.parseTimepoint(valuePair, columnIndex) + if err == nil { + points = append(points, point) + } + } + result = append(result, &tsdb.TimeSeries{ + Name: rp.formatSerieName(row, column, query), + Points: points, + Tags: row.Tags, + }) + } + } + + return result +} + +func (rp *ResponseParser) formatSerieName(row Row, column string, query *Query) string { + if query.Alias == "" { + return rp.buildSerieNameFromQuery(row, column) + } + + nameSegment := strings.Split(row.Name, ".") + + result := legendFormat.ReplaceAllFunc([]byte(query.Alias), func(in []byte) []byte { + aliasFormat := string(in) + aliasFormat = strings.Replace(aliasFormat, "[[", "", 1) + aliasFormat = strings.Replace(aliasFormat, "]]", "", 1) + aliasFormat = strings.Replace(aliasFormat, "$", "", 1) + + if aliasFormat == "m" || aliasFormat == "measurement" { + return []byte(query.Measurement) + } + if aliasFormat == "col" { + return []byte(column) + } + + pos, err := strconv.Atoi(aliasFormat) + if err == nil && len(nameSegment) >= pos { + return []byte(nameSegment[pos]) + } + + if !strings.HasPrefix(aliasFormat, "tag_") { + return in + } + + tagKey := strings.Replace(aliasFormat, "tag_", "", 1) + tagValue, exist := row.Tags[tagKey] + if exist { + return []byte(tagValue) + } + + return in + }) + + return string(result) +} + +func (rp *ResponseParser) buildSerieNameFromQuery(row Row, column string) string { + /*var tags []string + + for k, v := range row.Tags { + tags = append(tags, fmt.Sprintf("%s: %s", k, v)) + } + + tagText := "" + if len(tags) > 0 { + tagText = fmt.Sprintf(" { %s }", strings.Join(tags, " ")) + } + + return fmt.Sprintf("%s.%s%s", row.Name, column, tagText)*/ + return fmt.Sprintf("%s.%s", row.Name, column) +} + +func (rp *ResponseParser) parseTimepoint(valuePair []interface{}, valuePosition int) (tsdb.TimePoint, error) { + var value *float64 = rp.parseValue(valuePair[valuePosition]) + + timestampNumber, _ := valuePair[0].(json.Number) + timestamp, err := timestampNumber.Float64() + if err != nil { + return tsdb.TimePoint{}, err + } + + return tsdb.NewTimePoint(value, timestamp), nil +} + +func (rp *ResponseParser) parseValue(value interface{}) *float64 { + number, ok := value.(json.Number) + if !ok { + return nil + } + + fvalue, err := number.Float64() + if err == nil { + return &fvalue + } + + ivalue, err := number.Int64() + if err == nil { + ret := float64(ivalue) + return &ret + } + + return nil +} diff --git a/pkg/monitor/tsdb/driver/influxdb/response_parser_test.go b/pkg/monitor/tsdb/driver/influxdb/response_parser_test.go new file mode 100644 index 0000000000..f0b65308bc --- /dev/null +++ b/pkg/monitor/tsdb/driver/influxdb/response_parser_test.go @@ -0,0 +1,185 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package influxdb + +import ( + "encoding/json" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestInfluxdbResponseParser(t *testing.T) { + Convey("Influxdb response parser", t, func() { + Convey("Response parser", func() { + parser := &ResponseParser{} + + response := &Response{ + Results: []Result{ + { + Series: []Row{ + { + Name: "cpu", + Columns: []string{"time", "mean", "sum"}, + Tags: map[string]string{"datacenter": "America"}, + Values: [][]interface{}{ + {json.Number("111"), json.Number("222"), json.Number("333")}, + {json.Number("111"), json.Number("222"), json.Number("333")}, + {json.Number("111"), json.Number("null"), json.Number("333")}, + }, + }, + }, + }, + }, + } + + query := &Query{} + + result := parser.Parse(response, query) + + Convey("can parse all series", func() { + So(len(result.Series), ShouldEqual, 2) + }) + + Convey("can parse all points", func() { + So(len(result.Series[0].Points), ShouldEqual, 3) + So(len(result.Series[1].Points), ShouldEqual, 3) + }) + + Convey("can parse multi row result", func() { + So(result.Series[0].Points[1].Value(), ShouldEqual, float64(222)) + So(result.Series[1].Points[1].Value(), ShouldEqual, float64(333)) + }) + + Convey("can parse null points", func() { + So(result.Series[0].Points[2].IsValid(), ShouldBeFalse) + }) + + Convey("can format serie names", func() { + So(result.Series[0].Name, ShouldEqual, "cpu.mean") + So(result.Series[0].Tags, ShouldResemble, map[string]string{"datacenter": "America"}) + So(result.Series[1].Name, ShouldEqual, "cpu.sum") + So(result.Series[1].Tags, ShouldResemble, map[string]string{"datacenter": "America"}) + }) + }) + + Convey("Response parser with alias", func() { + parser := &ResponseParser{} + + response := &Response{ + Results: []Result{ + { + Series: []Row{ + { + Name: "cpu.upc", + Columns: []string{"time", "mean", "sum"}, + Tags: map[string]string{ + "datacenter": "America", + "dc.region.name": "Northeast", + }, + Values: [][]interface{}{ + {json.Number("111"), json.Number("222"), json.Number("333")}, + }, + }, + }, + }, + }, + } + + Convey("$ alias", func() { + Convey("simple alias", func() { + query := &Query{Alias: "serie alias"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "serie alias") + }) + + Convey("measurement alias", func() { + query := &Query{Alias: "alias $m $measurement", Measurement: "10m"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "alias 10m 10m") + }) + + Convey("column alias", func() { + query := &Query{Alias: "alias $col", Measurement: "10m"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "alias mean") + So(result.Series[1].Name, ShouldEqual, "alias sum") + }) + + Convey("tag alias", func() { + query := &Query{Alias: "alias $tag_datacenter"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "alias America") + }) + + Convey("segment alias", func() { + query := &Query{Alias: "alias $1"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "alias upc") + }) + + Convey("segment position out of bound", func() { + query := &Query{Alias: "alias $5"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "alias $5") + }) + }) + + Convey("[[]] alias", func() { + Convey("simple alias", func() { + query := &Query{Alias: "serie alias"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "serie alias") + }) + + Convey("measurement alias", func() { + query := &Query{Alias: "alias [[m]] [[measurement]]", Measurement: "10m"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "alias 10m 10m") + }) + + Convey("column alias", func() { + query := &Query{Alias: "alias [[col]]", Measurement: "10m"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "alias mean") + So(result.Series[1].Name, ShouldEqual, "alias sum") + }) + + Convey("tag alias", func() { + query := &Query{Alias: "alias [[tag_datacenter]]"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "alias America") + }) + + Convey("tag alias with periods", func() { + query := &Query{Alias: "alias [[tag_dc.region.name]]"} + result := parser.Parse(response, query) + + So(result.Series[0].Name, ShouldEqual, "alias Northeast") + }) + }) + }) + }) +} diff --git a/pkg/monitor/tsdb/interval.go b/pkg/monitor/tsdb/interval.go new file mode 100644 index 0000000000..addaa4dd34 --- /dev/null +++ b/pkg/monitor/tsdb/interval.go @@ -0,0 +1,219 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tsdb + +import ( + "fmt" + "strings" + "time" +) + +var ( + defaultRes int64 = 1500 + defaultMinInterval = time.Millisecond * 1 + year = time.Hour * 24 * 365 + day = time.Hour * 24 +) + +type Interval struct { + Text string + Value time.Duration +} + +type intervalCalculator struct { + minInterval time.Duration +} + +type IntervalCalculator interface { + Calculate(timeRange *TimeRange, minInterval time.Duration) Interval +} + +type IntervalOptions struct { + MinInterval time.Duration +} + +func NewIntervalCalculator(opt *IntervalOptions) *intervalCalculator { + if opt == nil { + opt = &IntervalOptions{} + } + + calc := &intervalCalculator{} + + if opt.MinInterval == 0 { + calc.minInterval = defaultMinInterval + } else { + calc.minInterval = opt.MinInterval + } + + return calc +} + +func (i *Interval) Milliseconds() int64 { + return i.Value.Nanoseconds() / int64(time.Millisecond) +} + +func (ic *intervalCalculator) Calculate(timerange *TimeRange, minInterval time.Duration) Interval { + to := timerange.MustGetTo().UnixNano() + from := timerange.MustGetFrom().UnixNano() + interval := time.Duration((to - from) / defaultRes) + + if interval < minInterval { + return Interval{Text: FormatDuration(minInterval), Value: minInterval} + } + + rounded := roundInterval(interval) + return Interval{Text: FormatDuration(rounded), Value: rounded} +} + +func GetIntervalFrom(dsInfo *DataSource, queryModel *Query, defaultInterval time.Duration) (time.Duration, error) { + interval := queryModel.Interval + if interval == "" && dsInfo.TimeInterval != "" { + interval = dsInfo.TimeInterval + } + if interval == "" { + return defaultInterval, nil + } + + interval = strings.Replace(strings.Replace(interval, "<", "", 1), ">", "", 1) + parsedInterval, err := time.ParseDuration(interval) + if err != nil { + return time.Duration(0), err + } + + return parsedInterval, nil +} + +// FormatDuration converts a duration into the kbn format e.g. 1m 2h or 3d +func FormatDuration(inter time.Duration) string { + if inter >= year { + return fmt.Sprintf("%dy", inter/year) + } + + if inter >= day { + return fmt.Sprintf("%dd", inter/day) + } + + if inter >= time.Hour { + return fmt.Sprintf("%dh", inter/time.Hour) + } + + if inter >= time.Minute { + return fmt.Sprintf("%dm", inter/time.Minute) + } + + if inter >= time.Second { + return fmt.Sprintf("%ds", inter/time.Second) + } + + if inter >= time.Millisecond { + return fmt.Sprintf("%dms", inter/time.Millisecond) + } + + return "1ms" +} + +func roundInterval(interval time.Duration) time.Duration { + switch true { + // 0.015s + case interval <= 15*time.Millisecond: + return time.Millisecond * 10 // 0.01s + // 0.035s + case interval <= 35*time.Millisecond: + return time.Millisecond * 20 // 0.02s + // 0.075s + case interval <= 75*time.Millisecond: + return time.Millisecond * 50 // 0.05s + // 0.15s + case interval <= 150*time.Millisecond: + return time.Millisecond * 100 // 0.1s + // 0.35s + case interval <= 350*time.Millisecond: + return time.Millisecond * 200 // 0.2s + // 0.75s + case interval <= 750*time.Millisecond: + return time.Millisecond * 500 // 0.5s + // 1.5s + case interval <= 1500*time.Millisecond: + return time.Millisecond * 1000 // 1s + // 3.5s + case interval <= 3500*time.Millisecond: + return time.Millisecond * 2000 // 2s + // 7.5s + case interval <= 7500*time.Millisecond: + return time.Millisecond * 5000 // 5s + // 12.5s + case interval <= 12500*time.Millisecond: + return time.Millisecond * 10000 // 10s + // 17.5s + case interval <= 17500*time.Millisecond: + return time.Millisecond * 15000 // 15s + // 25s + case interval <= 25000*time.Millisecond: + return time.Millisecond * 20000 // 20s + // 45s + case interval <= 45000*time.Millisecond: + return time.Millisecond * 30000 // 30s + // 1.5m + case interval <= 90000*time.Millisecond: + return time.Millisecond * 60000 // 1m + // 3.5m + case interval <= 210000*time.Millisecond: + return time.Millisecond * 120000 // 2m + // 7.5m + case interval <= 450000*time.Millisecond: + return time.Millisecond * 300000 // 5m + // 12.5m + case interval <= 750000*time.Millisecond: + return time.Millisecond * 600000 // 10m + // 12.5m + case interval <= 1050000*time.Millisecond: + return time.Millisecond * 900000 // 15m + // 25m + case interval <= 1500000*time.Millisecond: + return time.Millisecond * 1200000 // 20m + // 45m + case interval <= 2700000*time.Millisecond: + return time.Millisecond * 1800000 // 30m + // 1.5h + case interval <= 5400000*time.Millisecond: + return time.Millisecond * 3600000 // 1h + // 2.5h + case interval <= 9000000*time.Millisecond: + return time.Millisecond * 7200000 // 2h + // 4.5h + case interval <= 16200000*time.Millisecond: + return time.Millisecond * 10800000 // 3h + // 9h + case interval <= 32400000*time.Millisecond: + return time.Millisecond * 21600000 // 6h + // 24h + case interval <= 86400000*time.Millisecond: + return time.Millisecond * 43200000 // 12h + // 48h + case interval <= 172800000*time.Millisecond: + return time.Millisecond * 86400000 // 24h + // 1w + case interval <= 604800000*time.Millisecond: + return time.Millisecond * 86400000 // 24h + // 3w + case interval <= 1814400000*time.Millisecond: + return time.Millisecond * 604800000 // 1w + // 2y + case interval < 3628800000*time.Millisecond: + return time.Millisecond * 2592000000 // 30d + default: + return time.Millisecond * 31536000000 // 1y + } +} diff --git a/pkg/monitor/tsdb/interval_test.go b/pkg/monitor/tsdb/interval_test.go new file mode 100644 index 0000000000..019d5b6416 --- /dev/null +++ b/pkg/monitor/tsdb/interval_test.go @@ -0,0 +1,69 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tsdb + +import ( + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestInterval(t *testing.T) { + Convey("Default interval", t, func() { + calculator := NewIntervalCalculator(&IntervalOptions{}) + + Convey("for 5min", func() { + tr := NewTimeRange("5m", "now") + + interval := calculator.Calculate(tr, time.Millisecond*1) + So(interval.Text, ShouldEqual, "200ms") + }) + + Convey("for 15min", func() { + tr := NewTimeRange("15m", "now") + + interval := calculator.Calculate(tr, time.Millisecond*1) + So(interval.Text, ShouldEqual, "500ms") + }) + + Convey("for 30min", func() { + tr := NewTimeRange("30m", "now") + + interval := calculator.Calculate(tr, time.Millisecond*1) + So(interval.Text, ShouldEqual, "1s") + }) + + Convey("for 1h", func() { + tr := NewTimeRange("1h", "now") + + interval := calculator.Calculate(tr, time.Millisecond*1) + So(interval.Text, ShouldEqual, "2s") + }) + + Convey("Round interval", func() { + So(roundInterval(time.Millisecond*30), ShouldEqual, time.Millisecond*20) + So(roundInterval(time.Millisecond*45), ShouldEqual, time.Millisecond*50) + }) + + Convey("Format value", func() { + So(FormatDuration(time.Second*61), ShouldEqual, "1m") + So(FormatDuration(time.Millisecond*30), ShouldEqual, "30ms") + So(FormatDuration(time.Hour*23), ShouldEqual, "23h") + So(FormatDuration(time.Hour*24), ShouldEqual, "1d") + So(FormatDuration(time.Hour*24*367), ShouldEqual, "1y") + }) + }) +} diff --git a/pkg/monitor/tsdb/models.go b/pkg/monitor/tsdb/models.go new file mode 100644 index 0000000000..bf08e6c56a --- /dev/null +++ b/pkg/monitor/tsdb/models.go @@ -0,0 +1,114 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tsdb + +import api "yunion.io/x/onecloud/pkg/apis/monitor" + +type TsdbQuery struct { + TimeRange *TimeRange + Queries []*Query + Debug bool +} + +type Query struct { + RefId string + api.MetricQuery + DataSource DataSource + MaxDataPoints int64 + IntervalMs int64 +} + +type Response struct { + Results map[string]*QueryResult `json:"results"` + Message string `json:"message,omitempty"` +} + +type QueryResultMeta struct { + RawQuery string `json:"raw_query"` +} + +type QueryResult struct { + Error error `json:"-"` + ErrorString string `json:"error,omitempty"` + RefId string `json:"ref_id"` + Meta QueryResultMeta `json:"meta"` + Series TimeSeriesSlice `json:"series"` + Tables []*Table `json:"tables"` + Dataframes [][]byte `json:"dataframes"` +} + +type TimeSeries struct { + RawName string `json:"raw_name"` + Name string `json:"name"` + Points TimeSeriesPoints `json:"points"` + Tags map[string]string `json:"tags,omitempty"` +} + +type Table struct { + Columns []TableColumn `json:"columns"` + Rows []RowValues `json:"rows"` +} + +type TableColumn struct { + Text string `json:"text"` +} + +type RowValues []interface{} +type TimePoint [2]interface{} +type TimeSeriesPoints []TimePoint +type TimeSeriesSlice []*TimeSeries + +func NewQueryResult() *QueryResult { + return &QueryResult{ + Series: make(TimeSeriesSlice, 0), + } +} + +func NewTimePoint(value *float64, timestamp float64) TimePoint { + return TimePoint{value, timestamp} +} + +func NewTimePointByVal(value float64, timestamp float64) TimePoint { + return NewTimePoint(&value, timestamp) +} + +func (p TimePoint) IsValid() bool { + return p[0].(*float64) != nil +} + +func (p TimePoint) Value() float64 { + return *(p[0].(*float64)) +} + +func (p TimePoint) Timestamp() float64 { + return p[1].(float64) +} + +func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints { + points := make(TimeSeriesPoints, 0) + + for i := 0; i < len(values); i += 2 { + points = append(points, NewTimePoint(&values[i], values[i+1])) + } + + return points +} + +func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries { + return &TimeSeries{ + Name: name, + Points: points, + } +} diff --git a/pkg/monitor/tsdb/query_endpoint.go b/pkg/monitor/tsdb/query_endpoint.go new file mode 100644 index 0000000000..28d7c6024e --- /dev/null +++ b/pkg/monitor/tsdb/query_endpoint.go @@ -0,0 +1,52 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tsdb + +import ( + "context" + + "yunion.io/x/pkg/errors" +) + +type TsdbQueryEndpoint interface { + Query(ctx context.Context, ds *DataSource, query *TsdbQuery) (*Response, error) +} + +var registry map[string]GetTsdbQueryEndpointFn + +type GetTsdbQueryEndpointFn func(dsInfo *DataSource) (TsdbQueryEndpoint, error) + +func init() { + registry = make(map[string]GetTsdbQueryEndpointFn) +} + +const ( + ErrorNotFoundExecutorDataSource = errors.Error("Not find executor for data source") +) + +func getTsdbQueryEndpointFor(dsInfo *DataSource) (TsdbQueryEndpoint, error) { + if fn, exists := registry[dsInfo.Type]; exists { + executor, err := fn(dsInfo) + if err != nil { + return nil, err + } + return executor, nil + } + return nil, errors.Wrapf(ErrorNotFoundExecutorDataSource, "type: %s", dsInfo.Type) +} + +func RegisterTsdbQueryEndpoint(dataSourceType string, fn GetTsdbQueryEndpointFn) { + registry[dataSourceType] = fn +} diff --git a/pkg/monitor/tsdb/request.go b/pkg/monitor/tsdb/request.go new file mode 100644 index 0000000000..adf8c8aecf --- /dev/null +++ b/pkg/monitor/tsdb/request.go @@ -0,0 +1,30 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tsdb + +import ( + "context" +) + +type HandleRequestFunc func(ctx context.Context, dsInfo *DataSource, req *TsdbQuery) (*Response, error) + +func HandleRequest(ctx context.Context, dsInfo *DataSource, req *TsdbQuery) (*Response, error) { + endpoint, err := getTsdbQueryEndpointFor(dsInfo) + if err != nil { + return nil, err + } + + return endpoint.Query(ctx, dsInfo, req) +} diff --git a/pkg/monitor/tsdb/time_range.go b/pkg/monitor/tsdb/time_range.go new file mode 100644 index 0000000000..4d94f59e45 --- /dev/null +++ b/pkg/monitor/tsdb/time_range.go @@ -0,0 +1,134 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tsdb + +import ( + "fmt" + "strconv" + "strings" + "time" +) + +type TimeRange struct { + From string + To string + now time.Time +} + +func NewTimeRange(from, to string) *TimeRange { + return &TimeRange{ + From: from, + To: to, + now: time.Now(), + } +} + +func tryParseUnixMsEpoch(val string) (time.Time, bool) { + if val, err := strconv.ParseInt(val, 10, 64); err == nil { + seconds := val / 1000 + nano := (val - seconds*1000) * 1000000 + return time.Unix(seconds, nano), true + } + return time.Time{}, false +} + +func (tr *TimeRange) ParseFrom() (time.Time, error) { + if res, ok := tryParseUnixMsEpoch(tr.From); ok { + return res, nil + } + + fromRaw := strings.Replace(tr.From, "now-", "", 1) + diff, err := time.ParseDuration("-" + fromRaw) + if err != nil { + return time.Time{}, err + } + return tr.now.Add(diff), nil +} + +func (tr *TimeRange) ParseTo() (time.Time, error) { + if tr.To == "now" { + return tr.now, nil + } else if strings.HasPrefix(tr.To, "now-") { + withoutNow := strings.Replace(tr.To, "now-", "", 1) + + diff, err := time.ParseDuration("-" + withoutNow) + if err != nil { + return time.Time{}, nil + } + + return tr.now.Add(diff), nil + } + + if res, ok := tryParseUnixMsEpoch(tr.To); ok { + return res, nil + } + + return time.Time{}, fmt.Errorf("cannot parse to value %s", tr.To) +} + +func (tr *TimeRange) MustGetFrom() time.Time { + res, err := tr.ParseFrom() + if err != nil { + return time.Unix(0, 0) + } + return res +} + +func (tr *TimeRange) MustGetTo() time.Time { + res, err := tr.ParseTo() + if err != nil { + return time.Unix(0, 0) + } + return res +} + +func (tr *TimeRange) GetFromAsMsEpoch() int64 { + return tr.MustGetFrom().UnixNano() / int64(time.Millisecond) +} + +func (tr *TimeRange) GetFromAsSecondsEpoch() int64 { + return tr.GetFromAsMsEpoch() / 1000 +} + +func (tr *TimeRange) GetFromAsTimeUTC() time.Time { + return tr.MustGetFrom().UTC() +} + +func (tr *TimeRange) GetToAsMsEpoch() int64 { + return tr.MustGetTo().UnixNano() / int64(time.Millisecond) +} + +func (tr *TimeRange) GetToAsSecondsEpoch() int64 { + return tr.GetToAsMsEpoch() / 1000 +} + +func (tr *TimeRange) GetToAsTimeUTC() time.Time { + return tr.MustGetTo().UTC() +} + +// EpochPrecisionToMs converts epoch precision to millisecond, if needed. +// Only seconds to milliseconds supported right now +func EpochPrecisionToMs(value float64) float64 { + s := strconv.FormatFloat(value, 'e', -1, 64) + if strings.HasSuffix(s, "e+09") { + return value * float64(1e3) + } + + if strings.HasSuffix(s, "e+18") { + return value / float64(time.Millisecond) + } + + return value +} diff --git a/pkg/monitor/tsdb/time_range_test.go b/pkg/monitor/tsdb/time_range_test.go new file mode 100644 index 0000000000..26602699b6 --- /dev/null +++ b/pkg/monitor/tsdb/time_range_test.go @@ -0,0 +1,109 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tsdb + +import ( + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestTimeRange(t *testing.T) { + Convey("Time range", t, func() { + + now := time.Now() + + Convey("Can parse 5m, now", func() { + tr := TimeRange{ + From: "5m", + To: "now", + now: now, + } + + Convey("5m ago ", func() { + fiveMinAgo, _ := time.ParseDuration("-5m") + expected := now.Add(fiveMinAgo) + + res, err := tr.ParseFrom() + So(err, ShouldBeNil) + So(res.Unix(), ShouldEqual, expected.Unix()) + }) + + Convey("now ", func() { + res, err := tr.ParseTo() + So(err, ShouldBeNil) + So(res.Unix(), ShouldEqual, now.Unix()) + }) + }) + + Convey("Can parse 5h, now-10m", func() { + tr := TimeRange{ + From: "5h", + To: "now-10m", + now: now, + } + + Convey("5h ago ", func() { + fiveHourAgo, _ := time.ParseDuration("-5h") + expected := now.Add(fiveHourAgo) + + res, err := tr.ParseFrom() + So(err, ShouldBeNil) + So(res.Unix(), ShouldEqual, expected.Unix()) + }) + + Convey("now-10m ", func() { + tenMinAgo, _ := time.ParseDuration("-10m") + expected := now.Add(tenMinAgo) + res, err := tr.ParseTo() + So(err, ShouldBeNil) + So(res.Unix(), ShouldEqual, expected.Unix()) + }) + }) + + Convey("can parse unix epocs", func() { + var err error + tr := TimeRange{ + From: "1474973725473", + To: "1474975757930", + now: now, + } + + res, err := tr.ParseFrom() + So(err, ShouldBeNil) + So(res.UnixNano()/int64(time.Millisecond), ShouldEqual, int64(1474973725473)) + + res, err = tr.ParseTo() + So(err, ShouldBeNil) + So(res.UnixNano()/int64(time.Millisecond), ShouldEqual, int64(1474975757930)) + }) + + Convey("Cannot parse asdf", func() { + var err error + tr := TimeRange{ + From: "asdf", + To: "asdf", + now: now, + } + + _, err = tr.ParseFrom() + So(err, ShouldNotBeNil) + + _, err = tr.ParseTo() + So(err, ShouldNotBeNil) + }) + }) +} diff --git a/pkg/monitor/validators/doc.go b/pkg/monitor/validators/doc.go new file mode 100644 index 0000000000..a78c3c496c --- /dev/null +++ b/pkg/monitor/validators/doc.go @@ -0,0 +1 @@ +package validators // import "yunion.io/x/onecloud/pkg/monitor/validators" diff --git a/pkg/monitor/validators/validators.go b/pkg/monitor/validators/validators.go new file mode 100644 index 0000000000..e21accd2b1 --- /dev/null +++ b/pkg/monitor/validators/validators.go @@ -0,0 +1,161 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package validators + +import ( + "strings" + "time" + + "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/utils" + + "yunion.io/x/onecloud/pkg/apis/monitor" + "yunion.io/x/onecloud/pkg/httperrors" +) + +const ( + ErrMissingParameterThreshold = errors.Error("Condition is missing the threshold parameter") + ErrMissingParameterType = errors.Error("Condition is missing the type parameter") + ErrInvalidEvaluatorType = errors.Error("Invalid condition evaluator type") + ErrAlertConditionUnknown = errors.Error("Unknown alert condition") + ErrAlertConditionEmpty = errors.Error("Alert is missing conditions") +) + +var ( + EvaluatorDefaultTypes = []string{"gt", "lt"} + EvaluatorRangedTypes = []string{"within_range", "outside_range"} +) + +func ValidateAlertCreateInput(input monitor.AlertCreateInput) error { + if len(input.Settings.Conditions) == 0 { + return httperrors.NewInputParameterError("input condition is empty") + } + for _, condition := range input.Settings.Conditions { + if err := ValidateAlertCondition(condition); err != nil { + return err + } + } + return nil +} + +func ValidateAlertCondition(input monitor.AlertCondition) error { + condType := input.Type + if condType != "query" { + return httperrors.NewInputParameterError("Unkown alert condition type: %s", condType) + } + if err := ValidateAlertConditionQuery(input.Query); err != nil { + return err + } + if err := ValidateAlertConditionReducer(input.Reducer); err != nil { + return err + } + if err := ValidateAlertConditionEvaluator(input.Evaluator); err != nil { + return err + } + if input.Operator == "" { + input.Operator = "and" + } + if !utils.IsInStringArray(input.Operator, []string{"and", "or"}) { + return httperrors.NewInputParameterError("Unkown operator %s", input.Operator) + } + return nil +} + +func ValidateAlertConditionQuery(input monitor.AlertQuery) error { + if err := ValidateFromValue(input.From); err != nil { + return err + } + if err := ValidateToValue(input.To); err != nil { + return err + } + return nil +} + +func ValidateAlertConditionReducer(input monitor.Condition) error { + return nil +} + +func ValidateAlertConditionEvaluator(input monitor.Condition) error { + typ := input.Type + if typ == "" { + return ErrMissingParameterType + } + if utils.IsInStringArray(typ, EvaluatorDefaultTypes) { + return ValidateAlertConditionThresholdEvaluator(input) + } + if utils.IsInStringArray(typ, EvaluatorRangedTypes) { + return ValidateAlertConditionRangedEvaluator(input) + } + if typ != "no_value" { + return errors.Wrapf(ErrInvalidEvaluatorType, "type: %s", typ) + } + return nil +} + +func ValidateAlertConditionThresholdEvaluator(input monitor.Condition) error { + if len(input.Params) == 0 { + return errors.Wrapf(ErrMissingParameterThreshold, "Evaluator %s", HumanThresholdType(input.Type)) + } + return nil +} + +func ValidateAlertConditionRangedEvaluator(input monitor.Condition) error { + if len(input.Params) == 0 { + return errors.Wrapf(ErrMissingParameterThreshold, "Evaluator %s", HumanThresholdType(input.Type)) + } + if len(input.Params) == 1 { + return errors.Wrap(ErrMissingParameterThreshold, "RangedEvaluator parameter second parameter is missing") + } + return nil +} + +// HumanThresholdType converts a threshold "type" string to a string that matches the UI +// so errors are less confusing. +func HumanThresholdType(typ string) string { + switch typ { + case "gt": + return "IS ABOVE" + case "lt": + return "IS BELOW" + case "within_range": + return "IS WITHIN RANGE" + case "outside_range": + return "IS OUTSIDE RANGE" + } + return "" +} + +func ValidateFromValue(from string) error { + fromRaw := strings.Replace(from, "now-", "", 1) + + _, err := time.ParseDuration("-" + fromRaw) + return err +} + +func ValidateToValue(to string) error { + if to == "now" { + return nil + } else if strings.HasPrefix(to, "now-") { + withoutNow := strings.Replace(to, "now-", "", 1) + + _, err := time.ParseDuration("-" + withoutNow) + if err == nil { + return nil + } + } + + _, err := time.ParseDuration(to) + return err +} diff --git a/scripts/docker_push.sh b/scripts/docker_push.sh index 37e5f34268..e1d78a73e5 100755 --- a/scripts/docker_push.sh +++ b/scripts/docker_push.sh @@ -81,8 +81,8 @@ for component in $COMPONENTS; do echo "Please build image for climc" continue fi - build_bin $component - build_bundle_libraries $component + #build_bin $component + #build_bundle_libraries $component img_name="$REGISTRY/$component:$TAG" build_image $img_name $DOCKER_DIR/Dockerfile.$component $SRC_DIR push_image "$img_name" diff --git a/vendor/github.com/benbjohnson/clock/LICENSE b/vendor/github.com/benbjohnson/clock/LICENSE new file mode 100644 index 0000000000..ce212cb1ce --- /dev/null +++ b/vendor/github.com/benbjohnson/clock/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Ben Johnson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/benbjohnson/clock/README.md b/vendor/github.com/benbjohnson/clock/README.md new file mode 100644 index 0000000000..5d4f4fe72e --- /dev/null +++ b/vendor/github.com/benbjohnson/clock/README.md @@ -0,0 +1,104 @@ +clock [![Build Status](https://drone.io/github.com/benbjohnson/clock/status.png)](https://drone.io/github.com/benbjohnson/clock/latest) [![Coverage Status](https://coveralls.io/repos/benbjohnson/clock/badge.png?branch=master)](https://coveralls.io/r/benbjohnson/clock?branch=master) [![GoDoc](https://godoc.org/github.com/benbjohnson/clock?status.png)](https://godoc.org/github.com/benbjohnson/clock) ![Project status](http://img.shields.io/status/experimental.png?color=red) +===== + +Clock is a small library for mocking time in Go. It provides an interface +around the standard library's [`time`][time] package so that the application +can use the realtime clock while tests can use the mock clock. + +[time]: http://golang.org/pkg/time/ + + +## Usage + +### Realtime Clock + +Your application can maintain a `Clock` variable that will allow realtime and +mock clocks to be interchangable. For example, if you had an `Application` type: + +```go +import "github.com/benbjohnson/clock" + +type Application struct { + Clock clock.Clock +} +``` + +You could initialize it to use the realtime clock like this: + +```go +var app Application +app.Clock = clock.New() +... +``` + +Then all timers and time-related functionality should be performed from the +`Clock` variable. + + +### Mocking time + +In your tests, you will want to use a `Mock` clock: + +```go +import ( + "testing" + + "github.com/benbjohnson/clock" +) + +func TestApplication_DoSomething(t *testing.T) { + mock := clock.NewMock() + app := Application{Clock: mock} + ... +} +``` + +Now that you've initialized your application to use the mock clock, you can +adjust the time programmatically. The mock clock always starts from the Unix +epoch (midnight, Jan 1, 1970 UTC). + + +### Controlling time + +The mock clock provides the same functions that the standard library's `time` +package provides. For example, to find the current time, you use the `Now()` +function: + +```go +mock := clock.NewMock() + +// Find the current time. +mock.Now().UTC() // 1970-01-01 00:00:00 +0000 UTC + +// Move the clock forward. +mock.Add(2 * time.Hour) + +// Check the time again. It's 2 hours later! +mock.Now().UTC() // 1970-01-01 02:00:00 +0000 UTC +``` + +Timers and Tickers are also controlled by this same mock clock. They will only +execute when the clock is moved forward: + +``` +mock := clock.NewMock() +count := 0 + +// Kick off a timer to increment every 1 mock second. +go func() { + ticker := clock.Ticker(1 * time.Second) + for { + <-ticker.C + count++ + } +}() +runtime.Gosched() + +// Move the clock forward 10 second. +mock.Add(10 * time.Second) + +// This prints 10. +fmt.Println(count) +``` + + diff --git a/vendor/github.com/benbjohnson/clock/clock.go b/vendor/github.com/benbjohnson/clock/clock.go new file mode 100644 index 0000000000..c4c52309ef --- /dev/null +++ b/vendor/github.com/benbjohnson/clock/clock.go @@ -0,0 +1,327 @@ +package clock + +import ( + "sort" + "sync" + "time" +) + +// Clock represents an interface to the functions in the standard library time +// package. Two implementations are available in the clock package. The first +// is a real-time clock which simply wraps the time package's functions. The +// second is a mock clock which will only make forward progress when +// programmatically adjusted. +type Clock interface { + After(d time.Duration) <-chan time.Time + AfterFunc(d time.Duration, f func()) *Timer + Now() time.Time + Since(t time.Time) time.Duration + Sleep(d time.Duration) + Tick(d time.Duration) <-chan time.Time + Ticker(d time.Duration) *Ticker + Timer(d time.Duration) *Timer +} + +// New returns an instance of a real-time clock. +func New() Clock { + return &clock{} +} + +// clock implements a real-time clock by simply wrapping the time package functions. +type clock struct{} + +func (c *clock) After(d time.Duration) <-chan time.Time { return time.After(d) } + +func (c *clock) AfterFunc(d time.Duration, f func()) *Timer { + return &Timer{timer: time.AfterFunc(d, f)} +} + +func (c *clock) Now() time.Time { return time.Now() } + +func (c *clock) Since(t time.Time) time.Duration { return time.Since(t) } + +func (c *clock) Sleep(d time.Duration) { time.Sleep(d) } + +func (c *clock) Tick(d time.Duration) <-chan time.Time { return time.Tick(d) } + +func (c *clock) Ticker(d time.Duration) *Ticker { + t := time.NewTicker(d) + return &Ticker{C: t.C, ticker: t} +} + +func (c *clock) Timer(d time.Duration) *Timer { + t := time.NewTimer(d) + return &Timer{C: t.C, timer: t} +} + +// Mock represents a mock clock that only moves forward programmically. +// It can be preferable to a real-time clock when testing time-based functionality. +type Mock struct { + mu sync.Mutex + now time.Time // current time + timers clockTimers // tickers & timers +} + +// NewMock returns an instance of a mock clock. +// The current time of the mock clock on initialization is the Unix epoch. +func NewMock() *Mock { + return &Mock{now: time.Unix(0, 0)} +} + +// Add moves the current time of the mock clock forward by the duration. +// This should only be called from a single goroutine at a time. +func (m *Mock) Add(d time.Duration) { + // Calculate the final current time. + t := m.now.Add(d) + + // Continue to execute timers until there are no more before the new time. + for { + if !m.runNextTimer(t) { + break + } + } + + // Ensure that we end with the new time. + m.mu.Lock() + m.now = t + m.mu.Unlock() + + // Give a small buffer to make sure the other goroutines get handled. + gosched() +} + +// Set sets the current time of the mock clock to a specific one. +// This should only be called from a single goroutine at a time. +func (m *Mock) Set(t time.Time) { + // Continue to execute timers until there are no more before the new time. + for { + if !m.runNextTimer(t) { + break + } + } + + // Ensure that we end with the new time. + m.mu.Lock() + m.now = t + m.mu.Unlock() + + // Give a small buffer to make sure the other goroutines get handled. + gosched() +} + +// runNextTimer executes the next timer in chronological order and moves the +// current time to the timer's next tick time. The next time is not executed if +// it's next time if after the max time. Returns true if a timer is executed. +func (m *Mock) runNextTimer(max time.Time) bool { + m.mu.Lock() + + // Sort timers by time. + sort.Sort(m.timers) + + // If we have no more timers then exit. + if len(m.timers) == 0 { + m.mu.Unlock() + return false + } + + // Retrieve next timer. Exit if next tick is after new time. + t := m.timers[0] + if t.Next().After(max) { + m.mu.Unlock() + return false + } + + // Move "now" forward and unlock clock. + m.now = t.Next() + m.mu.Unlock() + + // Execute timer. + t.Tick(m.now) + return true +} + +// After waits for the duration to elapse and then sends the current time on the returned channel. +func (m *Mock) After(d time.Duration) <-chan time.Time { + return m.Timer(d).C +} + +// AfterFunc waits for the duration to elapse and then executes a function. +// A Timer is returned that can be stopped. +func (m *Mock) AfterFunc(d time.Duration, f func()) *Timer { + t := m.Timer(d) + t.C = nil + t.fn = f + return t +} + +// Now returns the current wall time on the mock clock. +func (m *Mock) Now() time.Time { + m.mu.Lock() + defer m.mu.Unlock() + return m.now +} + +// Since returns time since the mock clocks wall time. +func (m *Mock) Since(t time.Time) time.Duration { + return m.Now().Sub(t) +} + +// Sleep pauses the goroutine for the given duration on the mock clock. +// The clock must be moved forward in a separate goroutine. +func (m *Mock) Sleep(d time.Duration) { + <-m.After(d) +} + +// Tick is a convenience function for Ticker(). +// It will return a ticker channel that cannot be stopped. +func (m *Mock) Tick(d time.Duration) <-chan time.Time { + return m.Ticker(d).C +} + +// Ticker creates a new instance of Ticker. +func (m *Mock) Ticker(d time.Duration) *Ticker { + m.mu.Lock() + defer m.mu.Unlock() + ch := make(chan time.Time, 1) + t := &Ticker{ + C: ch, + c: ch, + mock: m, + d: d, + next: m.now.Add(d), + } + m.timers = append(m.timers, (*internalTicker)(t)) + return t +} + +// Timer creates a new instance of Timer. +func (m *Mock) Timer(d time.Duration) *Timer { + m.mu.Lock() + defer m.mu.Unlock() + ch := make(chan time.Time, 1) + t := &Timer{ + C: ch, + c: ch, + mock: m, + next: m.now.Add(d), + stopped: false, + } + m.timers = append(m.timers, (*internalTimer)(t)) + return t +} + +func (m *Mock) removeClockTimer(t clockTimer) { + m.mu.Lock() + defer m.mu.Unlock() + for i, timer := range m.timers { + if timer == t { + copy(m.timers[i:], m.timers[i+1:]) + m.timers[len(m.timers)-1] = nil + m.timers = m.timers[:len(m.timers)-1] + break + } + } + sort.Sort(m.timers) +} + +// clockTimer represents an object with an associated start time. +type clockTimer interface { + Next() time.Time + Tick(time.Time) +} + +// clockTimers represents a list of sortable timers. +type clockTimers []clockTimer + +func (a clockTimers) Len() int { return len(a) } +func (a clockTimers) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a clockTimers) Less(i, j int) bool { return a[i].Next().Before(a[j].Next()) } + +// Timer represents a single event. +// The current time will be sent on C, unless the timer was created by AfterFunc. +type Timer struct { + C <-chan time.Time + c chan time.Time + timer *time.Timer // realtime impl, if set + next time.Time // next tick time + mock *Mock // mock clock, if set + fn func() // AfterFunc function, if set + stopped bool // True if stopped, false if running +} + +// Stop turns off the ticker. +func (t *Timer) Stop() bool { + if t.timer != nil { + return t.timer.Stop() + } + + registered := !t.stopped + t.mock.removeClockTimer((*internalTimer)(t)) + t.stopped = true + return registered +} + +// Reset changes the expiry time of the timer +func (t *Timer) Reset(d time.Duration) bool { + if t.timer != nil { + return t.timer.Reset(d) + } + + t.next = t.mock.now.Add(d) + registered := !t.stopped + if t.stopped { + t.mock.mu.Lock() + t.mock.timers = append(t.mock.timers, (*internalTimer)(t)) + t.mock.mu.Unlock() + } + t.stopped = false + return registered +} + +type internalTimer Timer + +func (t *internalTimer) Next() time.Time { return t.next } +func (t *internalTimer) Tick(now time.Time) { + if t.fn != nil { + t.fn() + } else { + t.c <- now + } + t.mock.removeClockTimer((*internalTimer)(t)) + t.stopped = true + gosched() +} + +// Ticker holds a channel that receives "ticks" at regular intervals. +type Ticker struct { + C <-chan time.Time + c chan time.Time + ticker *time.Ticker // realtime impl, if set + next time.Time // next tick time + mock *Mock // mock clock, if set + d time.Duration // time between ticks +} + +// Stop turns off the ticker. +func (t *Ticker) Stop() { + if t.ticker != nil { + t.ticker.Stop() + } else { + t.mock.removeClockTimer((*internalTicker)(t)) + } +} + +type internalTicker Ticker + +func (t *internalTicker) Next() time.Time { return t.next } +func (t *internalTicker) Tick(now time.Time) { + select { + case t.c <- now: + default: + } + t.next = now.Add(t.d) + gosched() +} + +// Sleep momentarily so that other goroutines can process. +func gosched() { time.Sleep(1 * time.Millisecond) } diff --git a/vendor/github.com/benbjohnson/clock/go.mod b/vendor/github.com/benbjohnson/clock/go.mod new file mode 100644 index 0000000000..2785ed60c0 --- /dev/null +++ b/vendor/github.com/benbjohnson/clock/go.mod @@ -0,0 +1,3 @@ +module github.com/benbjohnson/clock + +go 1.13 diff --git a/vendor/github.com/gopherjs/gopherjs/LICENSE b/vendor/github.com/gopherjs/gopherjs/LICENSE new file mode 100644 index 0000000000..d496fef109 --- /dev/null +++ b/vendor/github.com/gopherjs/gopherjs/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2013 Richard Musiol. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/github.com/gopherjs/gopherjs/js/js.go b/vendor/github.com/gopherjs/gopherjs/js/js.go new file mode 100644 index 0000000000..3fbf1d88c6 --- /dev/null +++ b/vendor/github.com/gopherjs/gopherjs/js/js.go @@ -0,0 +1,168 @@ +// Package js provides functions for interacting with native JavaScript APIs. Calls to these functions are treated specially by GopherJS and translated directly to their corresponding JavaScript syntax. +// +// Use MakeWrapper to expose methods to JavaScript. When passing values directly, the following type conversions are performed: +// +// | Go type | JavaScript type | Conversions back to interface{} | +// | --------------------- | --------------------- | ------------------------------- | +// | bool | Boolean | bool | +// | integers and floats | Number | float64 | +// | string | String | string | +// | []int8 | Int8Array | []int8 | +// | []int16 | Int16Array | []int16 | +// | []int32, []int | Int32Array | []int | +// | []uint8 | Uint8Array | []uint8 | +// | []uint16 | Uint16Array | []uint16 | +// | []uint32, []uint | Uint32Array | []uint | +// | []float32 | Float32Array | []float32 | +// | []float64 | Float64Array | []float64 | +// | all other slices | Array | []interface{} | +// | arrays | see slice type | see slice type | +// | functions | Function | func(...interface{}) *js.Object | +// | time.Time | Date | time.Time | +// | - | instanceof Node | *js.Object | +// | maps, structs | instanceof Object | map[string]interface{} | +// +// Additionally, for a struct containing a *js.Object field, only the content of the field will be passed to JavaScript and vice versa. +package js + +// Object is a container for a native JavaScript object. Calls to its methods are treated specially by GopherJS and translated directly to their JavaScript syntax. A nil pointer to Object is equal to JavaScript's "null". Object can not be used as a map key. +type Object struct{ object *Object } + +// Get returns the object's property with the given key. +func (o *Object) Get(key string) *Object { return o.object.Get(key) } + +// Set assigns the value to the object's property with the given key. +func (o *Object) Set(key string, value interface{}) { o.object.Set(key, value) } + +// Delete removes the object's property with the given key. +func (o *Object) Delete(key string) { o.object.Delete(key) } + +// Length returns the object's "length" property, converted to int. +func (o *Object) Length() int { return o.object.Length() } + +// Index returns the i'th element of an array. +func (o *Object) Index(i int) *Object { return o.object.Index(i) } + +// SetIndex sets the i'th element of an array. +func (o *Object) SetIndex(i int, value interface{}) { o.object.SetIndex(i, value) } + +// Call calls the object's method with the given name. +func (o *Object) Call(name string, args ...interface{}) *Object { return o.object.Call(name, args...) } + +// Invoke calls the object itself. This will fail if it is not a function. +func (o *Object) Invoke(args ...interface{}) *Object { return o.object.Invoke(args...) } + +// New creates a new instance of this type object. This will fail if it not a function (constructor). +func (o *Object) New(args ...interface{}) *Object { return o.object.New(args...) } + +// Bool returns the object converted to bool according to JavaScript type conversions. +func (o *Object) Bool() bool { return o.object.Bool() } + +// String returns the object converted to string according to JavaScript type conversions. +func (o *Object) String() string { return o.object.String() } + +// Int returns the object converted to int according to JavaScript type conversions (parseInt). +func (o *Object) Int() int { return o.object.Int() } + +// Int64 returns the object converted to int64 according to JavaScript type conversions (parseInt). +func (o *Object) Int64() int64 { return o.object.Int64() } + +// Uint64 returns the object converted to uint64 according to JavaScript type conversions (parseInt). +func (o *Object) Uint64() uint64 { return o.object.Uint64() } + +// Float returns the object converted to float64 according to JavaScript type conversions (parseFloat). +func (o *Object) Float() float64 { return o.object.Float() } + +// Interface returns the object converted to interface{}. See table in package comment for details. +func (o *Object) Interface() interface{} { return o.object.Interface() } + +// Unsafe returns the object as an uintptr, which can be converted via unsafe.Pointer. Not intended for public use. +func (o *Object) Unsafe() uintptr { return o.object.Unsafe() } + +// Error encapsulates JavaScript errors. Those are turned into a Go panic and may be recovered, giving an *Error that holds the JavaScript error object. +type Error struct { + *Object +} + +// Error returns the message of the encapsulated JavaScript error object. +func (err *Error) Error() string { + return "JavaScript error: " + err.Get("message").String() +} + +// Stack returns the stack property of the encapsulated JavaScript error object. +func (err *Error) Stack() string { + return err.Get("stack").String() +} + +// Global gives JavaScript's global object ("window" for browsers and "GLOBAL" for Node.js). +var Global *Object + +// Module gives the value of the "module" variable set by Node.js. Hint: Set a module export with 'js.Module.Get("exports").Set("exportName", ...)'. +var Module *Object + +// Undefined gives the JavaScript value "undefined". +var Undefined *Object + +// Debugger gets compiled to JavaScript's "debugger;" statement. +func Debugger() {} + +// InternalObject returns the internal JavaScript object that represents i. Not intended for public use. +func InternalObject(i interface{}) *Object { + return nil +} + +// MakeFunc wraps a function and gives access to the values of JavaScript's "this" and "arguments" keywords. +func MakeFunc(fn func(this *Object, arguments []*Object) interface{}) *Object { + return Global.Call("$makeFunc", InternalObject(fn)) +} + +// Keys returns the keys of the given JavaScript object. +func Keys(o *Object) []string { + if o == nil || o == Undefined { + return nil + } + a := Global.Get("Object").Call("keys", o) + s := make([]string, a.Length()) + for i := 0; i < a.Length(); i++ { + s[i] = a.Index(i).String() + } + return s +} + +// MakeWrapper creates a JavaScript object which has wrappers for the exported methods of i. Use explicit getter and setter methods to expose struct fields to JavaScript. +func MakeWrapper(i interface{}) *Object { + v := InternalObject(i) + o := Global.Get("Object").New() + o.Set("__internal_object__", v) + methods := v.Get("constructor").Get("methods") + for i := 0; i < methods.Length(); i++ { + m := methods.Index(i) + if m.Get("pkg").String() != "" { // not exported + continue + } + o.Set(m.Get("name").String(), func(args ...*Object) *Object { + return Global.Call("$externalizeFunction", v.Get(m.Get("prop").String()), m.Get("typ"), true).Call("apply", v, args) + }) + } + return o +} + +// NewArrayBuffer creates a JavaScript ArrayBuffer from a byte slice. +func NewArrayBuffer(b []byte) *Object { + slice := InternalObject(b) + offset := slice.Get("$offset").Int() + length := slice.Get("$length").Int() + return slice.Get("$array").Get("buffer").Call("slice", offset, offset+length) +} + +// M is a simple map type. It is intended as a shorthand for JavaScript objects (before conversion). +type M map[string]interface{} + +// S is a simple slice type. It is intended as a shorthand for JavaScript arrays (before conversion). +type S []interface{} + +func init() { + // avoid dead code elimination + e := Error{} + _ = e +} diff --git a/vendor/github.com/jtolds/gls/LICENSE b/vendor/github.com/jtolds/gls/LICENSE new file mode 100644 index 0000000000..9b4a822d92 --- /dev/null +++ b/vendor/github.com/jtolds/gls/LICENSE @@ -0,0 +1,18 @@ +Copyright (c) 2013, Space Monkey, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/github.com/jtolds/gls/README.md b/vendor/github.com/jtolds/gls/README.md new file mode 100644 index 0000000000..4ebb692fb1 --- /dev/null +++ b/vendor/github.com/jtolds/gls/README.md @@ -0,0 +1,89 @@ +gls +=== + +Goroutine local storage + +### IMPORTANT NOTE ### + +It is my duty to point you to https://blog.golang.org/context, which is how +Google solves all of the problems you'd perhaps consider using this package +for at scale. + +One downside to Google's approach is that *all* of your functions must have +a new first argument, but after clearing that hurdle everything else is much +better. + +If you aren't interested in this warning, read on. + +### Huhwaht? Why? ### + +Every so often, a thread shows up on the +[golang-nuts](https://groups.google.com/d/forum/golang-nuts) asking for some +form of goroutine-local-storage, or some kind of goroutine id, or some kind of +context. There are a few valid use cases for goroutine-local-storage, one of +the most prominent being log line context. One poster was interested in being +able to log an HTTP request context id in every log line in the same goroutine +as the incoming HTTP request, without having to change every library and +function call he was interested in logging. + +This would be pretty useful. Provided that you could get some kind of +goroutine-local-storage, you could call +[log.SetOutput](http://golang.org/pkg/log/#SetOutput) with your own logging +writer that checks goroutine-local-storage for some context information and +adds that context to your log lines. + +But alas, Andrew Gerrand's typically diplomatic answer to the question of +goroutine-local variables was: + +> We wouldn't even be having this discussion if thread local storage wasn't +> useful. But every feature comes at a cost, and in my opinion the cost of +> threadlocals far outweighs their benefits. They're just not a good fit for +> Go. + +So, yeah, that makes sense. That's a pretty good reason for why the language +won't support a specific and (relatively) unuseful feature that requires some +runtime changes, just for the sake of a little bit of log improvement. + +But does Go require runtime changes? + +### How it works ### + +Go has pretty fantastic introspective and reflective features, but one thing Go +doesn't give you is any kind of access to the stack pointer, or frame pointer, +or goroutine id, or anything contextual about your current stack. It gives you +access to your list of callers, but only along with program counters, which are +fixed at compile time. + +But it does give you the stack. + +So, we define 16 special functions and embed base-16 tags into the stack using +the call order of those 16 functions. Then, we can read our tags back out of +the stack looking at the callers list. + +We then use these tags as an index into a traditional map for implementing +this library. + +### What are people saying? ### + +"Wow, that's horrifying." + +"This is the most terrible thing I have seen in a very long time." + +"Where is it getting a context from? Is this serializing all the requests? +What the heck is the client being bound to? What are these tags? Why does he +need callers? Oh god no. No no no." + +### Docs ### + +Please see the docs at http://godoc.org/github.com/jtolds/gls + +### Related ### + +If you're okay relying on the string format of the current runtime stacktrace +including a unique goroutine id (not guaranteed by the spec or anything, but +very unlikely to change within a Go release), you might be able to squeeze +out a bit more performance by using this similar library, inspired by some +code Brad Fitzpatrick wrote for debugging his HTTP/2 library: +https://github.com/tylerb/gls (in contrast, jtolds/gls doesn't require +any knowledge of the string format of the runtime stacktrace, which +probably adds unnecessary overhead). diff --git a/vendor/github.com/jtolds/gls/context.go b/vendor/github.com/jtolds/gls/context.go new file mode 100644 index 0000000000..618a171061 --- /dev/null +++ b/vendor/github.com/jtolds/gls/context.go @@ -0,0 +1,153 @@ +// Package gls implements goroutine-local storage. +package gls + +import ( + "sync" +) + +var ( + mgrRegistry = make(map[*ContextManager]bool) + mgrRegistryMtx sync.RWMutex +) + +// Values is simply a map of key types to value types. Used by SetValues to +// set multiple values at once. +type Values map[interface{}]interface{} + +// ContextManager is the main entrypoint for interacting with +// Goroutine-local-storage. You can have multiple independent ContextManagers +// at any given time. ContextManagers are usually declared globally for a given +// class of context variables. You should use NewContextManager for +// construction. +type ContextManager struct { + mtx sync.Mutex + values map[uint]Values +} + +// NewContextManager returns a brand new ContextManager. It also registers the +// new ContextManager in the ContextManager registry which is used by the Go +// method. ContextManagers are typically defined globally at package scope. +func NewContextManager() *ContextManager { + mgr := &ContextManager{values: make(map[uint]Values)} + mgrRegistryMtx.Lock() + defer mgrRegistryMtx.Unlock() + mgrRegistry[mgr] = true + return mgr +} + +// Unregister removes a ContextManager from the global registry, used by the +// Go method. Only intended for use when you're completely done with a +// ContextManager. Use of Unregister at all is rare. +func (m *ContextManager) Unregister() { + mgrRegistryMtx.Lock() + defer mgrRegistryMtx.Unlock() + delete(mgrRegistry, m) +} + +// SetValues takes a collection of values and a function to call for those +// values to be set in. Anything further down the stack will have the set +// values available through GetValue. SetValues will add new values or replace +// existing values of the same key and will not mutate or change values for +// previous stack frames. +// SetValues is slow (makes a copy of all current and new values for the new +// gls-context) in order to reduce the amount of lookups GetValue requires. +func (m *ContextManager) SetValues(new_values Values, context_call func()) { + if len(new_values) == 0 { + context_call() + return + } + + mutated_keys := make([]interface{}, 0, len(new_values)) + mutated_vals := make(Values, len(new_values)) + + EnsureGoroutineId(func(gid uint) { + m.mtx.Lock() + state, found := m.values[gid] + if !found { + state = make(Values, len(new_values)) + m.values[gid] = state + } + m.mtx.Unlock() + + for key, new_val := range new_values { + mutated_keys = append(mutated_keys, key) + if old_val, ok := state[key]; ok { + mutated_vals[key] = old_val + } + state[key] = new_val + } + + defer func() { + if !found { + m.mtx.Lock() + delete(m.values, gid) + m.mtx.Unlock() + return + } + + for _, key := range mutated_keys { + if val, ok := mutated_vals[key]; ok { + state[key] = val + } else { + delete(state, key) + } + } + }() + + context_call() + }) +} + +// GetValue will return a previously set value, provided that the value was set +// by SetValues somewhere higher up the stack. If the value is not found, ok +// will be false. +func (m *ContextManager) GetValue(key interface{}) ( + value interface{}, ok bool) { + gid, ok := GetGoroutineId() + if !ok { + return nil, false + } + + m.mtx.Lock() + state, found := m.values[gid] + m.mtx.Unlock() + + if !found { + return nil, false + } + value, ok = state[key] + return value, ok +} + +func (m *ContextManager) getValues() Values { + gid, ok := GetGoroutineId() + if !ok { + return nil + } + m.mtx.Lock() + state, _ := m.values[gid] + m.mtx.Unlock() + return state +} + +// Go preserves ContextManager values and Goroutine-local-storage across new +// goroutine invocations. The Go method makes a copy of all existing values on +// all registered context managers and makes sure they are still set after +// kicking off the provided function in a new goroutine. If you don't use this +// Go method instead of the standard 'go' keyword, you will lose values in +// ContextManagers, as goroutines have brand new stacks. +func Go(cb func()) { + mgrRegistryMtx.RLock() + defer mgrRegistryMtx.RUnlock() + + for mgr := range mgrRegistry { + values := mgr.getValues() + if len(values) > 0 { + cb = func(mgr *ContextManager, cb func()) func() { + return func() { mgr.SetValues(values, cb) } + }(mgr, cb) + } + } + + go cb() +} diff --git a/vendor/github.com/jtolds/gls/gen_sym.go b/vendor/github.com/jtolds/gls/gen_sym.go new file mode 100644 index 0000000000..7f615cce93 --- /dev/null +++ b/vendor/github.com/jtolds/gls/gen_sym.go @@ -0,0 +1,21 @@ +package gls + +import ( + "sync" +) + +var ( + keyMtx sync.Mutex + keyCounter uint64 +) + +// ContextKey is a throwaway value you can use as a key to a ContextManager +type ContextKey struct{ id uint64 } + +// GenSym will return a brand new, never-before-used ContextKey +func GenSym() ContextKey { + keyMtx.Lock() + defer keyMtx.Unlock() + keyCounter += 1 + return ContextKey{id: keyCounter} +} diff --git a/vendor/github.com/jtolds/gls/gid.go b/vendor/github.com/jtolds/gls/gid.go new file mode 100644 index 0000000000..c16bf3a554 --- /dev/null +++ b/vendor/github.com/jtolds/gls/gid.go @@ -0,0 +1,25 @@ +package gls + +var ( + stackTagPool = &idPool{} +) + +// Will return this goroutine's identifier if set. If you always need a +// goroutine identifier, you should use EnsureGoroutineId which will make one +// if there isn't one already. +func GetGoroutineId() (gid uint, ok bool) { + return readStackTag() +} + +// Will call cb with the current goroutine identifier. If one hasn't already +// been generated, one will be created and set first. The goroutine identifier +// might be invalid after cb returns. +func EnsureGoroutineId(cb func(gid uint)) { + if gid, ok := readStackTag(); ok { + cb(gid) + return + } + gid := stackTagPool.Acquire() + defer stackTagPool.Release(gid) + addStackTag(gid, func() { cb(gid) }) +} diff --git a/vendor/github.com/jtolds/gls/id_pool.go b/vendor/github.com/jtolds/gls/id_pool.go new file mode 100644 index 0000000000..b7974ae002 --- /dev/null +++ b/vendor/github.com/jtolds/gls/id_pool.go @@ -0,0 +1,34 @@ +package gls + +// though this could probably be better at keeping ids smaller, the goal of +// this class is to keep a registry of the smallest unique integer ids +// per-process possible + +import ( + "sync" +) + +type idPool struct { + mtx sync.Mutex + released []uint + max_id uint +} + +func (p *idPool) Acquire() (id uint) { + p.mtx.Lock() + defer p.mtx.Unlock() + if len(p.released) > 0 { + id = p.released[len(p.released)-1] + p.released = p.released[:len(p.released)-1] + return id + } + id = p.max_id + p.max_id++ + return id +} + +func (p *idPool) Release(id uint) { + p.mtx.Lock() + defer p.mtx.Unlock() + p.released = append(p.released, id) +} diff --git a/vendor/github.com/jtolds/gls/stack_tags.go b/vendor/github.com/jtolds/gls/stack_tags.go new file mode 100644 index 0000000000..37bbd3347a --- /dev/null +++ b/vendor/github.com/jtolds/gls/stack_tags.go @@ -0,0 +1,147 @@ +package gls + +// so, basically, we're going to encode integer tags in base-16 on the stack + +const ( + bitWidth = 4 + stackBatchSize = 16 +) + +var ( + pc_lookup = make(map[uintptr]int8, 17) + mark_lookup [16]func(uint, func()) +) + +func init() { + setEntries := func(f func(uint, func()), v int8) { + var ptr uintptr + f(0, func() { + ptr = findPtr() + }) + pc_lookup[ptr] = v + if v >= 0 { + mark_lookup[v] = f + } + } + setEntries(github_com_jtolds_gls_markS, -0x1) + setEntries(github_com_jtolds_gls_mark0, 0x0) + setEntries(github_com_jtolds_gls_mark1, 0x1) + setEntries(github_com_jtolds_gls_mark2, 0x2) + setEntries(github_com_jtolds_gls_mark3, 0x3) + setEntries(github_com_jtolds_gls_mark4, 0x4) + setEntries(github_com_jtolds_gls_mark5, 0x5) + setEntries(github_com_jtolds_gls_mark6, 0x6) + setEntries(github_com_jtolds_gls_mark7, 0x7) + setEntries(github_com_jtolds_gls_mark8, 0x8) + setEntries(github_com_jtolds_gls_mark9, 0x9) + setEntries(github_com_jtolds_gls_markA, 0xa) + setEntries(github_com_jtolds_gls_markB, 0xb) + setEntries(github_com_jtolds_gls_markC, 0xc) + setEntries(github_com_jtolds_gls_markD, 0xd) + setEntries(github_com_jtolds_gls_markE, 0xe) + setEntries(github_com_jtolds_gls_markF, 0xf) +} + +func addStackTag(tag uint, context_call func()) { + if context_call == nil { + return + } + github_com_jtolds_gls_markS(tag, context_call) +} + +// these private methods are named this horrendous name so gopherjs support +// is easier. it shouldn't add any runtime cost in non-js builds. + +//go:noinline +func github_com_jtolds_gls_markS(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_mark0(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_mark1(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_mark2(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_mark3(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_mark4(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_mark5(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_mark6(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_mark7(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_mark8(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_mark9(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_markA(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_markB(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_markC(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_markD(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_markE(tag uint, cb func()) { _m(tag, cb) } + +//go:noinline +func github_com_jtolds_gls_markF(tag uint, cb func()) { _m(tag, cb) } + +func _m(tag_remainder uint, cb func()) { + if tag_remainder == 0 { + cb() + } else { + mark_lookup[tag_remainder&0xf](tag_remainder>>bitWidth, cb) + } +} + +func readStackTag() (tag uint, ok bool) { + var current_tag uint + offset := 0 + for { + batch, next_offset := getStack(offset, stackBatchSize) + for _, pc := range batch { + val, ok := pc_lookup[pc] + if !ok { + continue + } + if val < 0 { + return current_tag, true + } + current_tag <<= bitWidth + current_tag += uint(val) + } + if next_offset == 0 { + break + } + offset = next_offset + } + return 0, false +} + +func (m *ContextManager) preventInlining() { + // dunno if findPtr or getStack are likely to get inlined in a future release + // of go, but if they are inlined and their callers are inlined, that could + // hork some things. let's do our best to explain to the compiler that we + // really don't want those two functions inlined by saying they could change + // at any time. assumes preventInlining doesn't get compiled out. + // this whole thing is probably overkill. + findPtr = m.values[0][0].(func() uintptr) + getStack = m.values[0][1].(func(int, int) ([]uintptr, int)) +} diff --git a/vendor/github.com/jtolds/gls/stack_tags_js.go b/vendor/github.com/jtolds/gls/stack_tags_js.go new file mode 100644 index 0000000000..c4e8b801d3 --- /dev/null +++ b/vendor/github.com/jtolds/gls/stack_tags_js.go @@ -0,0 +1,75 @@ +// +build js + +package gls + +// This file is used for GopherJS builds, which don't have normal runtime +// stack trace support + +import ( + "strconv" + "strings" + + "github.com/gopherjs/gopherjs/js" +) + +const ( + jsFuncNamePrefix = "github_com_jtolds_gls_mark" +) + +func jsMarkStack() (f []uintptr) { + lines := strings.Split( + js.Global.Get("Error").New().Get("stack").String(), "\n") + f = make([]uintptr, 0, len(lines)) + for i, line := range lines { + line = strings.TrimSpace(line) + if line == "" { + continue + } + if i == 0 { + if line != "Error" { + panic("didn't understand js stack trace") + } + continue + } + fields := strings.Fields(line) + if len(fields) < 2 || fields[0] != "at" { + panic("didn't understand js stack trace") + } + + pos := strings.Index(fields[1], jsFuncNamePrefix) + if pos < 0 { + continue + } + pos += len(jsFuncNamePrefix) + if pos >= len(fields[1]) { + panic("didn't understand js stack trace") + } + char := string(fields[1][pos]) + switch char { + case "S": + f = append(f, uintptr(0)) + default: + val, err := strconv.ParseUint(char, 16, 8) + if err != nil { + panic("didn't understand js stack trace") + } + f = append(f, uintptr(val)+1) + } + } + return f +} + +// variables to prevent inlining +var ( + findPtr = func() uintptr { + funcs := jsMarkStack() + if len(funcs) == 0 { + panic("failed to find function pointer") + } + return funcs[0] + } + + getStack = func(offset, amount int) (stack []uintptr, next_offset int) { + return jsMarkStack(), 0 + } +) diff --git a/vendor/github.com/jtolds/gls/stack_tags_main.go b/vendor/github.com/jtolds/gls/stack_tags_main.go new file mode 100644 index 0000000000..4da89e44f8 --- /dev/null +++ b/vendor/github.com/jtolds/gls/stack_tags_main.go @@ -0,0 +1,30 @@ +// +build !js + +package gls + +// This file is used for standard Go builds, which have the expected runtime +// support + +import ( + "runtime" +) + +var ( + findPtr = func() uintptr { + var pc [1]uintptr + n := runtime.Callers(4, pc[:]) + if n != 1 { + panic("failed to find function pointer") + } + return pc[0] + } + + getStack = func(offset, amount int) (stack []uintptr, next_offset int) { + stack = make([]uintptr, amount) + stack = stack[:runtime.Callers(offset, stack)] + if len(stack) < amount { + return stack, 0 + } + return stack, offset + len(stack) + } +) diff --git a/vendor/github.com/smartystreets/assertions/.gitignore b/vendor/github.com/smartystreets/assertions/.gitignore new file mode 100644 index 0000000000..07d3c71ce2 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/.gitignore @@ -0,0 +1,5 @@ +.DS_Store +Thumbs.db +*.iml +/.idea +coverage.out diff --git a/vendor/github.com/smartystreets/assertions/.travis.yml b/vendor/github.com/smartystreets/assertions/.travis.yml new file mode 100644 index 0000000000..72df752f82 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/.travis.yml @@ -0,0 +1,11 @@ +language: go + +go: + - 1.x + +install: + - go get -t ./... + +script: go test ./... -v + +sudo: false diff --git a/vendor/github.com/smartystreets/assertions/CONTRIBUTING.md b/vendor/github.com/smartystreets/assertions/CONTRIBUTING.md new file mode 100644 index 0000000000..1820ecb331 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/CONTRIBUTING.md @@ -0,0 +1,12 @@ +# Contributing + +In general, the code posted to the [SmartyStreets github organization](https://github.com/smartystreets) is created to solve specific problems at SmartyStreets that are ancillary to our core products in the address verification industry and may or may not be useful to other organizations or developers. Our reason for posting said code isn't necessarily to solicit feedback or contributions from the community but more as a showcase of some of the approaches to solving problems we have adopted. + +Having stated that, we do consider issues raised by other githubbers as well as contributions submitted via pull requests. When submitting such a pull request, please follow these guidelines: + +- _Look before you leap:_ If the changes you plan to make are significant, it's in everyone's best interest for you to discuss them with a SmartyStreets team member prior to opening a pull request. +- _License and ownership:_ If modifying the `LICENSE.md` file, limit your changes to fixing typographical mistakes. Do NOT modify the actual terms in the license or the copyright by **SmartyStreets, LLC**. Code submitted to SmartyStreets projects becomes property of SmartyStreets and must be compatible with the associated license. +- _Testing:_ If the code you are submitting resides in packages/modules covered by automated tests, be sure to add passing tests that cover your changes and assert expected behavior and state. Submit the additional test cases as part of your change set. +- _Style:_ Match your approach to **naming** and **formatting** with the surrounding code. Basically, the code you submit shouldn't stand out. + - "Naming" refers to such constructs as variables, methods, functions, classes, structs, interfaces, packages, modules, directories, files, etc... + - "Formatting" refers to such constructs as whitespace, horizontal line length, vertical function length, vertical file length, indentation, curly braces, etc... diff --git a/vendor/github.com/smartystreets/assertions/LICENSE.md b/vendor/github.com/smartystreets/assertions/LICENSE.md new file mode 100644 index 0000000000..8ea6f94552 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/LICENSE.md @@ -0,0 +1,23 @@ +Copyright (c) 2016 SmartyStreets, LLC + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +NOTE: Various optional and subordinate components carry their own licensing +requirements and restrictions. Use of those components is subject to the terms +and conditions outlined the respective license of each component. diff --git a/vendor/github.com/smartystreets/assertions/README.md b/vendor/github.com/smartystreets/assertions/README.md new file mode 100644 index 0000000000..58e51e903f --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/README.md @@ -0,0 +1,611 @@ +# assertions +-- + import "github.com/smartystreets/assertions" + +Package assertions contains the implementations for all assertions which are +referenced in goconvey's `convey` package +(github.com/smartystreets/goconvey/convey) and gunit +(github.com/smartystreets/gunit) for use with the So(...) method. They can also +be used in traditional Go test functions and even in applications. + +https://smartystreets.com + +Many of the assertions lean heavily on work done by Aaron Jacobs in his +excellent oglematchers library. (https://github.com/jacobsa/oglematchers) The +ShouldResemble assertion leans heavily on work done by Daniel Jacques in his +very helpful go-render library. (https://github.com/luci/go-render) + +## Usage + +#### func GoConveyMode + +```go +func GoConveyMode(yes bool) +``` +GoConveyMode provides control over JSON serialization of failures. When using +the assertions in this package from the convey package JSON results are very +helpful and can be rendered in a DIFF view. In that case, this function will be +called with a true value to enable the JSON serialization. By default, the +assertions in this package will not serializer a JSON result, making standalone +usage more convenient. + +#### func ShouldAlmostEqual + +```go +func ShouldAlmostEqual(actual interface{}, expected ...interface{}) string +``` +ShouldAlmostEqual makes sure that two parameters are close enough to being +equal. The acceptable delta may be specified with a third argument, or a very +small default delta will be used. + +#### func ShouldBeBetween + +```go +func ShouldBeBetween(actual interface{}, expected ...interface{}) string +``` +ShouldBeBetween receives exactly three parameters: an actual value, a lower +bound, and an upper bound. It ensures that the actual value is between both +bounds (but not equal to either of them). + +#### func ShouldBeBetweenOrEqual + +```go +func ShouldBeBetweenOrEqual(actual interface{}, expected ...interface{}) string +``` +ShouldBeBetweenOrEqual receives exactly three parameters: an actual value, a +lower bound, and an upper bound. It ensures that the actual value is between +both bounds or equal to one of them. + +#### func ShouldBeBlank + +```go +func ShouldBeBlank(actual interface{}, expected ...interface{}) string +``` +ShouldBeBlank receives exactly 1 string parameter and ensures that it is equal +to "". + +#### func ShouldBeChronological + +```go +func ShouldBeChronological(actual interface{}, expected ...interface{}) string +``` +ShouldBeChronological receives a []time.Time slice and asserts that the are in +chronological order starting with the first time.Time as the earliest. + +#### func ShouldBeEmpty + +```go +func ShouldBeEmpty(actual interface{}, expected ...interface{}) string +``` +ShouldBeEmpty receives a single parameter (actual) and determines whether or not +calling len(actual) would return `0`. It obeys the rules specified by the len +function for determining length: http://golang.org/pkg/builtin/#len + +#### func ShouldBeError + +```go +func ShouldBeError(actual interface{}, expected ...interface{}) string +``` +ShouldBeError asserts that the first argument implements the error interface. It +also compares the first argument against the second argument if provided (which +must be an error message string or another error value). + +#### func ShouldBeFalse + +```go +func ShouldBeFalse(actual interface{}, expected ...interface{}) string +``` +ShouldBeFalse receives a single parameter and ensures that it is false. + +#### func ShouldBeGreaterThan + +```go +func ShouldBeGreaterThan(actual interface{}, expected ...interface{}) string +``` +ShouldBeGreaterThan receives exactly two parameters and ensures that the first +is greater than the second. + +#### func ShouldBeGreaterThanOrEqualTo + +```go +func ShouldBeGreaterThanOrEqualTo(actual interface{}, expected ...interface{}) string +``` +ShouldBeGreaterThanOrEqualTo receives exactly two parameters and ensures that +the first is greater than or equal to the second. + +#### func ShouldBeIn + +```go +func ShouldBeIn(actual interface{}, expected ...interface{}) string +``` +ShouldBeIn receives at least 2 parameters. The first is a proposed member of the +collection that is passed in either as the second parameter, or of the +collection that is comprised of all the remaining parameters. This assertion +ensures that the proposed member is in the collection (using ShouldEqual). + +#### func ShouldBeLessThan + +```go +func ShouldBeLessThan(actual interface{}, expected ...interface{}) string +``` +ShouldBeLessThan receives exactly two parameters and ensures that the first is +less than the second. + +#### func ShouldBeLessThanOrEqualTo + +```go +func ShouldBeLessThanOrEqualTo(actual interface{}, expected ...interface{}) string +``` +ShouldBeLessThan receives exactly two parameters and ensures that the first is +less than or equal to the second. + +#### func ShouldBeNil + +```go +func ShouldBeNil(actual interface{}, expected ...interface{}) string +``` +ShouldBeNil receives a single parameter and ensures that it is nil. + +#### func ShouldBeTrue + +```go +func ShouldBeTrue(actual interface{}, expected ...interface{}) string +``` +ShouldBeTrue receives a single parameter and ensures that it is true. + +#### func ShouldBeZeroValue + +```go +func ShouldBeZeroValue(actual interface{}, expected ...interface{}) string +``` +ShouldBeZeroValue receives a single parameter and ensures that it is the Go +equivalent of the default value, or "zero" value. + +#### func ShouldContain + +```go +func ShouldContain(actual interface{}, expected ...interface{}) string +``` +ShouldContain receives exactly two parameters. The first is a slice and the +second is a proposed member. Membership is determined using ShouldEqual. + +#### func ShouldContainKey + +```go +func ShouldContainKey(actual interface{}, expected ...interface{}) string +``` +ShouldContainKey receives exactly two parameters. The first is a map and the +second is a proposed key. Keys are compared with a simple '=='. + +#### func ShouldContainSubstring + +```go +func ShouldContainSubstring(actual interface{}, expected ...interface{}) string +``` +ShouldContainSubstring receives exactly 2 string parameters and ensures that the +first contains the second as a substring. + +#### func ShouldEndWith + +```go +func ShouldEndWith(actual interface{}, expected ...interface{}) string +``` +ShouldEndWith receives exactly 2 string parameters and ensures that the first +ends with the second. + +#### func ShouldEqual + +```go +func ShouldEqual(actual interface{}, expected ...interface{}) string +``` +ShouldEqual receives exactly two parameters and does an equality check using the +following semantics: 1. If the expected and actual values implement an Equal +method in the form `func (this T) Equal(that T) bool` then call the method. If +true, they are equal. 2. The expected and actual values are judged equal or not +by oglematchers.Equals. + +#### func ShouldEqualJSON + +```go +func ShouldEqualJSON(actual interface{}, expected ...interface{}) string +``` +ShouldEqualJSON receives exactly two parameters and does an equality check by +marshalling to JSON + +#### func ShouldEqualTrimSpace + +```go +func ShouldEqualTrimSpace(actual interface{}, expected ...interface{}) string +``` +ShouldEqualTrimSpace receives exactly 2 string parameters and ensures that the +first is equal to the second after removing all leading and trailing whitespace +using strings.TrimSpace(first). + +#### func ShouldEqualWithout + +```go +func ShouldEqualWithout(actual interface{}, expected ...interface{}) string +``` +ShouldEqualWithout receives exactly 3 string parameters and ensures that the +first is equal to the second after removing all instances of the third from the +first using strings.Replace(first, third, "", -1). + +#### func ShouldHappenAfter + +```go +func ShouldHappenAfter(actual interface{}, expected ...interface{}) string +``` +ShouldHappenAfter receives exactly 2 time.Time arguments and asserts that the +first happens after the second. + +#### func ShouldHappenBefore + +```go +func ShouldHappenBefore(actual interface{}, expected ...interface{}) string +``` +ShouldHappenBefore receives exactly 2 time.Time arguments and asserts that the +first happens before the second. + +#### func ShouldHappenBetween + +```go +func ShouldHappenBetween(actual interface{}, expected ...interface{}) string +``` +ShouldHappenBetween receives exactly 3 time.Time arguments and asserts that the +first happens between (not on) the second and third. + +#### func ShouldHappenOnOrAfter + +```go +func ShouldHappenOnOrAfter(actual interface{}, expected ...interface{}) string +``` +ShouldHappenOnOrAfter receives exactly 2 time.Time arguments and asserts that +the first happens on or after the second. + +#### func ShouldHappenOnOrBefore + +```go +func ShouldHappenOnOrBefore(actual interface{}, expected ...interface{}) string +``` +ShouldHappenOnOrBefore receives exactly 2 time.Time arguments and asserts that +the first happens on or before the second. + +#### func ShouldHappenOnOrBetween + +```go +func ShouldHappenOnOrBetween(actual interface{}, expected ...interface{}) string +``` +ShouldHappenOnOrBetween receives exactly 3 time.Time arguments and asserts that +the first happens between or on the second and third. + +#### func ShouldHappenWithin + +```go +func ShouldHappenWithin(actual interface{}, expected ...interface{}) string +``` +ShouldHappenWithin receives a time.Time, a time.Duration, and a time.Time (3 +arguments) and asserts that the first time.Time happens within or on the +duration specified relative to the other time.Time. + +#### func ShouldHaveLength + +```go +func ShouldHaveLength(actual interface{}, expected ...interface{}) string +``` +ShouldHaveLength receives 2 parameters. The first is a collection to check the +length of, the second being the expected length. It obeys the rules specified by +the len function for determining length: http://golang.org/pkg/builtin/#len + +#### func ShouldHaveSameTypeAs + +```go +func ShouldHaveSameTypeAs(actual interface{}, expected ...interface{}) string +``` +ShouldHaveSameTypeAs receives exactly two parameters and compares their +underlying types for equality. + +#### func ShouldImplement + +```go +func ShouldImplement(actual interface{}, expectedList ...interface{}) string +``` +ShouldImplement receives exactly two parameters and ensures that the first +implements the interface type of the second. + +#### func ShouldNotAlmostEqual + +```go +func ShouldNotAlmostEqual(actual interface{}, expected ...interface{}) string +``` +ShouldNotAlmostEqual is the inverse of ShouldAlmostEqual + +#### func ShouldNotBeBetween + +```go +func ShouldNotBeBetween(actual interface{}, expected ...interface{}) string +``` +ShouldNotBeBetween receives exactly three parameters: an actual value, a lower +bound, and an upper bound. It ensures that the actual value is NOT between both +bounds. + +#### func ShouldNotBeBetweenOrEqual + +```go +func ShouldNotBeBetweenOrEqual(actual interface{}, expected ...interface{}) string +``` +ShouldNotBeBetweenOrEqual receives exactly three parameters: an actual value, a +lower bound, and an upper bound. It ensures that the actual value is nopt +between the bounds nor equal to either of them. + +#### func ShouldNotBeBlank + +```go +func ShouldNotBeBlank(actual interface{}, expected ...interface{}) string +``` +ShouldNotBeBlank receives exactly 1 string parameter and ensures that it is +equal to "". + +#### func ShouldNotBeEmpty + +```go +func ShouldNotBeEmpty(actual interface{}, expected ...interface{}) string +``` +ShouldNotBeEmpty receives a single parameter (actual) and determines whether or +not calling len(actual) would return a value greater than zero. It obeys the +rules specified by the `len` function for determining length: +http://golang.org/pkg/builtin/#len + +#### func ShouldNotBeIn + +```go +func ShouldNotBeIn(actual interface{}, expected ...interface{}) string +``` +ShouldNotBeIn receives at least 2 parameters. The first is a proposed member of +the collection that is passed in either as the second parameter, or of the +collection that is comprised of all the remaining parameters. This assertion +ensures that the proposed member is NOT in the collection (using ShouldEqual). + +#### func ShouldNotBeNil + +```go +func ShouldNotBeNil(actual interface{}, expected ...interface{}) string +``` +ShouldNotBeNil receives a single parameter and ensures that it is not nil. + +#### func ShouldNotBeZeroValue + +```go +func ShouldNotBeZeroValue(actual interface{}, expected ...interface{}) string +``` +ShouldBeZeroValue receives a single parameter and ensures that it is NOT the Go +equivalent of the default value, or "zero" value. + +#### func ShouldNotContain + +```go +func ShouldNotContain(actual interface{}, expected ...interface{}) string +``` +ShouldNotContain receives exactly two parameters. The first is a slice and the +second is a proposed member. Membership is determinied using ShouldEqual. + +#### func ShouldNotContainKey + +```go +func ShouldNotContainKey(actual interface{}, expected ...interface{}) string +``` +ShouldNotContainKey receives exactly two parameters. The first is a map and the +second is a proposed absent key. Keys are compared with a simple '=='. + +#### func ShouldNotContainSubstring + +```go +func ShouldNotContainSubstring(actual interface{}, expected ...interface{}) string +``` +ShouldNotContainSubstring receives exactly 2 string parameters and ensures that +the first does NOT contain the second as a substring. + +#### func ShouldNotEndWith + +```go +func ShouldNotEndWith(actual interface{}, expected ...interface{}) string +``` +ShouldEndWith receives exactly 2 string parameters and ensures that the first +does not end with the second. + +#### func ShouldNotEqual + +```go +func ShouldNotEqual(actual interface{}, expected ...interface{}) string +``` +ShouldNotEqual receives exactly two parameters and does an inequality check. See +ShouldEqual for details on how equality is determined. + +#### func ShouldNotHappenOnOrBetween + +```go +func ShouldNotHappenOnOrBetween(actual interface{}, expected ...interface{}) string +``` +ShouldNotHappenOnOrBetween receives exactly 3 time.Time arguments and asserts +that the first does NOT happen between or on the second or third. + +#### func ShouldNotHappenWithin + +```go +func ShouldNotHappenWithin(actual interface{}, expected ...interface{}) string +``` +ShouldNotHappenWithin receives a time.Time, a time.Duration, and a time.Time (3 +arguments) and asserts that the first time.Time does NOT happen within or on the +duration specified relative to the other time.Time. + +#### func ShouldNotHaveSameTypeAs + +```go +func ShouldNotHaveSameTypeAs(actual interface{}, expected ...interface{}) string +``` +ShouldNotHaveSameTypeAs receives exactly two parameters and compares their +underlying types for inequality. + +#### func ShouldNotImplement + +```go +func ShouldNotImplement(actual interface{}, expectedList ...interface{}) string +``` +ShouldNotImplement receives exactly two parameters and ensures that the first +does NOT implement the interface type of the second. + +#### func ShouldNotPanic + +```go +func ShouldNotPanic(actual interface{}, expected ...interface{}) (message string) +``` +ShouldNotPanic receives a void, niladic function and expects to execute the +function without any panic. + +#### func ShouldNotPanicWith + +```go +func ShouldNotPanicWith(actual interface{}, expected ...interface{}) (message string) +``` +ShouldNotPanicWith receives a void, niladic function and expects to recover a +panic whose content differs from the second argument. + +#### func ShouldNotPointTo + +```go +func ShouldNotPointTo(actual interface{}, expected ...interface{}) string +``` +ShouldNotPointTo receives exactly two parameters and checks to see that they +point to different addresess. + +#### func ShouldNotResemble + +```go +func ShouldNotResemble(actual interface{}, expected ...interface{}) string +``` +ShouldNotResemble receives exactly two parameters and does an inverse deep equal +check (see reflect.DeepEqual) + +#### func ShouldNotStartWith + +```go +func ShouldNotStartWith(actual interface{}, expected ...interface{}) string +``` +ShouldNotStartWith receives exactly 2 string parameters and ensures that the +first does not start with the second. + +#### func ShouldPanic + +```go +func ShouldPanic(actual interface{}, expected ...interface{}) (message string) +``` +ShouldPanic receives a void, niladic function and expects to recover a panic. + +#### func ShouldPanicWith + +```go +func ShouldPanicWith(actual interface{}, expected ...interface{}) (message string) +``` +ShouldPanicWith receives a void, niladic function and expects to recover a panic +with the second argument as the content. + +#### func ShouldPointTo + +```go +func ShouldPointTo(actual interface{}, expected ...interface{}) string +``` +ShouldPointTo receives exactly two parameters and checks to see that they point +to the same address. + +#### func ShouldResemble + +```go +func ShouldResemble(actual interface{}, expected ...interface{}) string +``` +ShouldResemble receives exactly two parameters and does a deep equal check (see +reflect.DeepEqual) + +#### func ShouldStartWith + +```go +func ShouldStartWith(actual interface{}, expected ...interface{}) string +``` +ShouldStartWith receives exactly 2 string parameters and ensures that the first +starts with the second. + +#### func So + +```go +func So(actual interface{}, assert assertion, expected ...interface{}) (bool, string) +``` +So is a convenience function (as opposed to an inconvenience function?) for +running assertions on arbitrary arguments in any context, be it for testing or +even application logging. It allows you to perform assertion-like behavior (and +get nicely formatted messages detailing discrepancies) but without the program +blowing up or panicking. All that is required is to import this package and call +`So` with one of the assertions exported by this package as the second +parameter. The first return parameter is a boolean indicating if the assertion +was true. The second return parameter is the well-formatted message showing why +an assertion was incorrect, or blank if the assertion was correct. + +Example: + + if ok, message := So(x, ShouldBeGreaterThan, y); !ok { + log.Println(message) + } + +For an alternative implementation of So (that provides more flexible return +options) see the `So` function in the package at +github.com/smartystreets/assertions/assert. + +#### type Assertion + +```go +type Assertion struct { +} +``` + + +#### func New + +```go +func New(t testingT) *Assertion +``` +New swallows the *testing.T struct and prints failed assertions using t.Error. +Example: assertions.New(t).So(1, should.Equal, 1) + +#### func (*Assertion) Failed + +```go +func (this *Assertion) Failed() bool +``` +Failed reports whether any calls to So (on this Assertion instance) have failed. + +#### func (*Assertion) So + +```go +func (this *Assertion) So(actual interface{}, assert assertion, expected ...interface{}) bool +``` +So calls the standalone So function and additionally, calls t.Error in failure +scenarios. + +#### type FailureView + +```go +type FailureView struct { + Message string `json:"Message"` + Expected string `json:"Expected"` + Actual string `json:"Actual"` +} +``` + +This struct is also declared in +github.com/smartystreets/goconvey/convey/reporting. The json struct tags should +be equal in both declarations. + +#### type Serializer + +```go +type Serializer interface { + // contains filtered or unexported methods +} +``` diff --git a/vendor/github.com/smartystreets/assertions/collections.go b/vendor/github.com/smartystreets/assertions/collections.go new file mode 100644 index 0000000000..b534d4bafa --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/collections.go @@ -0,0 +1,244 @@ +package assertions + +import ( + "fmt" + "reflect" + + "github.com/smartystreets/assertions/internal/oglematchers" +) + +// ShouldContain receives exactly two parameters. The first is a slice and the +// second is a proposed member. Membership is determined using ShouldEqual. +func ShouldContain(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + if matchError := oglematchers.Contains(expected[0]).Matches(actual); matchError != nil { + typeName := reflect.TypeOf(actual) + + if fmt.Sprintf("%v", matchError) == "which is not a slice or array" { + return fmt.Sprintf(shouldHaveBeenAValidCollection, typeName) + } + return fmt.Sprintf(shouldHaveContained, typeName, expected[0]) + } + return success +} + +// ShouldNotContain receives exactly two parameters. The first is a slice and the +// second is a proposed member. Membership is determinied using ShouldEqual. +func ShouldNotContain(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + typeName := reflect.TypeOf(actual) + + if matchError := oglematchers.Contains(expected[0]).Matches(actual); matchError != nil { + if fmt.Sprintf("%v", matchError) == "which is not a slice or array" { + return fmt.Sprintf(shouldHaveBeenAValidCollection, typeName) + } + return success + } + return fmt.Sprintf(shouldNotHaveContained, typeName, expected[0]) +} + +// ShouldContainKey receives exactly two parameters. The first is a map and the +// second is a proposed key. Keys are compared with a simple '=='. +func ShouldContainKey(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + keys, isMap := mapKeys(actual) + if !isMap { + return fmt.Sprintf(shouldHaveBeenAValidMap, reflect.TypeOf(actual)) + } + + if !keyFound(keys, expected[0]) { + return fmt.Sprintf(shouldHaveContainedKey, reflect.TypeOf(actual), expected) + } + + return "" +} + +// ShouldNotContainKey receives exactly two parameters. The first is a map and the +// second is a proposed absent key. Keys are compared with a simple '=='. +func ShouldNotContainKey(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + keys, isMap := mapKeys(actual) + if !isMap { + return fmt.Sprintf(shouldHaveBeenAValidMap, reflect.TypeOf(actual)) + } + + if keyFound(keys, expected[0]) { + return fmt.Sprintf(shouldNotHaveContainedKey, reflect.TypeOf(actual), expected) + } + + return "" +} + +func mapKeys(m interface{}) ([]reflect.Value, bool) { + value := reflect.ValueOf(m) + if value.Kind() != reflect.Map { + return nil, false + } + return value.MapKeys(), true +} +func keyFound(keys []reflect.Value, expectedKey interface{}) bool { + found := false + for _, key := range keys { + if key.Interface() == expectedKey { + found = true + } + } + return found +} + +// ShouldBeIn receives at least 2 parameters. The first is a proposed member of the collection +// that is passed in either as the second parameter, or of the collection that is comprised +// of all the remaining parameters. This assertion ensures that the proposed member is in +// the collection (using ShouldEqual). +func ShouldBeIn(actual interface{}, expected ...interface{}) string { + if fail := atLeast(1, expected); fail != success { + return fail + } + + if len(expected) == 1 { + return shouldBeIn(actual, expected[0]) + } + return shouldBeIn(actual, expected) +} +func shouldBeIn(actual interface{}, expected interface{}) string { + if matchError := oglematchers.Contains(actual).Matches(expected); matchError != nil { + return fmt.Sprintf(shouldHaveBeenIn, actual, reflect.TypeOf(expected)) + } + return success +} + +// ShouldNotBeIn receives at least 2 parameters. The first is a proposed member of the collection +// that is passed in either as the second parameter, or of the collection that is comprised +// of all the remaining parameters. This assertion ensures that the proposed member is NOT in +// the collection (using ShouldEqual). +func ShouldNotBeIn(actual interface{}, expected ...interface{}) string { + if fail := atLeast(1, expected); fail != success { + return fail + } + + if len(expected) == 1 { + return shouldNotBeIn(actual, expected[0]) + } + return shouldNotBeIn(actual, expected) +} +func shouldNotBeIn(actual interface{}, expected interface{}) string { + if matchError := oglematchers.Contains(actual).Matches(expected); matchError == nil { + return fmt.Sprintf(shouldNotHaveBeenIn, actual, reflect.TypeOf(expected)) + } + return success +} + +// ShouldBeEmpty receives a single parameter (actual) and determines whether or not +// calling len(actual) would return `0`. It obeys the rules specified by the len +// function for determining length: http://golang.org/pkg/builtin/#len +func ShouldBeEmpty(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } + + if actual == nil { + return success + } + + value := reflect.ValueOf(actual) + switch value.Kind() { + case reflect.Slice: + if value.Len() == 0 { + return success + } + case reflect.Chan: + if value.Len() == 0 { + return success + } + case reflect.Map: + if value.Len() == 0 { + return success + } + case reflect.String: + if value.Len() == 0 { + return success + } + case reflect.Ptr: + elem := value.Elem() + kind := elem.Kind() + if (kind == reflect.Slice || kind == reflect.Array) && elem.Len() == 0 { + return success + } + } + + return fmt.Sprintf(shouldHaveBeenEmpty, actual) +} + +// ShouldNotBeEmpty receives a single parameter (actual) and determines whether or not +// calling len(actual) would return a value greater than zero. It obeys the rules +// specified by the `len` function for determining length: http://golang.org/pkg/builtin/#len +func ShouldNotBeEmpty(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } + + if empty := ShouldBeEmpty(actual, expected...); empty != success { + return success + } + return fmt.Sprintf(shouldNotHaveBeenEmpty, actual) +} + +// ShouldHaveLength receives 2 parameters. The first is a collection to check +// the length of, the second being the expected length. It obeys the rules +// specified by the len function for determining length: +// http://golang.org/pkg/builtin/#len +func ShouldHaveLength(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + var expectedLen int64 + lenValue := reflect.ValueOf(expected[0]) + switch lenValue.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + expectedLen = lenValue.Int() + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + expectedLen = int64(lenValue.Uint()) + default: + return fmt.Sprintf(shouldHaveBeenAValidInteger, reflect.TypeOf(expected[0])) + } + + if expectedLen < 0 { + return fmt.Sprintf(shouldHaveBeenAValidLength, expected[0]) + } + + value := reflect.ValueOf(actual) + switch value.Kind() { + case reflect.Slice, + reflect.Chan, + reflect.Map, + reflect.String: + if int64(value.Len()) == expectedLen { + return success + } else { + return fmt.Sprintf(shouldHaveHadLength, expectedLen, value.Len(), actual) + } + case reflect.Ptr: + elem := value.Elem() + kind := elem.Kind() + if kind == reflect.Slice || kind == reflect.Array { + if int64(elem.Len()) == expectedLen { + return success + } else { + return fmt.Sprintf(shouldHaveHadLength, expectedLen, elem.Len(), actual) + } + } + } + return fmt.Sprintf(shouldHaveBeenAValidCollection, reflect.TypeOf(actual)) +} diff --git a/vendor/github.com/smartystreets/assertions/doc.go b/vendor/github.com/smartystreets/assertions/doc.go new file mode 100644 index 0000000000..ba30a9261a --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/doc.go @@ -0,0 +1,109 @@ +// Package assertions contains the implementations for all assertions which +// are referenced in goconvey's `convey` package +// (github.com/smartystreets/goconvey/convey) and gunit (github.com/smartystreets/gunit) +// for use with the So(...) method. +// They can also be used in traditional Go test functions and even in +// applications. +// +// https://smartystreets.com +// +// Many of the assertions lean heavily on work done by Aaron Jacobs in his excellent oglematchers library. +// (https://github.com/jacobsa/oglematchers) +// The ShouldResemble assertion leans heavily on work done by Daniel Jacques in his very helpful go-render library. +// (https://github.com/luci/go-render) +package assertions + +import ( + "fmt" + "runtime" +) + +// By default we use a no-op serializer. The actual Serializer provides a JSON +// representation of failure results on selected assertions so the goconvey +// web UI can display a convenient diff. +var serializer Serializer = new(noopSerializer) + +// GoConveyMode provides control over JSON serialization of failures. When +// using the assertions in this package from the convey package JSON results +// are very helpful and can be rendered in a DIFF view. In that case, this function +// will be called with a true value to enable the JSON serialization. By default, +// the assertions in this package will not serializer a JSON result, making +// standalone usage more convenient. +func GoConveyMode(yes bool) { + if yes { + serializer = newSerializer() + } else { + serializer = new(noopSerializer) + } +} + +type testingT interface { + Error(args ...interface{}) +} + +type Assertion struct { + t testingT + failed bool +} + +// New swallows the *testing.T struct and prints failed assertions using t.Error. +// Example: assertions.New(t).So(1, should.Equal, 1) +func New(t testingT) *Assertion { + return &Assertion{t: t} +} + +// Failed reports whether any calls to So (on this Assertion instance) have failed. +func (this *Assertion) Failed() bool { + return this.failed +} + +// So calls the standalone So function and additionally, calls t.Error in failure scenarios. +func (this *Assertion) So(actual interface{}, assert assertion, expected ...interface{}) bool { + ok, result := So(actual, assert, expected...) + if !ok { + this.failed = true + _, file, line, _ := runtime.Caller(1) + this.t.Error(fmt.Sprintf("\n%s:%d\n%s", file, line, result)) + } + return ok +} + +// So is a convenience function (as opposed to an inconvenience function?) +// for running assertions on arbitrary arguments in any context, be it for testing or even +// application logging. It allows you to perform assertion-like behavior (and get nicely +// formatted messages detailing discrepancies) but without the program blowing up or panicking. +// All that is required is to import this package and call `So` with one of the assertions +// exported by this package as the second parameter. +// The first return parameter is a boolean indicating if the assertion was true. The second +// return parameter is the well-formatted message showing why an assertion was incorrect, or +// blank if the assertion was correct. +// +// Example: +// +// if ok, message := So(x, ShouldBeGreaterThan, y); !ok { +// log.Println(message) +// } +// +// For an alternative implementation of So (that provides more flexible return options) +// see the `So` function in the package at github.com/smartystreets/assertions/assert. +func So(actual interface{}, assert assertion, expected ...interface{}) (bool, string) { + if result := so(actual, assert, expected...); len(result) == 0 { + return true, result + } else { + return false, result + } +} + +// so is like So, except that it only returns the string message, which is blank if the +// assertion passed. Used to facilitate testing. +func so(actual interface{}, assert func(interface{}, ...interface{}) string, expected ...interface{}) string { + return assert(actual, expected...) +} + +// assertion is an alias for a function with a signature that the So() +// function can handle. Any future or custom assertions should conform to this +// method signature. The return value should be an empty string if the assertion +// passes and a well-formed failure message if not. +type assertion func(actual interface{}, expected ...interface{}) string + +//////////////////////////////////////////////////////////////////////////// diff --git a/vendor/github.com/smartystreets/assertions/equal_method.go b/vendor/github.com/smartystreets/assertions/equal_method.go new file mode 100644 index 0000000000..c4fc38fab5 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/equal_method.go @@ -0,0 +1,75 @@ +package assertions + +import "reflect" + +type equalityMethodSpecification struct { + a interface{} + b interface{} + + aType reflect.Type + bType reflect.Type + + equalMethod reflect.Value +} + +func newEqualityMethodSpecification(a, b interface{}) *equalityMethodSpecification { + return &equalityMethodSpecification{ + a: a, + b: b, + } +} + +func (this *equalityMethodSpecification) IsSatisfied() bool { + if !this.bothAreSameType() { + return false + } + if !this.typeHasEqualMethod() { + return false + } + if !this.equalMethodReceivesSameTypeForComparison() { + return false + } + if !this.equalMethodReturnsBool() { + return false + } + return true +} + +func (this *equalityMethodSpecification) bothAreSameType() bool { + this.aType = reflect.TypeOf(this.a) + if this.aType == nil { + return false + } + if this.aType.Kind() == reflect.Ptr { + this.aType = this.aType.Elem() + } + this.bType = reflect.TypeOf(this.b) + return this.aType == this.bType +} +func (this *equalityMethodSpecification) typeHasEqualMethod() bool { + aInstance := reflect.ValueOf(this.a) + this.equalMethod = aInstance.MethodByName("Equal") + return this.equalMethod != reflect.Value{} +} + +func (this *equalityMethodSpecification) equalMethodReceivesSameTypeForComparison() bool { + signature := this.equalMethod.Type() + return signature.NumIn() == 1 && signature.In(0) == this.aType +} + +func (this *equalityMethodSpecification) equalMethodReturnsBool() bool { + signature := this.equalMethod.Type() + return signature.NumOut() == 1 && signature.Out(0) == reflect.TypeOf(true) +} + +func (this *equalityMethodSpecification) AreEqual() bool { + a := reflect.ValueOf(this.a) + b := reflect.ValueOf(this.b) + return areEqual(a, b) && areEqual(b, a) +} +func areEqual(receiver reflect.Value, argument reflect.Value) bool { + equalMethod := receiver.MethodByName("Equal") + argumentList := []reflect.Value{argument} + result := equalMethod.Call(argumentList) + return result[0].Bool() +} diff --git a/vendor/github.com/smartystreets/assertions/equality.go b/vendor/github.com/smartystreets/assertions/equality.go new file mode 100644 index 0000000000..6c10d24d90 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/equality.go @@ -0,0 +1,328 @@ +package assertions + +import ( + "encoding/json" + "errors" + "fmt" + "math" + "reflect" + "strings" + + "github.com/smartystreets/assertions/internal/go-render/render" + "github.com/smartystreets/assertions/internal/oglematchers" +) + +// ShouldEqual receives exactly two parameters and does an equality check +// using the following semantics: +// 1. If the expected and actual values implement an Equal method in the form +// `func (this T) Equal(that T) bool` then call the method. If true, they are equal. +// 2. The expected and actual values are judged equal or not by oglematchers.Equals. +func ShouldEqual(actual interface{}, expected ...interface{}) string { + if message := need(1, expected); message != success { + return message + } + return shouldEqual(actual, expected[0]) +} +func shouldEqual(actual, expected interface{}) (message string) { + defer func() { + if r := recover(); r != nil { + message = serializer.serialize(expected, actual, fmt.Sprintf(shouldHaveBeenEqual, expected, actual)) + } + }() + + if specification := newEqualityMethodSpecification(expected, actual); specification.IsSatisfied() { + if specification.AreEqual() { + return success + } else { + message = fmt.Sprintf(shouldHaveBeenEqual, expected, actual) + return serializer.serialize(expected, actual, message) + } + } + if matchError := oglematchers.Equals(expected).Matches(actual); matchError != nil { + expectedSyntax := fmt.Sprintf("%v", expected) + actualSyntax := fmt.Sprintf("%v", actual) + if expectedSyntax == actualSyntax && reflect.TypeOf(expected) != reflect.TypeOf(actual) { + message = fmt.Sprintf(shouldHaveBeenEqualTypeMismatch, expected, expected, actual, actual) + } else { + message = fmt.Sprintf(shouldHaveBeenEqual, expected, actual) + } + return serializer.serialize(expected, actual, message) + } + + return success +} + +// ShouldNotEqual receives exactly two parameters and does an inequality check. +// See ShouldEqual for details on how equality is determined. +func ShouldNotEqual(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } else if ShouldEqual(actual, expected[0]) == success { + return fmt.Sprintf(shouldNotHaveBeenEqual, actual, expected[0]) + } + return success +} + +// ShouldAlmostEqual makes sure that two parameters are close enough to being equal. +// The acceptable delta may be specified with a third argument, +// or a very small default delta will be used. +func ShouldAlmostEqual(actual interface{}, expected ...interface{}) string { + actualFloat, expectedFloat, deltaFloat, err := cleanAlmostEqualInput(actual, expected...) + + if err != "" { + return err + } + + if math.Abs(actualFloat-expectedFloat) <= deltaFloat { + return success + } else { + return fmt.Sprintf(shouldHaveBeenAlmostEqual, actualFloat, expectedFloat) + } +} + +// ShouldNotAlmostEqual is the inverse of ShouldAlmostEqual +func ShouldNotAlmostEqual(actual interface{}, expected ...interface{}) string { + actualFloat, expectedFloat, deltaFloat, err := cleanAlmostEqualInput(actual, expected...) + + if err != "" { + return err + } + + if math.Abs(actualFloat-expectedFloat) > deltaFloat { + return success + } else { + return fmt.Sprintf(shouldHaveNotBeenAlmostEqual, actualFloat, expectedFloat) + } +} + +func cleanAlmostEqualInput(actual interface{}, expected ...interface{}) (float64, float64, float64, string) { + deltaFloat := 0.0000000001 + + if len(expected) == 0 { + return 0.0, 0.0, 0.0, "This assertion requires exactly one comparison value and an optional delta (you provided neither)" + } else if len(expected) == 2 { + delta, err := getFloat(expected[1]) + + if err != nil { + return 0.0, 0.0, 0.0, "The delta value " + err.Error() + } + + deltaFloat = delta + } else if len(expected) > 2 { + return 0.0, 0.0, 0.0, "This assertion requires exactly one comparison value and an optional delta (you provided more values)" + } + + actualFloat, err := getFloat(actual) + if err != nil { + return 0.0, 0.0, 0.0, "The actual value " + err.Error() + } + + expectedFloat, err := getFloat(expected[0]) + if err != nil { + return 0.0, 0.0, 0.0, "The comparison value " + err.Error() + } + + return actualFloat, expectedFloat, deltaFloat, "" +} + +// returns the float value of any real number, or error if it is not a numerical type +func getFloat(num interface{}) (float64, error) { + numValue := reflect.ValueOf(num) + numKind := numValue.Kind() + + if numKind == reflect.Int || + numKind == reflect.Int8 || + numKind == reflect.Int16 || + numKind == reflect.Int32 || + numKind == reflect.Int64 { + return float64(numValue.Int()), nil + } else if numKind == reflect.Uint || + numKind == reflect.Uint8 || + numKind == reflect.Uint16 || + numKind == reflect.Uint32 || + numKind == reflect.Uint64 { + return float64(numValue.Uint()), nil + } else if numKind == reflect.Float32 || + numKind == reflect.Float64 { + return numValue.Float(), nil + } else { + return 0.0, errors.New("must be a numerical type, but was: " + numKind.String()) + } +} + +// ShouldEqualJSON receives exactly two parameters and does an equality check by marshalling to JSON +func ShouldEqualJSON(actual interface{}, expected ...interface{}) string { + if message := need(1, expected); message != success { + return message + } + + expectedString, expectedErr := remarshal(expected[0].(string)) + if expectedErr != nil { + return "Expected value not valid JSON: " + expectedErr.Error() + } + + actualString, actualErr := remarshal(actual.(string)) + if actualErr != nil { + return "Actual value not valid JSON: " + actualErr.Error() + } + + return ShouldEqual(actualString, expectedString) +} +func remarshal(value string) (string, error) { + var structured map[string]interface{} + err := json.Unmarshal([]byte(value), &structured) + if err != nil { + return "", err + } + canonical, _ := json.Marshal(structured) + return string(canonical), nil +} + +// ShouldResemble receives exactly two parameters and does a deep equal check (see reflect.DeepEqual) +func ShouldResemble(actual interface{}, expected ...interface{}) string { + if message := need(1, expected); message != success { + return message + } + + if matchError := oglematchers.DeepEquals(expected[0]).Matches(actual); matchError != nil { + return serializer.serializeDetailed(expected[0], actual, + fmt.Sprintf(shouldHaveResembled, render.Render(expected[0]), render.Render(actual))) + } + + return success +} + +// ShouldNotResemble receives exactly two parameters and does an inverse deep equal check (see reflect.DeepEqual) +func ShouldNotResemble(actual interface{}, expected ...interface{}) string { + if message := need(1, expected); message != success { + return message + } else if ShouldResemble(actual, expected[0]) == success { + return fmt.Sprintf(shouldNotHaveResembled, render.Render(actual), render.Render(expected[0])) + } + return success +} + +// ShouldPointTo receives exactly two parameters and checks to see that they point to the same address. +func ShouldPointTo(actual interface{}, expected ...interface{}) string { + if message := need(1, expected); message != success { + return message + } + return shouldPointTo(actual, expected[0]) + +} +func shouldPointTo(actual, expected interface{}) string { + actualValue := reflect.ValueOf(actual) + expectedValue := reflect.ValueOf(expected) + + if ShouldNotBeNil(actual) != success { + return fmt.Sprintf(shouldHaveBeenNonNilPointer, "first", "nil") + } else if ShouldNotBeNil(expected) != success { + return fmt.Sprintf(shouldHaveBeenNonNilPointer, "second", "nil") + } else if actualValue.Kind() != reflect.Ptr { + return fmt.Sprintf(shouldHaveBeenNonNilPointer, "first", "not") + } else if expectedValue.Kind() != reflect.Ptr { + return fmt.Sprintf(shouldHaveBeenNonNilPointer, "second", "not") + } else if ShouldEqual(actualValue.Pointer(), expectedValue.Pointer()) != success { + actualAddress := reflect.ValueOf(actual).Pointer() + expectedAddress := reflect.ValueOf(expected).Pointer() + return serializer.serialize(expectedAddress, actualAddress, fmt.Sprintf(shouldHavePointedTo, + actual, actualAddress, + expected, expectedAddress)) + } + return success +} + +// ShouldNotPointTo receives exactly two parameters and checks to see that they point to different addresess. +func ShouldNotPointTo(actual interface{}, expected ...interface{}) string { + if message := need(1, expected); message != success { + return message + } + compare := ShouldPointTo(actual, expected[0]) + if strings.HasPrefix(compare, shouldBePointers) { + return compare + } else if compare == success { + return fmt.Sprintf(shouldNotHavePointedTo, actual, expected[0], reflect.ValueOf(actual).Pointer()) + } + return success +} + +// ShouldBeNil receives a single parameter and ensures that it is nil. +func ShouldBeNil(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } else if actual == nil { + return success + } else if interfaceHasNilValue(actual) { + return success + } + return fmt.Sprintf(shouldHaveBeenNil, actual) +} +func interfaceHasNilValue(actual interface{}) bool { + value := reflect.ValueOf(actual) + kind := value.Kind() + nilable := kind == reflect.Slice || + kind == reflect.Chan || + kind == reflect.Func || + kind == reflect.Ptr || + kind == reflect.Map + + // Careful: reflect.Value.IsNil() will panic unless it's an interface, chan, map, func, slice, or ptr + // Reference: http://golang.org/pkg/reflect/#Value.IsNil + return nilable && value.IsNil() +} + +// ShouldNotBeNil receives a single parameter and ensures that it is not nil. +func ShouldNotBeNil(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } else if ShouldBeNil(actual) == success { + return fmt.Sprintf(shouldNotHaveBeenNil, actual) + } + return success +} + +// ShouldBeTrue receives a single parameter and ensures that it is true. +func ShouldBeTrue(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } else if actual != true { + return fmt.Sprintf(shouldHaveBeenTrue, actual) + } + return success +} + +// ShouldBeFalse receives a single parameter and ensures that it is false. +func ShouldBeFalse(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } else if actual != false { + return fmt.Sprintf(shouldHaveBeenFalse, actual) + } + return success +} + +// ShouldBeZeroValue receives a single parameter and ensures that it is +// the Go equivalent of the default value, or "zero" value. +func ShouldBeZeroValue(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } + zeroVal := reflect.Zero(reflect.TypeOf(actual)).Interface() + if !reflect.DeepEqual(zeroVal, actual) { + return serializer.serialize(zeroVal, actual, fmt.Sprintf(shouldHaveBeenZeroValue, actual)) + } + return success +} + +// ShouldBeZeroValue receives a single parameter and ensures that it is NOT +// the Go equivalent of the default value, or "zero" value. +func ShouldNotBeZeroValue(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } + zeroVal := reflect.Zero(reflect.TypeOf(actual)).Interface() + if reflect.DeepEqual(zeroVal, actual) { + return serializer.serialize(zeroVal, actual, fmt.Sprintf(shouldNotHaveBeenZeroValue, actual)) + } + return success +} diff --git a/vendor/github.com/smartystreets/assertions/filter.go b/vendor/github.com/smartystreets/assertions/filter.go new file mode 100644 index 0000000000..7c46ab8e35 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/filter.go @@ -0,0 +1,31 @@ +package assertions + +import "fmt" + +const ( + success = "" + needExactValues = "This assertion requires exactly %d comparison values (you provided %d)." + needNonEmptyCollection = "This assertion requires at least 1 comparison value (you provided 0)." + needFewerValues = "This assertion allows %d or fewer comparison values (you provided %d)." +) + +func need(needed int, expected []interface{}) string { + if len(expected) != needed { + return fmt.Sprintf(needExactValues, needed, len(expected)) + } + return success +} + +func atLeast(minimum int, expected []interface{}) string { + if len(expected) < 1 { + return needNonEmptyCollection + } + return success +} + +func atMost(max int, expected []interface{}) string { + if len(expected) > max { + return fmt.Sprintf(needFewerValues, max, len(expected)) + } + return success +} diff --git a/vendor/github.com/smartystreets/assertions/internal/go-render/LICENSE b/vendor/github.com/smartystreets/assertions/internal/go-render/LICENSE new file mode 100644 index 0000000000..6280ff0e06 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/go-render/LICENSE @@ -0,0 +1,27 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/github.com/smartystreets/assertions/internal/go-render/render/render.go b/vendor/github.com/smartystreets/assertions/internal/go-render/render/render.go new file mode 100644 index 0000000000..313611ef0c --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/go-render/render/render.go @@ -0,0 +1,481 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package render + +import ( + "bytes" + "fmt" + "reflect" + "sort" + "strconv" +) + +var builtinTypeMap = map[reflect.Kind]string{ + reflect.Bool: "bool", + reflect.Complex128: "complex128", + reflect.Complex64: "complex64", + reflect.Float32: "float32", + reflect.Float64: "float64", + reflect.Int16: "int16", + reflect.Int32: "int32", + reflect.Int64: "int64", + reflect.Int8: "int8", + reflect.Int: "int", + reflect.String: "string", + reflect.Uint16: "uint16", + reflect.Uint32: "uint32", + reflect.Uint64: "uint64", + reflect.Uint8: "uint8", + reflect.Uint: "uint", + reflect.Uintptr: "uintptr", +} + +var builtinTypeSet = map[string]struct{}{} + +func init() { + for _, v := range builtinTypeMap { + builtinTypeSet[v] = struct{}{} + } +} + +var typeOfString = reflect.TypeOf("") +var typeOfInt = reflect.TypeOf(int(1)) +var typeOfUint = reflect.TypeOf(uint(1)) +var typeOfFloat = reflect.TypeOf(10.1) + +// Render converts a structure to a string representation. Unline the "%#v" +// format string, this resolves pointer types' contents in structs, maps, and +// slices/arrays and prints their field values. +func Render(v interface{}) string { + buf := bytes.Buffer{} + s := (*traverseState)(nil) + s.render(&buf, 0, reflect.ValueOf(v), false) + return buf.String() +} + +// renderPointer is called to render a pointer value. +// +// This is overridable so that the test suite can have deterministic pointer +// values in its expectations. +var renderPointer = func(buf *bytes.Buffer, p uintptr) { + fmt.Fprintf(buf, "0x%016x", p) +} + +// traverseState is used to note and avoid recursion as struct members are being +// traversed. +// +// traverseState is allowed to be nil. Specifically, the root state is nil. +type traverseState struct { + parent *traverseState + ptr uintptr +} + +func (s *traverseState) forkFor(ptr uintptr) *traverseState { + for cur := s; cur != nil; cur = cur.parent { + if ptr == cur.ptr { + return nil + } + } + + fs := &traverseState{ + parent: s, + ptr: ptr, + } + return fs +} + +func (s *traverseState) render(buf *bytes.Buffer, ptrs int, v reflect.Value, implicit bool) { + if v.Kind() == reflect.Invalid { + buf.WriteString("nil") + return + } + vt := v.Type() + + // If the type being rendered is a potentially recursive type (a type that + // can contain itself as a member), we need to avoid recursion. + // + // If we've already seen this type before, mark that this is the case and + // write a recursion placeholder instead of actually rendering it. + // + // If we haven't seen it before, fork our `seen` tracking so any higher-up + // renderers will also render it at least once, then mark that we've seen it + // to avoid recursing on lower layers. + pe := uintptr(0) + vk := vt.Kind() + switch vk { + case reflect.Ptr: + // Since structs and arrays aren't pointers, they can't directly be + // recursed, but they can contain pointers to themselves. Record their + // pointer to avoid this. + switch v.Elem().Kind() { + case reflect.Struct, reflect.Array: + pe = v.Pointer() + } + + case reflect.Slice, reflect.Map: + pe = v.Pointer() + } + if pe != 0 { + s = s.forkFor(pe) + if s == nil { + buf.WriteString("") + return + } + } + + isAnon := func(t reflect.Type) bool { + if t.Name() != "" { + if _, ok := builtinTypeSet[t.Name()]; !ok { + return false + } + } + return t.Kind() != reflect.Interface + } + + switch vk { + case reflect.Struct: + if !implicit { + writeType(buf, ptrs, vt) + } + buf.WriteRune('{') + if rendered, ok := renderTime(v); ok { + buf.WriteString(rendered) + } else { + structAnon := vt.Name() == "" + for i := 0; i < vt.NumField(); i++ { + if i > 0 { + buf.WriteString(", ") + } + anon := structAnon && isAnon(vt.Field(i).Type) + + if !anon { + buf.WriteString(vt.Field(i).Name) + buf.WriteRune(':') + } + + s.render(buf, 0, v.Field(i), anon) + } + } + buf.WriteRune('}') + + case reflect.Slice: + if v.IsNil() { + if !implicit { + writeType(buf, ptrs, vt) + buf.WriteString("(nil)") + } else { + buf.WriteString("nil") + } + return + } + fallthrough + + case reflect.Array: + if !implicit { + writeType(buf, ptrs, vt) + } + anon := vt.Name() == "" && isAnon(vt.Elem()) + buf.WriteString("{") + for i := 0; i < v.Len(); i++ { + if i > 0 { + buf.WriteString(", ") + } + + s.render(buf, 0, v.Index(i), anon) + } + buf.WriteRune('}') + + case reflect.Map: + if !implicit { + writeType(buf, ptrs, vt) + } + if v.IsNil() { + buf.WriteString("(nil)") + } else { + buf.WriteString("{") + + mkeys := v.MapKeys() + tryAndSortMapKeys(vt, mkeys) + + kt := vt.Key() + keyAnon := typeOfString.ConvertibleTo(kt) || typeOfInt.ConvertibleTo(kt) || typeOfUint.ConvertibleTo(kt) || typeOfFloat.ConvertibleTo(kt) + valAnon := vt.Name() == "" && isAnon(vt.Elem()) + for i, mk := range mkeys { + if i > 0 { + buf.WriteString(", ") + } + + s.render(buf, 0, mk, keyAnon) + buf.WriteString(":") + s.render(buf, 0, v.MapIndex(mk), valAnon) + } + buf.WriteRune('}') + } + + case reflect.Ptr: + ptrs++ + fallthrough + case reflect.Interface: + if v.IsNil() { + writeType(buf, ptrs, v.Type()) + buf.WriteString("(nil)") + } else { + s.render(buf, ptrs, v.Elem(), false) + } + + case reflect.Chan, reflect.Func, reflect.UnsafePointer: + writeType(buf, ptrs, vt) + buf.WriteRune('(') + renderPointer(buf, v.Pointer()) + buf.WriteRune(')') + + default: + tstr := vt.String() + implicit = implicit || (ptrs == 0 && builtinTypeMap[vk] == tstr) + if !implicit { + writeType(buf, ptrs, vt) + buf.WriteRune('(') + } + + switch vk { + case reflect.String: + fmt.Fprintf(buf, "%q", v.String()) + case reflect.Bool: + fmt.Fprintf(buf, "%v", v.Bool()) + + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + fmt.Fprintf(buf, "%d", v.Int()) + + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + fmt.Fprintf(buf, "%d", v.Uint()) + + case reflect.Float32, reflect.Float64: + fmt.Fprintf(buf, "%g", v.Float()) + + case reflect.Complex64, reflect.Complex128: + fmt.Fprintf(buf, "%g", v.Complex()) + } + + if !implicit { + buf.WriteRune(')') + } + } +} + +func writeType(buf *bytes.Buffer, ptrs int, t reflect.Type) { + parens := ptrs > 0 + switch t.Kind() { + case reflect.Chan, reflect.Func, reflect.UnsafePointer: + parens = true + } + + if parens { + buf.WriteRune('(') + for i := 0; i < ptrs; i++ { + buf.WriteRune('*') + } + } + + switch t.Kind() { + case reflect.Ptr: + if ptrs == 0 { + // This pointer was referenced from within writeType (e.g., as part of + // rendering a list), and so hasn't had its pointer asterisk accounted + // for. + buf.WriteRune('*') + } + writeType(buf, 0, t.Elem()) + + case reflect.Interface: + if n := t.Name(); n != "" { + buf.WriteString(t.String()) + } else { + buf.WriteString("interface{}") + } + + case reflect.Array: + buf.WriteRune('[') + buf.WriteString(strconv.FormatInt(int64(t.Len()), 10)) + buf.WriteRune(']') + writeType(buf, 0, t.Elem()) + + case reflect.Slice: + if t == reflect.SliceOf(t.Elem()) { + buf.WriteString("[]") + writeType(buf, 0, t.Elem()) + } else { + // Custom slice type, use type name. + buf.WriteString(t.String()) + } + + case reflect.Map: + if t == reflect.MapOf(t.Key(), t.Elem()) { + buf.WriteString("map[") + writeType(buf, 0, t.Key()) + buf.WriteRune(']') + writeType(buf, 0, t.Elem()) + } else { + // Custom map type, use type name. + buf.WriteString(t.String()) + } + + default: + buf.WriteString(t.String()) + } + + if parens { + buf.WriteRune(')') + } +} + +type cmpFn func(a, b reflect.Value) int + +type sortableValueSlice struct { + cmp cmpFn + elements []reflect.Value +} + +func (s sortableValueSlice) Len() int { + return len(s.elements) +} + +func (s sortableValueSlice) Less(i, j int) bool { + return s.cmp(s.elements[i], s.elements[j]) < 0 +} + +func (s sortableValueSlice) Swap(i, j int) { + s.elements[i], s.elements[j] = s.elements[j], s.elements[i] +} + +// cmpForType returns a cmpFn which sorts the data for some type t in the same +// order that a go-native map key is compared for equality. +func cmpForType(t reflect.Type) cmpFn { + switch t.Kind() { + case reflect.String: + return func(av, bv reflect.Value) int { + a, b := av.String(), bv.String() + if a < b { + return -1 + } else if a > b { + return 1 + } + return 0 + } + + case reflect.Bool: + return func(av, bv reflect.Value) int { + a, b := av.Bool(), bv.Bool() + if !a && b { + return -1 + } else if a && !b { + return 1 + } + return 0 + } + + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return func(av, bv reflect.Value) int { + a, b := av.Int(), bv.Int() + if a < b { + return -1 + } else if a > b { + return 1 + } + return 0 + } + + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, + reflect.Uint64, reflect.Uintptr, reflect.UnsafePointer: + return func(av, bv reflect.Value) int { + a, b := av.Uint(), bv.Uint() + if a < b { + return -1 + } else if a > b { + return 1 + } + return 0 + } + + case reflect.Float32, reflect.Float64: + return func(av, bv reflect.Value) int { + a, b := av.Float(), bv.Float() + if a < b { + return -1 + } else if a > b { + return 1 + } + return 0 + } + + case reflect.Interface: + return func(av, bv reflect.Value) int { + a, b := av.InterfaceData(), bv.InterfaceData() + if a[0] < b[0] { + return -1 + } else if a[0] > b[0] { + return 1 + } + if a[1] < b[1] { + return -1 + } else if a[1] > b[1] { + return 1 + } + return 0 + } + + case reflect.Complex64, reflect.Complex128: + return func(av, bv reflect.Value) int { + a, b := av.Complex(), bv.Complex() + if real(a) < real(b) { + return -1 + } else if real(a) > real(b) { + return 1 + } + if imag(a) < imag(b) { + return -1 + } else if imag(a) > imag(b) { + return 1 + } + return 0 + } + + case reflect.Ptr, reflect.Chan: + return func(av, bv reflect.Value) int { + a, b := av.Pointer(), bv.Pointer() + if a < b { + return -1 + } else if a > b { + return 1 + } + return 0 + } + + case reflect.Struct: + cmpLst := make([]cmpFn, t.NumField()) + for i := range cmpLst { + cmpLst[i] = cmpForType(t.Field(i).Type) + } + return func(a, b reflect.Value) int { + for i, cmp := range cmpLst { + if rslt := cmp(a.Field(i), b.Field(i)); rslt != 0 { + return rslt + } + } + return 0 + } + } + + return nil +} + +func tryAndSortMapKeys(mt reflect.Type, k []reflect.Value) { + if cmp := cmpForType(mt.Key()); cmp != nil { + sort.Sort(sortableValueSlice{cmp, k}) + } +} diff --git a/vendor/github.com/smartystreets/assertions/internal/go-render/render/render_time.go b/vendor/github.com/smartystreets/assertions/internal/go-render/render/render_time.go new file mode 100644 index 0000000000..990c75d0ff --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/go-render/render/render_time.go @@ -0,0 +1,26 @@ +package render + +import ( + "reflect" + "time" +) + +func renderTime(value reflect.Value) (string, bool) { + if instant, ok := convertTime(value); !ok { + return "", false + } else if instant.IsZero() { + return "0", true + } else { + return instant.String(), true + } +} + +func convertTime(value reflect.Value) (t time.Time, ok bool) { + if value.Type() == timeType { + defer func() { recover() }() + t, ok = value.Interface().(time.Time) + } + return +} + +var timeType = reflect.TypeOf(time.Time{}) diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/.gitignore b/vendor/github.com/smartystreets/assertions/internal/oglematchers/.gitignore new file mode 100644 index 0000000000..dd8fc7468f --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/.gitignore @@ -0,0 +1,5 @@ +*.6 +6.out +_obj/ +_test/ +_testmain.go diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/.travis.yml b/vendor/github.com/smartystreets/assertions/internal/oglematchers/.travis.yml new file mode 100644 index 0000000000..b97211926e --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/.travis.yml @@ -0,0 +1,4 @@ +# Cf. http://docs.travis-ci.com/user/getting-started/ +# Cf. http://docs.travis-ci.com/user/languages/go/ + +language: go diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/LICENSE b/vendor/github.com/smartystreets/assertions/internal/oglematchers/LICENSE new file mode 100644 index 0000000000..d645695673 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/README.md b/vendor/github.com/smartystreets/assertions/internal/oglematchers/README.md new file mode 100644 index 0000000000..215a2bb7a8 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/README.md @@ -0,0 +1,58 @@ +[![GoDoc](https://godoc.org/github.com/smartystreets/assertions/internal/oglematchers?status.svg)](https://godoc.org/github.com/smartystreets/assertions/internal/oglematchers) + +`oglematchers` is a package for the Go programming language containing a set of +matchers, useful in a testing or mocking framework, inspired by and mostly +compatible with [Google Test][googletest] for C++ and +[Google JS Test][google-js-test]. The package is used by the +[ogletest][ogletest] testing framework and [oglemock][oglemock] mocking +framework, which may be more directly useful to you, but can be generically used +elsewhere as well. + +A "matcher" is simply an object with a `Matches` method defining a set of golang +values matched by the matcher, and a `Description` method describing that set. +For example, here are some matchers: + +```go +// Numbers +Equals(17.13) +LessThan(19) + +// Strings +Equals("taco") +HasSubstr("burrito") +MatchesRegex("t.*o") + +// Combining matchers +AnyOf(LessThan(17), GreaterThan(19)) +``` + +There are lots more; see [here][reference] for a reference. You can also add +your own simply by implementing the `oglematchers.Matcher` interface. + + +Installation +------------ + +First, make sure you have installed Go 1.0.2 or newer. See +[here][golang-install] for instructions. + +Use the following command to install `oglematchers` and keep it up to date: + + go get -u github.com/smartystreets/assertions/internal/oglematchers + + +Documentation +------------- + +See [here][reference] for documentation. Alternatively, you can install the +package and then use `godoc`: + + godoc github.com/smartystreets/assertions/internal/oglematchers + + +[reference]: http://godoc.org/github.com/smartystreets/assertions/internal/oglematchers +[golang-install]: http://golang.org/doc/install.html +[googletest]: http://code.google.com/p/googletest/ +[google-js-test]: http://code.google.com/p/google-js-test/ +[ogletest]: http://github.com/smartystreets/assertions/internal/ogletest +[oglemock]: http://github.com/smartystreets/assertions/internal/oglemock diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/any_of.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/any_of.go new file mode 100644 index 0000000000..2918b51f21 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/any_of.go @@ -0,0 +1,94 @@ +// Copyright 2011 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oglematchers + +import ( + "errors" + "fmt" + "reflect" + "strings" +) + +// AnyOf accepts a set of values S and returns a matcher that follows the +// algorithm below when considering a candidate c: +// +// 1. If there exists a value m in S such that m implements the Matcher +// interface and m matches c, return true. +// +// 2. Otherwise, if there exists a value v in S such that v does not implement +// the Matcher interface and the matcher Equals(v) matches c, return true. +// +// 3. Otherwise, if there is a value m in S such that m implements the Matcher +// interface and m returns a fatal error for c, return that fatal error. +// +// 4. Otherwise, return false. +// +// This is akin to a logical OR operation for matchers, with non-matchers x +// being treated as Equals(x). +func AnyOf(vals ...interface{}) Matcher { + // Get ahold of a type variable for the Matcher interface. + var dummy *Matcher + matcherType := reflect.TypeOf(dummy).Elem() + + // Create a matcher for each value, or use the value itself if it's already a + // matcher. + wrapped := make([]Matcher, len(vals)) + for i, v := range vals { + t := reflect.TypeOf(v) + if t != nil && t.Implements(matcherType) { + wrapped[i] = v.(Matcher) + } else { + wrapped[i] = Equals(v) + } + } + + return &anyOfMatcher{wrapped} +} + +type anyOfMatcher struct { + wrapped []Matcher +} + +func (m *anyOfMatcher) Description() string { + wrappedDescs := make([]string, len(m.wrapped)) + for i, matcher := range m.wrapped { + wrappedDescs[i] = matcher.Description() + } + + return fmt.Sprintf("or(%s)", strings.Join(wrappedDescs, ", ")) +} + +func (m *anyOfMatcher) Matches(c interface{}) (err error) { + err = errors.New("") + + // Try each matcher in turn. + for _, matcher := range m.wrapped { + wrappedErr := matcher.Matches(c) + + // Return immediately if there's a match. + if wrappedErr == nil { + err = nil + return + } + + // Note the fatal error, if any. + if _, isFatal := wrappedErr.(*FatalError); isFatal { + err = wrappedErr + } + } + + return +} diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/contains.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/contains.go new file mode 100644 index 0000000000..87f107d392 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/contains.go @@ -0,0 +1,61 @@ +// Copyright 2012 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oglematchers + +import ( + "fmt" + "reflect" +) + +// Return a matcher that matches arrays slices with at least one element that +// matches the supplied argument. If the argument x is not itself a Matcher, +// this is equivalent to Contains(Equals(x)). +func Contains(x interface{}) Matcher { + var result containsMatcher + var ok bool + + if result.elementMatcher, ok = x.(Matcher); !ok { + result.elementMatcher = DeepEquals(x) + } + + return &result +} + +type containsMatcher struct { + elementMatcher Matcher +} + +func (m *containsMatcher) Description() string { + return fmt.Sprintf("contains: %s", m.elementMatcher.Description()) +} + +func (m *containsMatcher) Matches(candidate interface{}) error { + // The candidate must be a slice or an array. + v := reflect.ValueOf(candidate) + if v.Kind() != reflect.Slice && v.Kind() != reflect.Array { + return NewFatalError("which is not a slice or array") + } + + // Check each element. + for i := 0; i < v.Len(); i++ { + elem := v.Index(i) + if matchErr := m.elementMatcher.Matches(elem.Interface()); matchErr == nil { + return nil + } + } + + return fmt.Errorf("") +} diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/deep_equals.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/deep_equals.go new file mode 100644 index 0000000000..1d91baef32 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/deep_equals.go @@ -0,0 +1,88 @@ +// Copyright 2012 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oglematchers + +import ( + "bytes" + "errors" + "fmt" + "reflect" +) + +var byteSliceType reflect.Type = reflect.TypeOf([]byte{}) + +// DeepEquals returns a matcher that matches based on 'deep equality', as +// defined by the reflect package. This matcher requires that values have +// identical types to x. +func DeepEquals(x interface{}) Matcher { + return &deepEqualsMatcher{x} +} + +type deepEqualsMatcher struct { + x interface{} +} + +func (m *deepEqualsMatcher) Description() string { + xDesc := fmt.Sprintf("%v", m.x) + xValue := reflect.ValueOf(m.x) + + // Special case: fmt.Sprintf presents nil slices as "[]", but + // reflect.DeepEqual makes a distinction between nil and empty slices. Make + // this less confusing. + if xValue.Kind() == reflect.Slice && xValue.IsNil() { + xDesc = "" + } + + return fmt.Sprintf("deep equals: %s", xDesc) +} + +func (m *deepEqualsMatcher) Matches(c interface{}) error { + // Make sure the types match. + ct := reflect.TypeOf(c) + xt := reflect.TypeOf(m.x) + + if ct != xt { + return NewFatalError(fmt.Sprintf("which is of type %v", ct)) + } + + // Special case: handle byte slices more efficiently. + cValue := reflect.ValueOf(c) + xValue := reflect.ValueOf(m.x) + + if ct == byteSliceType && !cValue.IsNil() && !xValue.IsNil() { + xBytes := m.x.([]byte) + cBytes := c.([]byte) + + if bytes.Equal(cBytes, xBytes) { + return nil + } + + return errors.New("") + } + + // Defer to the reflect package. + if reflect.DeepEqual(m.x, c) { + return nil + } + + // Special case: if the comparison failed because c is the nil slice, given + // an indication of this (since its value is printed as "[]"). + if cValue.Kind() == reflect.Slice && cValue.IsNil() { + return errors.New("which is nil") + } + + return errors.New("") +} diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/equals.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/equals.go new file mode 100644 index 0000000000..a510707b3c --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/equals.go @@ -0,0 +1,541 @@ +// Copyright 2011 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oglematchers + +import ( + "errors" + "fmt" + "math" + "reflect" +) + +// Equals(x) returns a matcher that matches values v such that v and x are +// equivalent. This includes the case when the comparison v == x using Go's +// built-in comparison operator is legal (except for structs, which this +// matcher does not support), but for convenience the following rules also +// apply: +// +// * Type checking is done based on underlying types rather than actual +// types, so that e.g. two aliases for string can be compared: +// +// type stringAlias1 string +// type stringAlias2 string +// +// a := "taco" +// b := stringAlias1("taco") +// c := stringAlias2("taco") +// +// ExpectTrue(a == b) // Legal, passes +// ExpectTrue(b == c) // Illegal, doesn't compile +// +// ExpectThat(a, Equals(b)) // Passes +// ExpectThat(b, Equals(c)) // Passes +// +// * Values of numeric type are treated as if they were abstract numbers, and +// compared accordingly. Therefore Equals(17) will match int(17), +// int16(17), uint(17), float32(17), complex64(17), and so on. +// +// If you want a stricter matcher that contains no such cleverness, see +// IdenticalTo instead. +// +// Arrays are supported by this matcher, but do not participate in the +// exceptions above. Two arrays compared with this matcher must have identical +// types, and their element type must itself be comparable according to Go's == +// operator. +func Equals(x interface{}) Matcher { + v := reflect.ValueOf(x) + + // This matcher doesn't support structs. + if v.Kind() == reflect.Struct { + panic(fmt.Sprintf("oglematchers.Equals: unsupported kind %v", v.Kind())) + } + + // The == operator is not defined for non-nil slices. + if v.Kind() == reflect.Slice && v.Pointer() != uintptr(0) { + panic(fmt.Sprintf("oglematchers.Equals: non-nil slice")) + } + + return &equalsMatcher{v} +} + +type equalsMatcher struct { + expectedValue reflect.Value +} + +//////////////////////////////////////////////////////////////////////// +// Numeric types +//////////////////////////////////////////////////////////////////////// + +func isSignedInteger(v reflect.Value) bool { + k := v.Kind() + return k >= reflect.Int && k <= reflect.Int64 +} + +func isUnsignedInteger(v reflect.Value) bool { + k := v.Kind() + return k >= reflect.Uint && k <= reflect.Uintptr +} + +func isInteger(v reflect.Value) bool { + return isSignedInteger(v) || isUnsignedInteger(v) +} + +func isFloat(v reflect.Value) bool { + k := v.Kind() + return k == reflect.Float32 || k == reflect.Float64 +} + +func isComplex(v reflect.Value) bool { + k := v.Kind() + return k == reflect.Complex64 || k == reflect.Complex128 +} + +func checkAgainstInt64(e int64, c reflect.Value) (err error) { + err = errors.New("") + + switch { + case isSignedInteger(c): + if c.Int() == e { + err = nil + } + + case isUnsignedInteger(c): + u := c.Uint() + if u <= math.MaxInt64 && int64(u) == e { + err = nil + } + + // Turn around the various floating point types so that the checkAgainst* + // functions for them can deal with precision issues. + case isFloat(c), isComplex(c): + return Equals(c.Interface()).Matches(e) + + default: + err = NewFatalError("which is not numeric") + } + + return +} + +func checkAgainstUint64(e uint64, c reflect.Value) (err error) { + err = errors.New("") + + switch { + case isSignedInteger(c): + i := c.Int() + if i >= 0 && uint64(i) == e { + err = nil + } + + case isUnsignedInteger(c): + if c.Uint() == e { + err = nil + } + + // Turn around the various floating point types so that the checkAgainst* + // functions for them can deal with precision issues. + case isFloat(c), isComplex(c): + return Equals(c.Interface()).Matches(e) + + default: + err = NewFatalError("which is not numeric") + } + + return +} + +func checkAgainstFloat32(e float32, c reflect.Value) (err error) { + err = errors.New("") + + switch { + case isSignedInteger(c): + if float32(c.Int()) == e { + err = nil + } + + case isUnsignedInteger(c): + if float32(c.Uint()) == e { + err = nil + } + + case isFloat(c): + // Compare using float32 to avoid a false sense of precision; otherwise + // e.g. Equals(float32(0.1)) won't match float32(0.1). + if float32(c.Float()) == e { + err = nil + } + + case isComplex(c): + comp := c.Complex() + rl := real(comp) + im := imag(comp) + + // Compare using float32 to avoid a false sense of precision; otherwise + // e.g. Equals(float32(0.1)) won't match (0.1 + 0i). + if im == 0 && float32(rl) == e { + err = nil + } + + default: + err = NewFatalError("which is not numeric") + } + + return +} + +func checkAgainstFloat64(e float64, c reflect.Value) (err error) { + err = errors.New("") + + ck := c.Kind() + + switch { + case isSignedInteger(c): + if float64(c.Int()) == e { + err = nil + } + + case isUnsignedInteger(c): + if float64(c.Uint()) == e { + err = nil + } + + // If the actual value is lower precision, turn the comparison around so we + // apply the low-precision rules. Otherwise, e.g. Equals(0.1) may not match + // float32(0.1). + case ck == reflect.Float32 || ck == reflect.Complex64: + return Equals(c.Interface()).Matches(e) + + // Otherwise, compare with double precision. + case isFloat(c): + if c.Float() == e { + err = nil + } + + case isComplex(c): + comp := c.Complex() + rl := real(comp) + im := imag(comp) + + if im == 0 && rl == e { + err = nil + } + + default: + err = NewFatalError("which is not numeric") + } + + return +} + +func checkAgainstComplex64(e complex64, c reflect.Value) (err error) { + err = errors.New("") + realPart := real(e) + imaginaryPart := imag(e) + + switch { + case isInteger(c) || isFloat(c): + // If we have no imaginary part, then we should just compare against the + // real part. Otherwise, we can't be equal. + if imaginaryPart != 0 { + return + } + + return checkAgainstFloat32(realPart, c) + + case isComplex(c): + // Compare using complex64 to avoid a false sense of precision; otherwise + // e.g. Equals(0.1 + 0i) won't match float32(0.1). + if complex64(c.Complex()) == e { + err = nil + } + + default: + err = NewFatalError("which is not numeric") + } + + return +} + +func checkAgainstComplex128(e complex128, c reflect.Value) (err error) { + err = errors.New("") + realPart := real(e) + imaginaryPart := imag(e) + + switch { + case isInteger(c) || isFloat(c): + // If we have no imaginary part, then we should just compare against the + // real part. Otherwise, we can't be equal. + if imaginaryPart != 0 { + return + } + + return checkAgainstFloat64(realPart, c) + + case isComplex(c): + if c.Complex() == e { + err = nil + } + + default: + err = NewFatalError("which is not numeric") + } + + return +} + +//////////////////////////////////////////////////////////////////////// +// Other types +//////////////////////////////////////////////////////////////////////// + +func checkAgainstBool(e bool, c reflect.Value) (err error) { + if c.Kind() != reflect.Bool { + err = NewFatalError("which is not a bool") + return + } + + err = errors.New("") + if c.Bool() == e { + err = nil + } + return +} + +func checkAgainstChan(e reflect.Value, c reflect.Value) (err error) { + // Create a description of e's type, e.g. "chan int". + typeStr := fmt.Sprintf("%s %s", e.Type().ChanDir(), e.Type().Elem()) + + // Make sure c is a chan of the correct type. + if c.Kind() != reflect.Chan || + c.Type().ChanDir() != e.Type().ChanDir() || + c.Type().Elem() != e.Type().Elem() { + err = NewFatalError(fmt.Sprintf("which is not a %s", typeStr)) + return + } + + err = errors.New("") + if c.Pointer() == e.Pointer() { + err = nil + } + return +} + +func checkAgainstFunc(e reflect.Value, c reflect.Value) (err error) { + // Make sure c is a function. + if c.Kind() != reflect.Func { + err = NewFatalError("which is not a function") + return + } + + err = errors.New("") + if c.Pointer() == e.Pointer() { + err = nil + } + return +} + +func checkAgainstMap(e reflect.Value, c reflect.Value) (err error) { + // Make sure c is a map. + if c.Kind() != reflect.Map { + err = NewFatalError("which is not a map") + return + } + + err = errors.New("") + if c.Pointer() == e.Pointer() { + err = nil + } + return +} + +func checkAgainstPtr(e reflect.Value, c reflect.Value) (err error) { + // Create a description of e's type, e.g. "*int". + typeStr := fmt.Sprintf("*%v", e.Type().Elem()) + + // Make sure c is a pointer of the correct type. + if c.Kind() != reflect.Ptr || + c.Type().Elem() != e.Type().Elem() { + err = NewFatalError(fmt.Sprintf("which is not a %s", typeStr)) + return + } + + err = errors.New("") + if c.Pointer() == e.Pointer() { + err = nil + } + return +} + +func checkAgainstSlice(e reflect.Value, c reflect.Value) (err error) { + // Create a description of e's type, e.g. "[]int". + typeStr := fmt.Sprintf("[]%v", e.Type().Elem()) + + // Make sure c is a slice of the correct type. + if c.Kind() != reflect.Slice || + c.Type().Elem() != e.Type().Elem() { + err = NewFatalError(fmt.Sprintf("which is not a %s", typeStr)) + return + } + + err = errors.New("") + if c.Pointer() == e.Pointer() { + err = nil + } + return +} + +func checkAgainstString(e reflect.Value, c reflect.Value) (err error) { + // Make sure c is a string. + if c.Kind() != reflect.String { + err = NewFatalError("which is not a string") + return + } + + err = errors.New("") + if c.String() == e.String() { + err = nil + } + return +} + +func checkAgainstArray(e reflect.Value, c reflect.Value) (err error) { + // Create a description of e's type, e.g. "[2]int". + typeStr := fmt.Sprintf("%v", e.Type()) + + // Make sure c is the correct type. + if c.Type() != e.Type() { + err = NewFatalError(fmt.Sprintf("which is not %s", typeStr)) + return + } + + // Check for equality. + if e.Interface() != c.Interface() { + err = errors.New("") + return + } + + return +} + +func checkAgainstUnsafePointer(e reflect.Value, c reflect.Value) (err error) { + // Make sure c is a pointer. + if c.Kind() != reflect.UnsafePointer { + err = NewFatalError("which is not a unsafe.Pointer") + return + } + + err = errors.New("") + if c.Pointer() == e.Pointer() { + err = nil + } + return +} + +func checkForNil(c reflect.Value) (err error) { + err = errors.New("") + + // Make sure it is legal to call IsNil. + switch c.Kind() { + case reflect.Invalid: + case reflect.Chan: + case reflect.Func: + case reflect.Interface: + case reflect.Map: + case reflect.Ptr: + case reflect.Slice: + + default: + err = NewFatalError("which cannot be compared to nil") + return + } + + // Ask whether the value is nil. Handle a nil literal (kind Invalid) + // specially, since it's not legal to call IsNil there. + if c.Kind() == reflect.Invalid || c.IsNil() { + err = nil + } + return +} + +//////////////////////////////////////////////////////////////////////// +// Public implementation +//////////////////////////////////////////////////////////////////////// + +func (m *equalsMatcher) Matches(candidate interface{}) error { + e := m.expectedValue + c := reflect.ValueOf(candidate) + ek := e.Kind() + + switch { + case ek == reflect.Bool: + return checkAgainstBool(e.Bool(), c) + + case isSignedInteger(e): + return checkAgainstInt64(e.Int(), c) + + case isUnsignedInteger(e): + return checkAgainstUint64(e.Uint(), c) + + case ek == reflect.Float32: + return checkAgainstFloat32(float32(e.Float()), c) + + case ek == reflect.Float64: + return checkAgainstFloat64(e.Float(), c) + + case ek == reflect.Complex64: + return checkAgainstComplex64(complex64(e.Complex()), c) + + case ek == reflect.Complex128: + return checkAgainstComplex128(complex128(e.Complex()), c) + + case ek == reflect.Chan: + return checkAgainstChan(e, c) + + case ek == reflect.Func: + return checkAgainstFunc(e, c) + + case ek == reflect.Map: + return checkAgainstMap(e, c) + + case ek == reflect.Ptr: + return checkAgainstPtr(e, c) + + case ek == reflect.Slice: + return checkAgainstSlice(e, c) + + case ek == reflect.String: + return checkAgainstString(e, c) + + case ek == reflect.Array: + return checkAgainstArray(e, c) + + case ek == reflect.UnsafePointer: + return checkAgainstUnsafePointer(e, c) + + case ek == reflect.Invalid: + return checkForNil(c) + } + + panic(fmt.Sprintf("equalsMatcher.Matches: unexpected kind: %v", ek)) +} + +func (m *equalsMatcher) Description() string { + // Special case: handle nil. + if !m.expectedValue.IsValid() { + return "is nil" + } + + return fmt.Sprintf("%v", m.expectedValue.Interface()) +} diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/greater_or_equal.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/greater_or_equal.go new file mode 100644 index 0000000000..4b9d103a38 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/greater_or_equal.go @@ -0,0 +1,39 @@ +// Copyright 2011 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oglematchers + +import ( + "fmt" + "reflect" +) + +// GreaterOrEqual returns a matcher that matches integer, floating point, or +// strings values v such that v >= x. Comparison is not defined between numeric +// and string types, but is defined between all integer and floating point +// types. +// +// x must itself be an integer, floating point, or string type; otherwise, +// GreaterOrEqual will panic. +func GreaterOrEqual(x interface{}) Matcher { + desc := fmt.Sprintf("greater than or equal to %v", x) + + // Special case: make it clear that strings are strings. + if reflect.TypeOf(x).Kind() == reflect.String { + desc = fmt.Sprintf("greater than or equal to \"%s\"", x) + } + + return transformDescription(Not(LessThan(x)), desc) +} diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/greater_than.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/greater_than.go new file mode 100644 index 0000000000..3eef32178f --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/greater_than.go @@ -0,0 +1,39 @@ +// Copyright 2011 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oglematchers + +import ( + "fmt" + "reflect" +) + +// GreaterThan returns a matcher that matches integer, floating point, or +// strings values v such that v > x. Comparison is not defined between numeric +// and string types, but is defined between all integer and floating point +// types. +// +// x must itself be an integer, floating point, or string type; otherwise, +// GreaterThan will panic. +func GreaterThan(x interface{}) Matcher { + desc := fmt.Sprintf("greater than %v", x) + + // Special case: make it clear that strings are strings. + if reflect.TypeOf(x).Kind() == reflect.String { + desc = fmt.Sprintf("greater than \"%s\"", x) + } + + return transformDescription(Not(LessOrEqual(x)), desc) +} diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/less_or_equal.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/less_or_equal.go new file mode 100644 index 0000000000..8402cdeaf0 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/less_or_equal.go @@ -0,0 +1,41 @@ +// Copyright 2011 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oglematchers + +import ( + "fmt" + "reflect" +) + +// LessOrEqual returns a matcher that matches integer, floating point, or +// strings values v such that v <= x. Comparison is not defined between numeric +// and string types, but is defined between all integer and floating point +// types. +// +// x must itself be an integer, floating point, or string type; otherwise, +// LessOrEqual will panic. +func LessOrEqual(x interface{}) Matcher { + desc := fmt.Sprintf("less than or equal to %v", x) + + // Special case: make it clear that strings are strings. + if reflect.TypeOf(x).Kind() == reflect.String { + desc = fmt.Sprintf("less than or equal to \"%s\"", x) + } + + // Put LessThan last so that its error messages will be used in the event of + // failure. + return transformDescription(AnyOf(Equals(x), LessThan(x)), desc) +} diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/less_than.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/less_than.go new file mode 100644 index 0000000000..8258e45d99 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/less_than.go @@ -0,0 +1,152 @@ +// Copyright 2011 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oglematchers + +import ( + "errors" + "fmt" + "math" + "reflect" +) + +// LessThan returns a matcher that matches integer, floating point, or strings +// values v such that v < x. Comparison is not defined between numeric and +// string types, but is defined between all integer and floating point types. +// +// x must itself be an integer, floating point, or string type; otherwise, +// LessThan will panic. +func LessThan(x interface{}) Matcher { + v := reflect.ValueOf(x) + kind := v.Kind() + + switch { + case isInteger(v): + case isFloat(v): + case kind == reflect.String: + + default: + panic(fmt.Sprintf("LessThan: unexpected kind %v", kind)) + } + + return &lessThanMatcher{v} +} + +type lessThanMatcher struct { + limit reflect.Value +} + +func (m *lessThanMatcher) Description() string { + // Special case: make it clear that strings are strings. + if m.limit.Kind() == reflect.String { + return fmt.Sprintf("less than \"%s\"", m.limit.String()) + } + + return fmt.Sprintf("less than %v", m.limit.Interface()) +} + +func compareIntegers(v1, v2 reflect.Value) (err error) { + err = errors.New("") + + switch { + case isSignedInteger(v1) && isSignedInteger(v2): + if v1.Int() < v2.Int() { + err = nil + } + return + + case isSignedInteger(v1) && isUnsignedInteger(v2): + if v1.Int() < 0 || uint64(v1.Int()) < v2.Uint() { + err = nil + } + return + + case isUnsignedInteger(v1) && isSignedInteger(v2): + if v1.Uint() <= math.MaxInt64 && int64(v1.Uint()) < v2.Int() { + err = nil + } + return + + case isUnsignedInteger(v1) && isUnsignedInteger(v2): + if v1.Uint() < v2.Uint() { + err = nil + } + return + } + + panic(fmt.Sprintf("compareIntegers: %v %v", v1, v2)) +} + +func getFloat(v reflect.Value) float64 { + switch { + case isSignedInteger(v): + return float64(v.Int()) + + case isUnsignedInteger(v): + return float64(v.Uint()) + + case isFloat(v): + return v.Float() + } + + panic(fmt.Sprintf("getFloat: %v", v)) +} + +func (m *lessThanMatcher) Matches(c interface{}) (err error) { + v1 := reflect.ValueOf(c) + v2 := m.limit + + err = errors.New("") + + // Handle strings as a special case. + if v1.Kind() == reflect.String && v2.Kind() == reflect.String { + if v1.String() < v2.String() { + err = nil + } + return + } + + // If we get here, we require that we are dealing with integers or floats. + v1Legal := isInteger(v1) || isFloat(v1) + v2Legal := isInteger(v2) || isFloat(v2) + if !v1Legal || !v2Legal { + err = NewFatalError("which is not comparable") + return + } + + // Handle the various comparison cases. + switch { + // Both integers + case isInteger(v1) && isInteger(v2): + return compareIntegers(v1, v2) + + // At least one float32 + case v1.Kind() == reflect.Float32 || v2.Kind() == reflect.Float32: + if float32(getFloat(v1)) < float32(getFloat(v2)) { + err = nil + } + return + + // At least one float64 + case v1.Kind() == reflect.Float64 || v2.Kind() == reflect.Float64: + if getFloat(v1) < getFloat(v2) { + err = nil + } + return + } + + // We shouldn't get here. + panic(fmt.Sprintf("lessThanMatcher.Matches: Shouldn't get here: %v %v", v1, v2)) +} diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/matcher.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/matcher.go new file mode 100644 index 0000000000..78159a0727 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/matcher.go @@ -0,0 +1,86 @@ +// Copyright 2011 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package oglematchers provides a set of matchers useful in a testing or +// mocking framework. These matchers are inspired by and mostly compatible with +// Google Test for C++ and Google JS Test. +// +// This package is used by github.com/smartystreets/assertions/internal/ogletest and +// github.com/smartystreets/assertions/internal/oglemock, which may be more directly useful if you're not +// writing your own testing package or defining your own matchers. +package oglematchers + +// A Matcher is some predicate implicitly defining a set of values that it +// matches. For example, GreaterThan(17) matches all numeric values greater +// than 17, and HasSubstr("taco") matches all strings with the substring +// "taco". +// +// Matchers are typically exposed to tests via constructor functions like +// HasSubstr. In order to implement such a function you can either define your +// own matcher type or use NewMatcher. +type Matcher interface { + // Check whether the supplied value belongs to the the set defined by the + // matcher. Return a non-nil error if and only if it does not. + // + // The error describes why the value doesn't match. The error text is a + // relative clause that is suitable for being placed after the value. For + // example, a predicate that matches strings with a particular substring may, + // when presented with a numerical value, return the following error text: + // + // "which is not a string" + // + // Then the failure message may look like: + // + // Expected: has substring "taco" + // Actual: 17, which is not a string + // + // If the error is self-apparent based on the description of the matcher, the + // error text may be empty (but the error still non-nil). For example: + // + // Expected: 17 + // Actual: 19 + // + // If you are implementing a new matcher, see also the documentation on + // FatalError. + Matches(candidate interface{}) error + + // Description returns a string describing the property that values matching + // this matcher have, as a verb phrase where the subject is the value. For + // example, "is greather than 17" or "has substring "taco"". + Description() string +} + +// FatalError is an implementation of the error interface that may be returned +// from matchers, indicating the error should be propagated. Returning a +// *FatalError indicates that the matcher doesn't process values of the +// supplied type, or otherwise doesn't know how to handle the value. +// +// For example, if GreaterThan(17) returned false for the value "taco" without +// a fatal error, then Not(GreaterThan(17)) would return true. This is +// technically correct, but is surprising and may mask failures where the wrong +// sort of matcher is accidentally used. Instead, GreaterThan(17) can return a +// fatal error, which will be propagated by Not(). +type FatalError struct { + errorText string +} + +// NewFatalError creates a FatalError struct with the supplied error text. +func NewFatalError(s string) *FatalError { + return &FatalError{s} +} + +func (e *FatalError) Error() string { + return e.errorText +} diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/not.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/not.go new file mode 100644 index 0000000000..623789fe28 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/not.go @@ -0,0 +1,53 @@ +// Copyright 2011 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oglematchers + +import ( + "errors" + "fmt" +) + +// Not returns a matcher that inverts the set of values matched by the wrapped +// matcher. It does not transform the result for values for which the wrapped +// matcher returns a fatal error. +func Not(m Matcher) Matcher { + return ¬Matcher{m} +} + +type notMatcher struct { + wrapped Matcher +} + +func (m *notMatcher) Matches(c interface{}) (err error) { + err = m.wrapped.Matches(c) + + // Did the wrapped matcher say yes? + if err == nil { + return errors.New("") + } + + // Did the wrapped matcher return a fatal error? + if _, isFatal := err.(*FatalError); isFatal { + return err + } + + // The wrapped matcher returned a non-fatal error. + return nil +} + +func (m *notMatcher) Description() string { + return fmt.Sprintf("not(%s)", m.wrapped.Description()) +} diff --git a/vendor/github.com/smartystreets/assertions/internal/oglematchers/transform_description.go b/vendor/github.com/smartystreets/assertions/internal/oglematchers/transform_description.go new file mode 100644 index 0000000000..8ea2807c6f --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/internal/oglematchers/transform_description.go @@ -0,0 +1,36 @@ +// Copyright 2011 Aaron Jacobs. All Rights Reserved. +// Author: aaronjjacobs@gmail.com (Aaron Jacobs) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oglematchers + +// transformDescription returns a matcher that is equivalent to the supplied +// one, except that it has the supplied description instead of the one attached +// to the existing matcher. +func transformDescription(m Matcher, newDesc string) Matcher { + return &transformDescriptionMatcher{newDesc, m} +} + +type transformDescriptionMatcher struct { + desc string + wrappedMatcher Matcher +} + +func (m *transformDescriptionMatcher) Description() string { + return m.desc +} + +func (m *transformDescriptionMatcher) Matches(c interface{}) error { + return m.wrappedMatcher.Matches(c) +} diff --git a/vendor/github.com/smartystreets/assertions/messages.go b/vendor/github.com/smartystreets/assertions/messages.go new file mode 100644 index 0000000000..6ed7dc2ae9 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/messages.go @@ -0,0 +1,97 @@ +package assertions + +const ( // equality + shouldHaveBeenEqual = "Expected: '%v'\nActual: '%v'\n(Should be equal)" + shouldNotHaveBeenEqual = "Expected '%v'\nto NOT equal '%v'\n(but it did)!" + shouldHaveBeenEqualTypeMismatch = "Expected: '%v' (%T)\nActual: '%v' (%T)\n(Should be equal, type mismatch)" + shouldHaveBeenAlmostEqual = "Expected '%v' to almost equal '%v' (but it didn't)!" + shouldHaveNotBeenAlmostEqual = "Expected '%v' to NOT almost equal '%v' (but it did)!" + shouldHaveResembled = "Expected: '%s'\nActual: '%s'\n(Should resemble)!" + shouldNotHaveResembled = "Expected '%#v'\nto NOT resemble '%#v'\n(but it did)!" + shouldBePointers = "Both arguments should be pointers " + shouldHaveBeenNonNilPointer = shouldBePointers + "(the %s was %s)!" + shouldHavePointedTo = "Expected '%+v' (address: '%v') and '%+v' (address: '%v') to be the same address (but their weren't)!" + shouldNotHavePointedTo = "Expected '%+v' and '%+v' to be different references (but they matched: '%v')!" + shouldHaveBeenNil = "Expected: nil\nActual: '%v'" + shouldNotHaveBeenNil = "Expected '%+v' to NOT be nil (but it was)!" + shouldHaveBeenTrue = "Expected: true\nActual: %v" + shouldHaveBeenFalse = "Expected: false\nActual: %v" + shouldHaveBeenZeroValue = "'%+v' should have been the zero value" //"Expected: (zero value)\nActual: %v" + shouldNotHaveBeenZeroValue = "'%+v' should NOT have been the zero value" +) + +const ( // quantity comparisons + shouldHaveBeenGreater = "Expected '%v' to be greater than '%v' (but it wasn't)!" + shouldHaveBeenGreaterOrEqual = "Expected '%v' to be greater than or equal to '%v' (but it wasn't)!" + shouldHaveBeenLess = "Expected '%v' to be less than '%v' (but it wasn't)!" + shouldHaveBeenLessOrEqual = "Expected '%v' to be less than or equal to '%v' (but it wasn't)!" + shouldHaveBeenBetween = "Expected '%v' to be between '%v' and '%v' (but it wasn't)!" + shouldNotHaveBeenBetween = "Expected '%v' NOT to be between '%v' and '%v' (but it was)!" + shouldHaveDifferentUpperAndLower = "The lower and upper bounds must be different values (they were both '%v')." + shouldHaveBeenBetweenOrEqual = "Expected '%v' to be between '%v' and '%v' or equal to one of them (but it wasn't)!" + shouldNotHaveBeenBetweenOrEqual = "Expected '%v' NOT to be between '%v' and '%v' or equal to one of them (but it was)!" +) + +const ( // collections + shouldHaveContained = "Expected the container (%v) to contain: '%v' (but it didn't)!" + shouldNotHaveContained = "Expected the container (%v) NOT to contain: '%v' (but it did)!" + shouldHaveContainedKey = "Expected the %v to contain the key: %v (but it didn't)!" + shouldNotHaveContainedKey = "Expected the %v NOT to contain the key: %v (but it did)!" + shouldHaveBeenIn = "Expected '%v' to be in the container (%v), but it wasn't!" + shouldNotHaveBeenIn = "Expected '%v' NOT to be in the container (%v), but it was!" + shouldHaveBeenAValidCollection = "You must provide a valid container (was %v)!" + shouldHaveBeenAValidMap = "You must provide a valid map type (was %v)!" + shouldHaveBeenEmpty = "Expected %+v to be empty (but it wasn't)!" + shouldNotHaveBeenEmpty = "Expected %+v to NOT be empty (but it was)!" + shouldHaveBeenAValidInteger = "You must provide a valid integer (was %v)!" + shouldHaveBeenAValidLength = "You must provide a valid positive integer (was %v)!" + shouldHaveHadLength = "Expected collection to have length equal to [%v], but it's length was [%v] instead! contents: %+v" +) + +const ( // strings + shouldHaveStartedWith = "Expected '%v'\nto start with '%v'\n(but it didn't)!" + shouldNotHaveStartedWith = "Expected '%v'\nNOT to start with '%v'\n(but it did)!" + shouldHaveEndedWith = "Expected '%v'\nto end with '%v'\n(but it didn't)!" + shouldNotHaveEndedWith = "Expected '%v'\nNOT to end with '%v'\n(but it did)!" + shouldAllBeStrings = "All arguments to this assertion must be strings (you provided: %v)." + shouldBothBeStrings = "Both arguments to this assertion must be strings (you provided %v and %v)." + shouldBeString = "The argument to this assertion must be a string (you provided %v)." + shouldHaveContainedSubstring = "Expected '%s' to contain substring '%s' (but it didn't)!" + shouldNotHaveContainedSubstring = "Expected '%s' NOT to contain substring '%s' (but it did)!" + shouldHaveBeenBlank = "Expected '%s' to be blank (but it wasn't)!" + shouldNotHaveBeenBlank = "Expected value to NOT be blank (but it was)!" +) + +const ( // panics + shouldUseVoidNiladicFunction = "You must provide a void, niladic function as the first argument!" + shouldHavePanickedWith = "Expected func() to panic with '%v' (but it panicked with '%v')!" + shouldHavePanicked = "Expected func() to panic (but it didn't)!" + shouldNotHavePanicked = "Expected func() NOT to panic (error: '%+v')!" + shouldNotHavePanickedWith = "Expected func() NOT to panic with '%v' (but it did)!" +) + +const ( // type checking + shouldHaveBeenA = "Expected '%v' to be: '%v' (but was: '%v')!" + shouldNotHaveBeenA = "Expected '%v' to NOT be: '%v' (but it was)!" + + shouldHaveImplemented = "Expected: '%v interface support'\nActual: '%v' does not implement the interface!" + shouldNotHaveImplemented = "Expected '%v'\nto NOT implement '%v'\n(but it did)!" + shouldCompareWithInterfacePointer = "The expected value must be a pointer to an interface type (eg. *fmt.Stringer)" + shouldNotBeNilActual = "The actual value was 'nil' and should be a value or a pointer to a value!" + + shouldBeError = "Expected an error value (but was '%v' instead)!" + shouldBeErrorInvalidComparisonValue = "The final argument to this assertion must be a string or an error value (you provided: '%v')." +) + +const ( // time comparisons + shouldUseTimes = "You must provide time instances as arguments to this assertion." + shouldUseTimeSlice = "You must provide a slice of time instances as the first argument to this assertion." + shouldUseDurationAndTime = "You must provide a duration and a time as arguments to this assertion." + shouldHaveHappenedBefore = "Expected '%v' to happen before '%v' (it happened '%v' after)!" + shouldHaveHappenedAfter = "Expected '%v' to happen after '%v' (it happened '%v' before)!" + shouldHaveHappenedBetween = "Expected '%v' to happen between '%v' and '%v' (it happened '%v' outside threshold)!" + shouldNotHaveHappenedOnOrBetween = "Expected '%v' to NOT happen on or between '%v' and '%v' (but it did)!" + + // format params: incorrect-index, previous-index, previous-time, incorrect-index, incorrect-time + shouldHaveBeenChronological = "The 'Time' at index [%d] should have happened after the previous one (but it didn't!):\n [%d]: %s\n [%d]: %s (see, it happened before!)" +) diff --git a/vendor/github.com/smartystreets/assertions/panic.go b/vendor/github.com/smartystreets/assertions/panic.go new file mode 100644 index 0000000000..7e75db1784 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/panic.go @@ -0,0 +1,115 @@ +package assertions + +import "fmt" + +// ShouldPanic receives a void, niladic function and expects to recover a panic. +func ShouldPanic(actual interface{}, expected ...interface{}) (message string) { + if fail := need(0, expected); fail != success { + return fail + } + + action, _ := actual.(func()) + + if action == nil { + message = shouldUseVoidNiladicFunction + return + } + + defer func() { + recovered := recover() + if recovered == nil { + message = shouldHavePanicked + } else { + message = success + } + }() + action() + + return +} + +// ShouldNotPanic receives a void, niladic function and expects to execute the function without any panic. +func ShouldNotPanic(actual interface{}, expected ...interface{}) (message string) { + if fail := need(0, expected); fail != success { + return fail + } + + action, _ := actual.(func()) + + if action == nil { + message = shouldUseVoidNiladicFunction + return + } + + defer func() { + recovered := recover() + if recovered != nil { + message = fmt.Sprintf(shouldNotHavePanicked, recovered) + } else { + message = success + } + }() + action() + + return +} + +// ShouldPanicWith receives a void, niladic function and expects to recover a panic with the second argument as the content. +func ShouldPanicWith(actual interface{}, expected ...interface{}) (message string) { + if fail := need(1, expected); fail != success { + return fail + } + + action, _ := actual.(func()) + + if action == nil { + message = shouldUseVoidNiladicFunction + return + } + + defer func() { + recovered := recover() + if recovered == nil { + message = shouldHavePanicked + } else { + if equal := ShouldEqual(recovered, expected[0]); equal != success { + message = serializer.serialize(expected[0], recovered, fmt.Sprintf(shouldHavePanickedWith, expected[0], recovered)) + } else { + message = success + } + } + }() + action() + + return +} + +// ShouldNotPanicWith receives a void, niladic function and expects to recover a panic whose content differs from the second argument. +func ShouldNotPanicWith(actual interface{}, expected ...interface{}) (message string) { + if fail := need(1, expected); fail != success { + return fail + } + + action, _ := actual.(func()) + + if action == nil { + message = shouldUseVoidNiladicFunction + return + } + + defer func() { + recovered := recover() + if recovered == nil { + message = success + } else { + if equal := ShouldEqual(recovered, expected[0]); equal == success { + message = fmt.Sprintf(shouldNotHavePanickedWith, expected[0]) + } else { + message = success + } + } + }() + action() + + return +} diff --git a/vendor/github.com/smartystreets/assertions/quantity.go b/vendor/github.com/smartystreets/assertions/quantity.go new file mode 100644 index 0000000000..f28b0a062b --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/quantity.go @@ -0,0 +1,141 @@ +package assertions + +import ( + "fmt" + + "github.com/smartystreets/assertions/internal/oglematchers" +) + +// ShouldBeGreaterThan receives exactly two parameters and ensures that the first is greater than the second. +func ShouldBeGreaterThan(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + if matchError := oglematchers.GreaterThan(expected[0]).Matches(actual); matchError != nil { + return fmt.Sprintf(shouldHaveBeenGreater, actual, expected[0]) + } + return success +} + +// ShouldBeGreaterThanOrEqualTo receives exactly two parameters and ensures that the first is greater than or equal to the second. +func ShouldBeGreaterThanOrEqualTo(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } else if matchError := oglematchers.GreaterOrEqual(expected[0]).Matches(actual); matchError != nil { + return fmt.Sprintf(shouldHaveBeenGreaterOrEqual, actual, expected[0]) + } + return success +} + +// ShouldBeLessThan receives exactly two parameters and ensures that the first is less than the second. +func ShouldBeLessThan(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } else if matchError := oglematchers.LessThan(expected[0]).Matches(actual); matchError != nil { + return fmt.Sprintf(shouldHaveBeenLess, actual, expected[0]) + } + return success +} + +// ShouldBeLessThan receives exactly two parameters and ensures that the first is less than or equal to the second. +func ShouldBeLessThanOrEqualTo(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } else if matchError := oglematchers.LessOrEqual(expected[0]).Matches(actual); matchError != nil { + return fmt.Sprintf(shouldHaveBeenLessOrEqual, actual, expected[0]) + } + return success +} + +// ShouldBeBetween receives exactly three parameters: an actual value, a lower bound, and an upper bound. +// It ensures that the actual value is between both bounds (but not equal to either of them). +func ShouldBeBetween(actual interface{}, expected ...interface{}) string { + if fail := need(2, expected); fail != success { + return fail + } + lower, upper, fail := deriveBounds(expected) + + if fail != success { + return fail + } else if !isBetween(actual, lower, upper) { + return fmt.Sprintf(shouldHaveBeenBetween, actual, lower, upper) + } + return success +} + +// ShouldNotBeBetween receives exactly three parameters: an actual value, a lower bound, and an upper bound. +// It ensures that the actual value is NOT between both bounds. +func ShouldNotBeBetween(actual interface{}, expected ...interface{}) string { + if fail := need(2, expected); fail != success { + return fail + } + lower, upper, fail := deriveBounds(expected) + + if fail != success { + return fail + } else if isBetween(actual, lower, upper) { + return fmt.Sprintf(shouldNotHaveBeenBetween, actual, lower, upper) + } + return success +} +func deriveBounds(values []interface{}) (lower interface{}, upper interface{}, fail string) { + lower = values[0] + upper = values[1] + + if ShouldNotEqual(lower, upper) != success { + return nil, nil, fmt.Sprintf(shouldHaveDifferentUpperAndLower, lower) + } else if ShouldBeLessThan(lower, upper) != success { + lower, upper = upper, lower + } + return lower, upper, success +} +func isBetween(value, lower, upper interface{}) bool { + if ShouldBeGreaterThan(value, lower) != success { + return false + } else if ShouldBeLessThan(value, upper) != success { + return false + } + return true +} + +// ShouldBeBetweenOrEqual receives exactly three parameters: an actual value, a lower bound, and an upper bound. +// It ensures that the actual value is between both bounds or equal to one of them. +func ShouldBeBetweenOrEqual(actual interface{}, expected ...interface{}) string { + if fail := need(2, expected); fail != success { + return fail + } + lower, upper, fail := deriveBounds(expected) + + if fail != success { + return fail + } else if !isBetweenOrEqual(actual, lower, upper) { + return fmt.Sprintf(shouldHaveBeenBetweenOrEqual, actual, lower, upper) + } + return success +} + +// ShouldNotBeBetweenOrEqual receives exactly three parameters: an actual value, a lower bound, and an upper bound. +// It ensures that the actual value is nopt between the bounds nor equal to either of them. +func ShouldNotBeBetweenOrEqual(actual interface{}, expected ...interface{}) string { + if fail := need(2, expected); fail != success { + return fail + } + lower, upper, fail := deriveBounds(expected) + + if fail != success { + return fail + } else if isBetweenOrEqual(actual, lower, upper) { + return fmt.Sprintf(shouldNotHaveBeenBetweenOrEqual, actual, lower, upper) + } + return success +} + +func isBetweenOrEqual(value, lower, upper interface{}) bool { + if ShouldBeGreaterThanOrEqualTo(value, lower) != success { + return false + } else if ShouldBeLessThanOrEqualTo(value, upper) != success { + return false + } + return true +} diff --git a/vendor/github.com/smartystreets/assertions/serializer.go b/vendor/github.com/smartystreets/assertions/serializer.go new file mode 100644 index 0000000000..fa32f9403f --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/serializer.go @@ -0,0 +1,63 @@ +package assertions + +import ( + "encoding/json" + "fmt" + + "github.com/smartystreets/assertions/internal/go-render/render" +) + +type Serializer interface { + serialize(expected, actual interface{}, message string) string + serializeDetailed(expected, actual interface{}, message string) string +} + +type failureSerializer struct{} + +func (self *failureSerializer) serializeDetailed(expected, actual interface{}, message string) string { + view := FailureView{ + Message: message, + Expected: render.Render(expected), + Actual: render.Render(actual), + } + serialized, _ := json.Marshal(view) + return string(serialized) +} + +func (self *failureSerializer) serialize(expected, actual interface{}, message string) string { + view := FailureView{ + Message: message, + Expected: fmt.Sprintf("%+v", expected), + Actual: fmt.Sprintf("%+v", actual), + } + serialized, _ := json.Marshal(view) + return string(serialized) +} + +func newSerializer() *failureSerializer { + return &failureSerializer{} +} + +/////////////////////////////////////////////////////////////////////////////// + +// This struct is also declared in github.com/smartystreets/goconvey/convey/reporting. +// The json struct tags should be equal in both declarations. +type FailureView struct { + Message string `json:"Message"` + Expected string `json:"Expected"` + Actual string `json:"Actual"` +} + +/////////////////////////////////////////////////////// + +// noopSerializer just gives back the original message. This is useful when we are using +// the assertions from a context other than the GoConvey Web UI, that requires the JSON +// structure provided by the failureSerializer. +type noopSerializer struct{} + +func (self *noopSerializer) serialize(expected, actual interface{}, message string) string { + return message +} +func (self *noopSerializer) serializeDetailed(expected, actual interface{}, message string) string { + return message +} diff --git a/vendor/github.com/smartystreets/assertions/strings.go b/vendor/github.com/smartystreets/assertions/strings.go new file mode 100644 index 0000000000..dbc3f04790 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/strings.go @@ -0,0 +1,227 @@ +package assertions + +import ( + "fmt" + "reflect" + "strings" +) + +// ShouldStartWith receives exactly 2 string parameters and ensures that the first starts with the second. +func ShouldStartWith(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + value, valueIsString := actual.(string) + prefix, prefixIsString := expected[0].(string) + + if !valueIsString || !prefixIsString { + return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0])) + } + + return shouldStartWith(value, prefix) +} +func shouldStartWith(value, prefix string) string { + if !strings.HasPrefix(value, prefix) { + shortval := value + if len(shortval) > len(prefix) { + shortval = shortval[:len(prefix)] + "..." + } + return serializer.serialize(prefix, shortval, fmt.Sprintf(shouldHaveStartedWith, value, prefix)) + } + return success +} + +// ShouldNotStartWith receives exactly 2 string parameters and ensures that the first does not start with the second. +func ShouldNotStartWith(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + value, valueIsString := actual.(string) + prefix, prefixIsString := expected[0].(string) + + if !valueIsString || !prefixIsString { + return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0])) + } + + return shouldNotStartWith(value, prefix) +} +func shouldNotStartWith(value, prefix string) string { + if strings.HasPrefix(value, prefix) { + if value == "" { + value = "" + } + if prefix == "" { + prefix = "" + } + return fmt.Sprintf(shouldNotHaveStartedWith, value, prefix) + } + return success +} + +// ShouldEndWith receives exactly 2 string parameters and ensures that the first ends with the second. +func ShouldEndWith(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + value, valueIsString := actual.(string) + suffix, suffixIsString := expected[0].(string) + + if !valueIsString || !suffixIsString { + return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0])) + } + + return shouldEndWith(value, suffix) +} +func shouldEndWith(value, suffix string) string { + if !strings.HasSuffix(value, suffix) { + shortval := value + if len(shortval) > len(suffix) { + shortval = "..." + shortval[len(shortval)-len(suffix):] + } + return serializer.serialize(suffix, shortval, fmt.Sprintf(shouldHaveEndedWith, value, suffix)) + } + return success +} + +// ShouldEndWith receives exactly 2 string parameters and ensures that the first does not end with the second. +func ShouldNotEndWith(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + value, valueIsString := actual.(string) + suffix, suffixIsString := expected[0].(string) + + if !valueIsString || !suffixIsString { + return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0])) + } + + return shouldNotEndWith(value, suffix) +} +func shouldNotEndWith(value, suffix string) string { + if strings.HasSuffix(value, suffix) { + if value == "" { + value = "" + } + if suffix == "" { + suffix = "" + } + return fmt.Sprintf(shouldNotHaveEndedWith, value, suffix) + } + return success +} + +// ShouldContainSubstring receives exactly 2 string parameters and ensures that the first contains the second as a substring. +func ShouldContainSubstring(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + long, longOk := actual.(string) + short, shortOk := expected[0].(string) + + if !longOk || !shortOk { + return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0])) + } + + if !strings.Contains(long, short) { + return serializer.serialize(expected[0], actual, fmt.Sprintf(shouldHaveContainedSubstring, long, short)) + } + return success +} + +// ShouldNotContainSubstring receives exactly 2 string parameters and ensures that the first does NOT contain the second as a substring. +func ShouldNotContainSubstring(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + long, longOk := actual.(string) + short, shortOk := expected[0].(string) + + if !longOk || !shortOk { + return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0])) + } + + if strings.Contains(long, short) { + return fmt.Sprintf(shouldNotHaveContainedSubstring, long, short) + } + return success +} + +// ShouldBeBlank receives exactly 1 string parameter and ensures that it is equal to "". +func ShouldBeBlank(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } + value, ok := actual.(string) + if !ok { + return fmt.Sprintf(shouldBeString, reflect.TypeOf(actual)) + } + if value != "" { + return serializer.serialize("", value, fmt.Sprintf(shouldHaveBeenBlank, value)) + } + return success +} + +// ShouldNotBeBlank receives exactly 1 string parameter and ensures that it is equal to "". +func ShouldNotBeBlank(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } + value, ok := actual.(string) + if !ok { + return fmt.Sprintf(shouldBeString, reflect.TypeOf(actual)) + } + if value == "" { + return shouldNotHaveBeenBlank + } + return success +} + +// ShouldEqualWithout receives exactly 3 string parameters and ensures that the first is equal to the second +// after removing all instances of the third from the first using strings.Replace(first, third, "", -1). +func ShouldEqualWithout(actual interface{}, expected ...interface{}) string { + if fail := need(2, expected); fail != success { + return fail + } + actualString, ok1 := actual.(string) + expectedString, ok2 := expected[0].(string) + replace, ok3 := expected[1].(string) + + if !ok1 || !ok2 || !ok3 { + return fmt.Sprintf(shouldAllBeStrings, []reflect.Type{ + reflect.TypeOf(actual), + reflect.TypeOf(expected[0]), + reflect.TypeOf(expected[1]), + }) + } + + replaced := strings.Replace(actualString, replace, "", -1) + if replaced == expectedString { + return "" + } + + return fmt.Sprintf("Expected '%s' to equal '%s' but without any '%s' (but it didn't).", actualString, expectedString, replace) +} + +// ShouldEqualTrimSpace receives exactly 2 string parameters and ensures that the first is equal to the second +// after removing all leading and trailing whitespace using strings.TrimSpace(first). +func ShouldEqualTrimSpace(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + actualString, valueIsString := actual.(string) + _, value2IsString := expected[0].(string) + + if !valueIsString || !value2IsString { + return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0])) + } + + actualString = strings.TrimSpace(actualString) + return ShouldEqual(actualString, expected[0]) +} diff --git a/vendor/github.com/smartystreets/assertions/time.go b/vendor/github.com/smartystreets/assertions/time.go new file mode 100644 index 0000000000..7e05026143 --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/time.go @@ -0,0 +1,202 @@ +package assertions + +import ( + "fmt" + "time" +) + +// ShouldHappenBefore receives exactly 2 time.Time arguments and asserts that the first happens before the second. +func ShouldHappenBefore(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + actualTime, firstOk := actual.(time.Time) + expectedTime, secondOk := expected[0].(time.Time) + + if !firstOk || !secondOk { + return shouldUseTimes + } + + if !actualTime.Before(expectedTime) { + return fmt.Sprintf(shouldHaveHappenedBefore, actualTime, expectedTime, actualTime.Sub(expectedTime)) + } + + return success +} + +// ShouldHappenOnOrBefore receives exactly 2 time.Time arguments and asserts that the first happens on or before the second. +func ShouldHappenOnOrBefore(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + actualTime, firstOk := actual.(time.Time) + expectedTime, secondOk := expected[0].(time.Time) + + if !firstOk || !secondOk { + return shouldUseTimes + } + + if actualTime.Equal(expectedTime) { + return success + } + return ShouldHappenBefore(actualTime, expectedTime) +} + +// ShouldHappenAfter receives exactly 2 time.Time arguments and asserts that the first happens after the second. +func ShouldHappenAfter(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + actualTime, firstOk := actual.(time.Time) + expectedTime, secondOk := expected[0].(time.Time) + + if !firstOk || !secondOk { + return shouldUseTimes + } + if !actualTime.After(expectedTime) { + return fmt.Sprintf(shouldHaveHappenedAfter, actualTime, expectedTime, expectedTime.Sub(actualTime)) + } + return success +} + +// ShouldHappenOnOrAfter receives exactly 2 time.Time arguments and asserts that the first happens on or after the second. +func ShouldHappenOnOrAfter(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + actualTime, firstOk := actual.(time.Time) + expectedTime, secondOk := expected[0].(time.Time) + + if !firstOk || !secondOk { + return shouldUseTimes + } + if actualTime.Equal(expectedTime) { + return success + } + return ShouldHappenAfter(actualTime, expectedTime) +} + +// ShouldHappenBetween receives exactly 3 time.Time arguments and asserts that the first happens between (not on) the second and third. +func ShouldHappenBetween(actual interface{}, expected ...interface{}) string { + if fail := need(2, expected); fail != success { + return fail + } + actualTime, firstOk := actual.(time.Time) + min, secondOk := expected[0].(time.Time) + max, thirdOk := expected[1].(time.Time) + + if !firstOk || !secondOk || !thirdOk { + return shouldUseTimes + } + + if !actualTime.After(min) { + return fmt.Sprintf(shouldHaveHappenedBetween, actualTime, min, max, min.Sub(actualTime)) + } + if !actualTime.Before(max) { + return fmt.Sprintf(shouldHaveHappenedBetween, actualTime, min, max, actualTime.Sub(max)) + } + return success +} + +// ShouldHappenOnOrBetween receives exactly 3 time.Time arguments and asserts that the first happens between or on the second and third. +func ShouldHappenOnOrBetween(actual interface{}, expected ...interface{}) string { + if fail := need(2, expected); fail != success { + return fail + } + actualTime, firstOk := actual.(time.Time) + min, secondOk := expected[0].(time.Time) + max, thirdOk := expected[1].(time.Time) + + if !firstOk || !secondOk || !thirdOk { + return shouldUseTimes + } + if actualTime.Equal(min) || actualTime.Equal(max) { + return success + } + return ShouldHappenBetween(actualTime, min, max) +} + +// ShouldNotHappenOnOrBetween receives exactly 3 time.Time arguments and asserts that the first +// does NOT happen between or on the second or third. +func ShouldNotHappenOnOrBetween(actual interface{}, expected ...interface{}) string { + if fail := need(2, expected); fail != success { + return fail + } + actualTime, firstOk := actual.(time.Time) + min, secondOk := expected[0].(time.Time) + max, thirdOk := expected[1].(time.Time) + + if !firstOk || !secondOk || !thirdOk { + return shouldUseTimes + } + if actualTime.Equal(min) || actualTime.Equal(max) { + return fmt.Sprintf(shouldNotHaveHappenedOnOrBetween, actualTime, min, max) + } + if actualTime.After(min) && actualTime.Before(max) { + return fmt.Sprintf(shouldNotHaveHappenedOnOrBetween, actualTime, min, max) + } + return success +} + +// ShouldHappenWithin receives a time.Time, a time.Duration, and a time.Time (3 arguments) +// and asserts that the first time.Time happens within or on the duration specified relative to +// the other time.Time. +func ShouldHappenWithin(actual interface{}, expected ...interface{}) string { + if fail := need(2, expected); fail != success { + return fail + } + actualTime, firstOk := actual.(time.Time) + tolerance, secondOk := expected[0].(time.Duration) + threshold, thirdOk := expected[1].(time.Time) + + if !firstOk || !secondOk || !thirdOk { + return shouldUseDurationAndTime + } + + min := threshold.Add(-tolerance) + max := threshold.Add(tolerance) + return ShouldHappenOnOrBetween(actualTime, min, max) +} + +// ShouldNotHappenWithin receives a time.Time, a time.Duration, and a time.Time (3 arguments) +// and asserts that the first time.Time does NOT happen within or on the duration specified relative to +// the other time.Time. +func ShouldNotHappenWithin(actual interface{}, expected ...interface{}) string { + if fail := need(2, expected); fail != success { + return fail + } + actualTime, firstOk := actual.(time.Time) + tolerance, secondOk := expected[0].(time.Duration) + threshold, thirdOk := expected[1].(time.Time) + + if !firstOk || !secondOk || !thirdOk { + return shouldUseDurationAndTime + } + + min := threshold.Add(-tolerance) + max := threshold.Add(tolerance) + return ShouldNotHappenOnOrBetween(actualTime, min, max) +} + +// ShouldBeChronological receives a []time.Time slice and asserts that the are +// in chronological order starting with the first time.Time as the earliest. +func ShouldBeChronological(actual interface{}, expected ...interface{}) string { + if fail := need(0, expected); fail != success { + return fail + } + + times, ok := actual.([]time.Time) + if !ok { + return shouldUseTimeSlice + } + + var previous time.Time + for i, current := range times { + if i > 0 && current.Before(previous) { + return fmt.Sprintf(shouldHaveBeenChronological, + i, i-1, previous.String(), i, current.String()) + } + previous = current + } + return "" +} diff --git a/vendor/github.com/smartystreets/assertions/type.go b/vendor/github.com/smartystreets/assertions/type.go new file mode 100644 index 0000000000..d2d1dc864b --- /dev/null +++ b/vendor/github.com/smartystreets/assertions/type.go @@ -0,0 +1,134 @@ +package assertions + +import ( + "fmt" + "reflect" +) + +// ShouldHaveSameTypeAs receives exactly two parameters and compares their underlying types for equality. +func ShouldHaveSameTypeAs(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + first := reflect.TypeOf(actual) + second := reflect.TypeOf(expected[0]) + + if first != second { + return serializer.serialize(second, first, fmt.Sprintf(shouldHaveBeenA, actual, second, first)) + } + + return success +} + +// ShouldNotHaveSameTypeAs receives exactly two parameters and compares their underlying types for inequality. +func ShouldNotHaveSameTypeAs(actual interface{}, expected ...interface{}) string { + if fail := need(1, expected); fail != success { + return fail + } + + first := reflect.TypeOf(actual) + second := reflect.TypeOf(expected[0]) + + if (actual == nil && expected[0] == nil) || first == second { + return fmt.Sprintf(shouldNotHaveBeenA, actual, second) + } + return success +} + +// ShouldImplement receives exactly two parameters and ensures +// that the first implements the interface type of the second. +func ShouldImplement(actual interface{}, expectedList ...interface{}) string { + if fail := need(1, expectedList); fail != success { + return fail + } + + expected := expectedList[0] + if fail := ShouldBeNil(expected); fail != success { + return shouldCompareWithInterfacePointer + } + + if fail := ShouldNotBeNil(actual); fail != success { + return shouldNotBeNilActual + } + + var actualType reflect.Type + if reflect.TypeOf(actual).Kind() != reflect.Ptr { + actualType = reflect.PtrTo(reflect.TypeOf(actual)) + } else { + actualType = reflect.TypeOf(actual) + } + + expectedType := reflect.TypeOf(expected) + if fail := ShouldNotBeNil(expectedType); fail != success { + return shouldCompareWithInterfacePointer + } + + expectedInterface := expectedType.Elem() + + if !actualType.Implements(expectedInterface) { + return fmt.Sprintf(shouldHaveImplemented, expectedInterface, actualType) + } + return success +} + +// ShouldNotImplement receives exactly two parameters and ensures +// that the first does NOT implement the interface type of the second. +func ShouldNotImplement(actual interface{}, expectedList ...interface{}) string { + if fail := need(1, expectedList); fail != success { + return fail + } + + expected := expectedList[0] + if fail := ShouldBeNil(expected); fail != success { + return shouldCompareWithInterfacePointer + } + + if fail := ShouldNotBeNil(actual); fail != success { + return shouldNotBeNilActual + } + + var actualType reflect.Type + if reflect.TypeOf(actual).Kind() != reflect.Ptr { + actualType = reflect.PtrTo(reflect.TypeOf(actual)) + } else { + actualType = reflect.TypeOf(actual) + } + + expectedType := reflect.TypeOf(expected) + if fail := ShouldNotBeNil(expectedType); fail != success { + return shouldCompareWithInterfacePointer + } + + expectedInterface := expectedType.Elem() + + if actualType.Implements(expectedInterface) { + return fmt.Sprintf(shouldNotHaveImplemented, actualType, expectedInterface) + } + return success +} + +// ShouldBeError asserts that the first argument implements the error interface. +// It also compares the first argument against the second argument if provided +// (which must be an error message string or another error value). +func ShouldBeError(actual interface{}, expected ...interface{}) string { + if fail := atMost(1, expected); fail != success { + return fail + } + + if !isError(actual) { + return fmt.Sprintf(shouldBeError, reflect.TypeOf(actual)) + } + + if len(expected) == 0 { + return success + } + + if expected := expected[0]; !isString(expected) && !isError(expected) { + return fmt.Sprintf(shouldBeErrorInvalidComparisonValue, reflect.TypeOf(expected)) + } + return ShouldEqual(fmt.Sprint(actual), fmt.Sprint(expected[0])) +} + +func isString(value interface{}) bool { _, ok := value.(string); return ok } +func isError(value interface{}) bool { _, ok := value.(error); return ok } diff --git a/vendor/github.com/smartystreets/goconvey/LICENSE.md b/vendor/github.com/smartystreets/goconvey/LICENSE.md new file mode 100644 index 0000000000..3f87a40e77 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/LICENSE.md @@ -0,0 +1,23 @@ +Copyright (c) 2016 SmartyStreets, LLC + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +NOTE: Various optional and subordinate components carry their own licensing +requirements and restrictions. Use of those components is subject to the terms +and conditions outlined the respective license of each component. diff --git a/vendor/github.com/smartystreets/goconvey/convey/assertions.go b/vendor/github.com/smartystreets/goconvey/convey/assertions.go new file mode 100644 index 0000000000..97e3bec82e --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/assertions.go @@ -0,0 +1,71 @@ +package convey + +import "github.com/smartystreets/assertions" + +var ( + ShouldEqual = assertions.ShouldEqual + ShouldNotEqual = assertions.ShouldNotEqual + ShouldAlmostEqual = assertions.ShouldAlmostEqual + ShouldNotAlmostEqual = assertions.ShouldNotAlmostEqual + ShouldResemble = assertions.ShouldResemble + ShouldNotResemble = assertions.ShouldNotResemble + ShouldPointTo = assertions.ShouldPointTo + ShouldNotPointTo = assertions.ShouldNotPointTo + ShouldBeNil = assertions.ShouldBeNil + ShouldNotBeNil = assertions.ShouldNotBeNil + ShouldBeTrue = assertions.ShouldBeTrue + ShouldBeFalse = assertions.ShouldBeFalse + ShouldBeZeroValue = assertions.ShouldBeZeroValue + ShouldNotBeZeroValue = assertions.ShouldNotBeZeroValue + + ShouldBeGreaterThan = assertions.ShouldBeGreaterThan + ShouldBeGreaterThanOrEqualTo = assertions.ShouldBeGreaterThanOrEqualTo + ShouldBeLessThan = assertions.ShouldBeLessThan + ShouldBeLessThanOrEqualTo = assertions.ShouldBeLessThanOrEqualTo + ShouldBeBetween = assertions.ShouldBeBetween + ShouldNotBeBetween = assertions.ShouldNotBeBetween + ShouldBeBetweenOrEqual = assertions.ShouldBeBetweenOrEqual + ShouldNotBeBetweenOrEqual = assertions.ShouldNotBeBetweenOrEqual + + ShouldContain = assertions.ShouldContain + ShouldNotContain = assertions.ShouldNotContain + ShouldContainKey = assertions.ShouldContainKey + ShouldNotContainKey = assertions.ShouldNotContainKey + ShouldBeIn = assertions.ShouldBeIn + ShouldNotBeIn = assertions.ShouldNotBeIn + ShouldBeEmpty = assertions.ShouldBeEmpty + ShouldNotBeEmpty = assertions.ShouldNotBeEmpty + ShouldHaveLength = assertions.ShouldHaveLength + + ShouldStartWith = assertions.ShouldStartWith + ShouldNotStartWith = assertions.ShouldNotStartWith + ShouldEndWith = assertions.ShouldEndWith + ShouldNotEndWith = assertions.ShouldNotEndWith + ShouldBeBlank = assertions.ShouldBeBlank + ShouldNotBeBlank = assertions.ShouldNotBeBlank + ShouldContainSubstring = assertions.ShouldContainSubstring + ShouldNotContainSubstring = assertions.ShouldNotContainSubstring + + ShouldPanic = assertions.ShouldPanic + ShouldNotPanic = assertions.ShouldNotPanic + ShouldPanicWith = assertions.ShouldPanicWith + ShouldNotPanicWith = assertions.ShouldNotPanicWith + + ShouldHaveSameTypeAs = assertions.ShouldHaveSameTypeAs + ShouldNotHaveSameTypeAs = assertions.ShouldNotHaveSameTypeAs + ShouldImplement = assertions.ShouldImplement + ShouldNotImplement = assertions.ShouldNotImplement + + ShouldHappenBefore = assertions.ShouldHappenBefore + ShouldHappenOnOrBefore = assertions.ShouldHappenOnOrBefore + ShouldHappenAfter = assertions.ShouldHappenAfter + ShouldHappenOnOrAfter = assertions.ShouldHappenOnOrAfter + ShouldHappenBetween = assertions.ShouldHappenBetween + ShouldHappenOnOrBetween = assertions.ShouldHappenOnOrBetween + ShouldNotHappenOnOrBetween = assertions.ShouldNotHappenOnOrBetween + ShouldHappenWithin = assertions.ShouldHappenWithin + ShouldNotHappenWithin = assertions.ShouldNotHappenWithin + ShouldBeChronological = assertions.ShouldBeChronological + + ShouldBeError = assertions.ShouldBeError +) diff --git a/vendor/github.com/smartystreets/goconvey/convey/context.go b/vendor/github.com/smartystreets/goconvey/convey/context.go new file mode 100644 index 0000000000..2c75c2d7b1 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/context.go @@ -0,0 +1,272 @@ +package convey + +import ( + "fmt" + + "github.com/jtolds/gls" + "github.com/smartystreets/goconvey/convey/reporting" +) + +type conveyErr struct { + fmt string + params []interface{} +} + +func (e *conveyErr) Error() string { + return fmt.Sprintf(e.fmt, e.params...) +} + +func conveyPanic(fmt string, params ...interface{}) { + panic(&conveyErr{fmt, params}) +} + +const ( + missingGoTest = `Top-level calls to Convey(...) need a reference to the *testing.T. + Hint: Convey("description here", t, func() { /* notice that the second argument was the *testing.T (t)! */ }) ` + extraGoTest = `Only the top-level call to Convey(...) needs a reference to the *testing.T.` + noStackContext = "Convey operation made without context on goroutine stack.\n" + + "Hint: Perhaps you meant to use `Convey(..., func(c C){...})` ?" + differentConveySituations = "Different set of Convey statements on subsequent pass!\nDid not expect %#v." + multipleIdenticalConvey = "Multiple convey suites with identical names: %#v" +) + +const ( + failureHalt = "___FAILURE_HALT___" + + nodeKey = "node" +) + +///////////////////////////////// Stack Context ///////////////////////////////// + +func getCurrentContext() *context { + ctx, ok := ctxMgr.GetValue(nodeKey) + if ok { + return ctx.(*context) + } + return nil +} + +func mustGetCurrentContext() *context { + ctx := getCurrentContext() + if ctx == nil { + conveyPanic(noStackContext) + } + return ctx +} + +//////////////////////////////////// Context //////////////////////////////////// + +// context magically handles all coordination of Convey's and So assertions. +// +// It is tracked on the stack as goroutine-local-storage with the gls package, +// or explicitly if the user decides to call convey like: +// +// Convey(..., func(c C) { +// c.So(...) +// }) +// +// This implements the `C` interface. +type context struct { + reporter reporting.Reporter + + children map[string]*context + + resets []func() + + executedOnce bool + expectChildRun *bool + complete bool + + focus bool + failureMode FailureMode +} + +// rootConvey is the main entry point to a test suite. This is called when +// there's no context in the stack already, and items must contain a `t` object, +// or this panics. +func rootConvey(items ...interface{}) { + entry := discover(items) + + if entry.Test == nil { + conveyPanic(missingGoTest) + } + + expectChildRun := true + ctx := &context{ + reporter: buildReporter(), + + children: make(map[string]*context), + + expectChildRun: &expectChildRun, + + focus: entry.Focus, + failureMode: defaultFailureMode.combine(entry.FailMode), + } + ctxMgr.SetValues(gls.Values{nodeKey: ctx}, func() { + ctx.reporter.BeginStory(reporting.NewStoryReport(entry.Test)) + defer ctx.reporter.EndStory() + + for ctx.shouldVisit() { + ctx.conveyInner(entry.Situation, entry.Func) + expectChildRun = true + } + }) +} + +//////////////////////////////////// Methods //////////////////////////////////// + +func (ctx *context) SkipConvey(items ...interface{}) { + ctx.Convey(items, skipConvey) +} + +func (ctx *context) FocusConvey(items ...interface{}) { + ctx.Convey(items, focusConvey) +} + +func (ctx *context) Convey(items ...interface{}) { + entry := discover(items) + + // we're a branch, or leaf (on the wind) + if entry.Test != nil { + conveyPanic(extraGoTest) + } + if ctx.focus && !entry.Focus { + return + } + + var inner_ctx *context + if ctx.executedOnce { + var ok bool + inner_ctx, ok = ctx.children[entry.Situation] + if !ok { + conveyPanic(differentConveySituations, entry.Situation) + } + } else { + if _, ok := ctx.children[entry.Situation]; ok { + conveyPanic(multipleIdenticalConvey, entry.Situation) + } + inner_ctx = &context{ + reporter: ctx.reporter, + + children: make(map[string]*context), + + expectChildRun: ctx.expectChildRun, + + focus: entry.Focus, + failureMode: ctx.failureMode.combine(entry.FailMode), + } + ctx.children[entry.Situation] = inner_ctx + } + + if inner_ctx.shouldVisit() { + ctxMgr.SetValues(gls.Values{nodeKey: inner_ctx}, func() { + inner_ctx.conveyInner(entry.Situation, entry.Func) + }) + } +} + +func (ctx *context) SkipSo(stuff ...interface{}) { + ctx.assertionReport(reporting.NewSkipReport()) +} + +func (ctx *context) So(actual interface{}, assert assertion, expected ...interface{}) { + if result := assert(actual, expected...); result == assertionSuccess { + ctx.assertionReport(reporting.NewSuccessReport()) + } else { + ctx.assertionReport(reporting.NewFailureReport(result)) + } +} + +func (ctx *context) Reset(action func()) { + /* TODO: Failure mode configuration */ + ctx.resets = append(ctx.resets, action) +} + +func (ctx *context) Print(items ...interface{}) (int, error) { + fmt.Fprint(ctx.reporter, items...) + return fmt.Print(items...) +} + +func (ctx *context) Println(items ...interface{}) (int, error) { + fmt.Fprintln(ctx.reporter, items...) + return fmt.Println(items...) +} + +func (ctx *context) Printf(format string, items ...interface{}) (int, error) { + fmt.Fprintf(ctx.reporter, format, items...) + return fmt.Printf(format, items...) +} + +//////////////////////////////////// Private //////////////////////////////////// + +// shouldVisit returns true iff we should traverse down into a Convey. Note +// that just because we don't traverse a Convey this time, doesn't mean that +// we may not traverse it on a subsequent pass. +func (c *context) shouldVisit() bool { + return !c.complete && *c.expectChildRun +} + +// conveyInner is the function which actually executes the user's anonymous test +// function body. At this point, Convey or RootConvey has decided that this +// function should actually run. +func (ctx *context) conveyInner(situation string, f func(C)) { + // Record/Reset state for next time. + defer func() { + ctx.executedOnce = true + + // This is only needed at the leaves, but there's no harm in also setting it + // when returning from branch Convey's + *ctx.expectChildRun = false + }() + + // Set up+tear down our scope for the reporter + ctx.reporter.Enter(reporting.NewScopeReport(situation)) + defer ctx.reporter.Exit() + + // Recover from any panics in f, and assign the `complete` status for this + // node of the tree. + defer func() { + ctx.complete = true + if problem := recover(); problem != nil { + if problem, ok := problem.(*conveyErr); ok { + panic(problem) + } + if problem != failureHalt { + ctx.reporter.Report(reporting.NewErrorReport(problem)) + } + } else { + for _, child := range ctx.children { + if !child.complete { + ctx.complete = false + return + } + } + } + }() + + // Resets are registered as the `f` function executes, so nil them here. + // All resets are run in registration order (FIFO). + ctx.resets = []func(){} + defer func() { + for _, r := range ctx.resets { + // panics handled by the previous defer + r() + } + }() + + if f == nil { + // if f is nil, this was either a Convey(..., nil), or a SkipConvey + ctx.reporter.Report(reporting.NewSkipReport()) + } else { + f(ctx) + } +} + +// assertionReport is a helper for So and SkipSo which makes the report and +// then possibly panics, depending on the current context's failureMode. +func (ctx *context) assertionReport(r *reporting.AssertionResult) { + ctx.reporter.Report(r) + if r.Failure != "" && ctx.failureMode == FailureHalts { + panic(failureHalt) + } +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/convey.goconvey b/vendor/github.com/smartystreets/goconvey/convey/convey.goconvey new file mode 100644 index 0000000000..a2d9327dc9 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/convey.goconvey @@ -0,0 +1,4 @@ +#ignore +-timeout=1s +#-covermode=count +#-coverpkg=github.com/smartystreets/goconvey/convey,github.com/smartystreets/goconvey/convey/gotest,github.com/smartystreets/goconvey/convey/reporting \ No newline at end of file diff --git a/vendor/github.com/smartystreets/goconvey/convey/discovery.go b/vendor/github.com/smartystreets/goconvey/convey/discovery.go new file mode 100644 index 0000000000..eb8d4cb2ce --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/discovery.go @@ -0,0 +1,103 @@ +package convey + +type actionSpecifier uint8 + +const ( + noSpecifier actionSpecifier = iota + skipConvey + focusConvey +) + +type suite struct { + Situation string + Test t + Focus bool + Func func(C) // nil means skipped + FailMode FailureMode +} + +func newSuite(situation string, failureMode FailureMode, f func(C), test t, specifier actionSpecifier) *suite { + ret := &suite{ + Situation: situation, + Test: test, + Func: f, + FailMode: failureMode, + } + switch specifier { + case skipConvey: + ret.Func = nil + case focusConvey: + ret.Focus = true + } + return ret +} + +func discover(items []interface{}) *suite { + name, items := parseName(items) + test, items := parseGoTest(items) + failure, items := parseFailureMode(items) + action, items := parseAction(items) + specifier, items := parseSpecifier(items) + + if len(items) != 0 { + conveyPanic(parseError) + } + + return newSuite(name, failure, action, test, specifier) +} +func item(items []interface{}) interface{} { + if len(items) == 0 { + conveyPanic(parseError) + } + return items[0] +} +func parseName(items []interface{}) (string, []interface{}) { + if name, parsed := item(items).(string); parsed { + return name, items[1:] + } + conveyPanic(parseError) + panic("never get here") +} +func parseGoTest(items []interface{}) (t, []interface{}) { + if test, parsed := item(items).(t); parsed { + return test, items[1:] + } + return nil, items +} +func parseFailureMode(items []interface{}) (FailureMode, []interface{}) { + if mode, parsed := item(items).(FailureMode); parsed { + return mode, items[1:] + } + return FailureInherits, items +} +func parseAction(items []interface{}) (func(C), []interface{}) { + switch x := item(items).(type) { + case nil: + return nil, items[1:] + case func(C): + return x, items[1:] + case func(): + return func(C) { x() }, items[1:] + } + conveyPanic(parseError) + panic("never get here") +} +func parseSpecifier(items []interface{}) (actionSpecifier, []interface{}) { + if len(items) == 0 { + return noSpecifier, items + } + if spec, ok := items[0].(actionSpecifier); ok { + return spec, items[1:] + } + conveyPanic(parseError) + panic("never get here") +} + +// This interface allows us to pass the *testing.T struct +// throughout the internals of this package without ever +// having to import the "testing" package. +type t interface { + Fail() +} + +const parseError = "You must provide a name (string), then a *testing.T (if in outermost scope), an optional FailureMode, and then an action (func())." diff --git a/vendor/github.com/smartystreets/goconvey/convey/doc.go b/vendor/github.com/smartystreets/goconvey/convey/doc.go new file mode 100644 index 0000000000..e4f7b51a86 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/doc.go @@ -0,0 +1,218 @@ +// Package convey contains all of the public-facing entry points to this project. +// This means that it should never be required of the user to import any other +// packages from this project as they serve internal purposes. +package convey + +import "github.com/smartystreets/goconvey/convey/reporting" + +////////////////////////////////// suite ////////////////////////////////// + +// C is the Convey context which you can optionally obtain in your action +// by calling Convey like: +// +// Convey(..., func(c C) { +// ... +// }) +// +// See the documentation on Convey for more details. +// +// All methods in this context behave identically to the global functions of the +// same name in this package. +type C interface { + Convey(items ...interface{}) + SkipConvey(items ...interface{}) + FocusConvey(items ...interface{}) + + So(actual interface{}, assert assertion, expected ...interface{}) + SkipSo(stuff ...interface{}) + + Reset(action func()) + + Println(items ...interface{}) (int, error) + Print(items ...interface{}) (int, error) + Printf(format string, items ...interface{}) (int, error) +} + +// Convey is the method intended for use when declaring the scopes of +// a specification. Each scope has a description and a func() which may contain +// other calls to Convey(), Reset() or Should-style assertions. Convey calls can +// be nested as far as you see fit. +// +// IMPORTANT NOTE: The top-level Convey() within a Test method +// must conform to the following signature: +// +// Convey(description string, t *testing.T, action func()) +// +// All other calls should look like this (no need to pass in *testing.T): +// +// Convey(description string, action func()) +// +// Don't worry, goconvey will panic if you get it wrong so you can fix it. +// +// Additionally, you may explicitly obtain access to the Convey context by doing: +// +// Convey(description string, action func(c C)) +// +// You may need to do this if you want to pass the context through to a +// goroutine, or to close over the context in a handler to a library which +// calls your handler in a goroutine (httptest comes to mind). +// +// All Convey()-blocks also accept an optional parameter of FailureMode which sets +// how goconvey should treat failures for So()-assertions in the block and +// nested blocks. See the constants in this file for the available options. +// +// By default it will inherit from its parent block and the top-level blocks +// default to the FailureHalts setting. +// +// This parameter is inserted before the block itself: +// +// Convey(description string, t *testing.T, mode FailureMode, action func()) +// Convey(description string, mode FailureMode, action func()) +// +// See the examples package for, well, examples. +func Convey(items ...interface{}) { + if ctx := getCurrentContext(); ctx == nil { + rootConvey(items...) + } else { + ctx.Convey(items...) + } +} + +// SkipConvey is analogous to Convey except that the scope is not executed +// (which means that child scopes defined within this scope are not run either). +// The reporter will be notified that this step was skipped. +func SkipConvey(items ...interface{}) { + Convey(append(items, skipConvey)...) +} + +// FocusConvey is has the inverse effect of SkipConvey. If the top-level +// Convey is changed to `FocusConvey`, only nested scopes that are defined +// with FocusConvey will be run. The rest will be ignored completely. This +// is handy when debugging a large suite that runs a misbehaving function +// repeatedly as you can disable all but one of that function +// without swaths of `SkipConvey` calls, just a targeted chain of calls +// to FocusConvey. +func FocusConvey(items ...interface{}) { + Convey(append(items, focusConvey)...) +} + +// Reset registers a cleanup function to be run after each Convey() +// in the same scope. See the examples package for a simple use case. +func Reset(action func()) { + mustGetCurrentContext().Reset(action) +} + +/////////////////////////////////// Assertions /////////////////////////////////// + +// assertion is an alias for a function with a signature that the convey.So() +// method can handle. Any future or custom assertions should conform to this +// method signature. The return value should be an empty string if the assertion +// passes and a well-formed failure message if not. +type assertion func(actual interface{}, expected ...interface{}) string + +const assertionSuccess = "" + +// So is the means by which assertions are made against the system under test. +// The majority of exported names in the assertions package begin with the word +// 'Should' and describe how the first argument (actual) should compare with any +// of the final (expected) arguments. How many final arguments are accepted +// depends on the particular assertion that is passed in as the assert argument. +// See the examples package for use cases and the assertions package for +// documentation on specific assertion methods. A failing assertion will +// cause t.Fail() to be invoked--you should never call this method (or other +// failure-inducing methods) in your test code. Leave that to GoConvey. +func So(actual interface{}, assert assertion, expected ...interface{}) { + mustGetCurrentContext().So(actual, assert, expected...) +} + +// SkipSo is analogous to So except that the assertion that would have been passed +// to So is not executed and the reporter is notified that the assertion was skipped. +func SkipSo(stuff ...interface{}) { + mustGetCurrentContext().SkipSo() +} + +// FailureMode is a type which determines how the So() blocks should fail +// if their assertion fails. See constants further down for acceptable values +type FailureMode string + +const ( + + // FailureContinues is a failure mode which prevents failing + // So()-assertions from halting Convey-block execution, instead + // allowing the test to continue past failing So()-assertions. + FailureContinues FailureMode = "continue" + + // FailureHalts is the default setting for a top-level Convey()-block + // and will cause all failing So()-assertions to halt further execution + // in that test-arm and continue on to the next arm. + FailureHalts FailureMode = "halt" + + // FailureInherits is the default setting for failure-mode, it will + // default to the failure-mode of the parent block. You should never + // need to specify this mode in your tests.. + FailureInherits FailureMode = "inherits" +) + +func (f FailureMode) combine(other FailureMode) FailureMode { + if other == FailureInherits { + return f + } + return other +} + +var defaultFailureMode FailureMode = FailureHalts + +// SetDefaultFailureMode allows you to specify the default failure mode +// for all Convey blocks. It is meant to be used in an init function to +// allow the default mode to be changdd across all tests for an entire packgae +// but it can be used anywhere. +func SetDefaultFailureMode(mode FailureMode) { + if mode == FailureContinues || mode == FailureHalts { + defaultFailureMode = mode + } else { + panic("You may only use the constants named 'FailureContinues' and 'FailureHalts' as default failure modes.") + } +} + +//////////////////////////////////// Print functions //////////////////////////////////// + +// Print is analogous to fmt.Print (and it even calls fmt.Print). It ensures that +// output is aligned with the corresponding scopes in the web UI. +func Print(items ...interface{}) (written int, err error) { + return mustGetCurrentContext().Print(items...) +} + +// Print is analogous to fmt.Println (and it even calls fmt.Println). It ensures that +// output is aligned with the corresponding scopes in the web UI. +func Println(items ...interface{}) (written int, err error) { + return mustGetCurrentContext().Println(items...) +} + +// Print is analogous to fmt.Printf (and it even calls fmt.Printf). It ensures that +// output is aligned with the corresponding scopes in the web UI. +func Printf(format string, items ...interface{}) (written int, err error) { + return mustGetCurrentContext().Printf(format, items...) +} + +/////////////////////////////////////////////////////////////////////////////// + +// SuppressConsoleStatistics prevents automatic printing of console statistics. +// Calling PrintConsoleStatistics explicitly will force printing of statistics. +func SuppressConsoleStatistics() { + reporting.SuppressConsoleStatistics() +} + +// PrintConsoleStatistics may be called at any time to print assertion statistics. +// Generally, the best place to do this would be in a TestMain function, +// after all tests have been run. Something like this: +// +// func TestMain(m *testing.M) { +// convey.SuppressConsoleStatistics() +// result := m.Run() +// convey.PrintConsoleStatistics() +// os.Exit(result) +// } +// +func PrintConsoleStatistics() { + reporting.PrintConsoleStatistics() +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/gotest/utils.go b/vendor/github.com/smartystreets/goconvey/convey/gotest/utils.go new file mode 100644 index 0000000000..167c8fb74a --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/gotest/utils.go @@ -0,0 +1,28 @@ +// Package gotest contains internal functionality. Although this package +// contains one or more exported names it is not intended for public +// consumption. See the examples package for how to use this project. +package gotest + +import ( + "runtime" + "strings" +) + +func ResolveExternalCaller() (file string, line int, name string) { + var caller_id uintptr + callers := runtime.Callers(0, callStack) + + for x := 0; x < callers; x++ { + caller_id, file, line, _ = runtime.Caller(x) + if strings.HasSuffix(file, "_test.go") || strings.HasSuffix(file, "_tests.go") { + name = runtime.FuncForPC(caller_id).Name() + return + } + } + file, line, name = "", -1, "" + return // panic? +} + +const maxStackDepth = 100 // This had better be enough... + +var callStack []uintptr = make([]uintptr, maxStackDepth, maxStackDepth) diff --git a/vendor/github.com/smartystreets/goconvey/convey/init.go b/vendor/github.com/smartystreets/goconvey/convey/init.go new file mode 100644 index 0000000000..cb930a0db4 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/init.go @@ -0,0 +1,81 @@ +package convey + +import ( + "flag" + "os" + + "github.com/jtolds/gls" + "github.com/smartystreets/assertions" + "github.com/smartystreets/goconvey/convey/reporting" +) + +func init() { + assertions.GoConveyMode(true) + + declareFlags() + + ctxMgr = gls.NewContextManager() +} + +func declareFlags() { + flag.BoolVar(&json, "convey-json", false, "When true, emits results in JSON blocks. Default: 'false'") + flag.BoolVar(&silent, "convey-silent", false, "When true, all output from GoConvey is suppressed.") + flag.BoolVar(&story, "convey-story", false, "When true, emits story output, otherwise emits dot output. When not provided, this flag mirrors the value of the '-test.v' flag") + + if noStoryFlagProvided() { + story = verboseEnabled + } + + // FYI: flag.Parse() is called from the testing package. +} + +func noStoryFlagProvided() bool { + return !story && !storyDisabled +} + +func buildReporter() reporting.Reporter { + selectReporter := os.Getenv("GOCONVEY_REPORTER") + + switch { + case testReporter != nil: + return testReporter + case json || selectReporter == "json": + return reporting.BuildJsonReporter() + case silent || selectReporter == "silent": + return reporting.BuildSilentReporter() + case selectReporter == "dot": + // Story is turned on when verbose is set, so we need to check for dot reporter first. + return reporting.BuildDotReporter() + case story || selectReporter == "story": + return reporting.BuildStoryReporter() + default: + return reporting.BuildDotReporter() + } +} + +var ( + ctxMgr *gls.ContextManager + + // only set by internal tests + testReporter reporting.Reporter +) + +var ( + json bool + silent bool + story bool + + verboseEnabled = flagFound("-test.v=true") + storyDisabled = flagFound("-story=false") +) + +// flagFound parses the command line args manually for flags defined in other +// packages. Like the '-v' flag from the "testing" package, for instance. +func flagFound(flagValue string) bool { + for _, arg := range os.Args { + if arg == flagValue { + return true + } + } + return false +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/nilReporter.go b/vendor/github.com/smartystreets/goconvey/convey/nilReporter.go new file mode 100644 index 0000000000..777b2a5122 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/nilReporter.go @@ -0,0 +1,15 @@ +package convey + +import ( + "github.com/smartystreets/goconvey/convey/reporting" +) + +type nilReporter struct{} + +func (self *nilReporter) BeginStory(story *reporting.StoryReport) {} +func (self *nilReporter) Enter(scope *reporting.ScopeReport) {} +func (self *nilReporter) Report(report *reporting.AssertionResult) {} +func (self *nilReporter) Exit() {} +func (self *nilReporter) EndStory() {} +func (self *nilReporter) Write(p []byte) (int, error) { return len(p), nil } +func newNilReporter() *nilReporter { return &nilReporter{} } diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/console.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/console.go new file mode 100644 index 0000000000..7bf67dbb2b --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/console.go @@ -0,0 +1,16 @@ +package reporting + +import ( + "fmt" + "io" +) + +type console struct{} + +func (self *console) Write(p []byte) (n int, err error) { + return fmt.Print(string(p)) +} + +func NewConsole() io.Writer { + return new(console) +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/doc.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/doc.go new file mode 100644 index 0000000000..a37d001946 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/doc.go @@ -0,0 +1,5 @@ +// Package reporting contains internal functionality related +// to console reporting and output. Although this package has +// exported names is not intended for public consumption. See the +// examples package for how to use this project. +package reporting diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/dot.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/dot.go new file mode 100644 index 0000000000..47d57c6b0d --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/dot.go @@ -0,0 +1,40 @@ +package reporting + +import "fmt" + +type dot struct{ out *Printer } + +func (self *dot) BeginStory(story *StoryReport) {} + +func (self *dot) Enter(scope *ScopeReport) {} + +func (self *dot) Report(report *AssertionResult) { + if report.Error != nil { + fmt.Print(redColor) + self.out.Insert(dotError) + } else if report.Failure != "" { + fmt.Print(yellowColor) + self.out.Insert(dotFailure) + } else if report.Skipped { + fmt.Print(yellowColor) + self.out.Insert(dotSkip) + } else { + fmt.Print(greenColor) + self.out.Insert(dotSuccess) + } + fmt.Print(resetColor) +} + +func (self *dot) Exit() {} + +func (self *dot) EndStory() {} + +func (self *dot) Write(content []byte) (written int, err error) { + return len(content), nil // no-op +} + +func NewDotReporter(out *Printer) *dot { + self := new(dot) + self.out = out + return self +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/gotest.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/gotest.go new file mode 100644 index 0000000000..c396e16b17 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/gotest.go @@ -0,0 +1,33 @@ +package reporting + +type gotestReporter struct{ test T } + +func (self *gotestReporter) BeginStory(story *StoryReport) { + self.test = story.Test +} + +func (self *gotestReporter) Enter(scope *ScopeReport) {} + +func (self *gotestReporter) Report(r *AssertionResult) { + if !passed(r) { + self.test.Fail() + } +} + +func (self *gotestReporter) Exit() {} + +func (self *gotestReporter) EndStory() { + self.test = nil +} + +func (self *gotestReporter) Write(content []byte) (written int, err error) { + return len(content), nil // no-op +} + +func NewGoTestReporter() *gotestReporter { + return new(gotestReporter) +} + +func passed(r *AssertionResult) bool { + return r.Error == nil && r.Failure == "" +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/init.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/init.go new file mode 100644 index 0000000000..99c3bd6d61 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/init.go @@ -0,0 +1,94 @@ +package reporting + +import ( + "os" + "runtime" + "strings" +) + +func init() { + if !isColorableTerminal() { + monochrome() + } + + if runtime.GOOS == "windows" { + success, failure, error_ = dotSuccess, dotFailure, dotError + } +} + +func BuildJsonReporter() Reporter { + out := NewPrinter(NewConsole()) + return NewReporters( + NewGoTestReporter(), + NewJsonReporter(out)) +} +func BuildDotReporter() Reporter { + out := NewPrinter(NewConsole()) + return NewReporters( + NewGoTestReporter(), + NewDotReporter(out), + NewProblemReporter(out), + consoleStatistics) +} +func BuildStoryReporter() Reporter { + out := NewPrinter(NewConsole()) + return NewReporters( + NewGoTestReporter(), + NewStoryReporter(out), + NewProblemReporter(out), + consoleStatistics) +} +func BuildSilentReporter() Reporter { + out := NewPrinter(NewConsole()) + return NewReporters( + NewGoTestReporter(), + NewSilentProblemReporter(out)) +} + +var ( + newline = "\n" + success = "✔" + failure = "✘" + error_ = "🔥" + skip = "⚠" + dotSuccess = "." + dotFailure = "x" + dotError = "E" + dotSkip = "S" + errorTemplate = "* %s \nLine %d: - %v \n%s\n" + failureTemplate = "* %s \nLine %d:\n%s\n%s\n" +) + +var ( + greenColor = "\033[32m" + yellowColor = "\033[33m" + redColor = "\033[31m" + resetColor = "\033[0m" +) + +var consoleStatistics = NewStatisticsReporter(NewPrinter(NewConsole())) + +func SuppressConsoleStatistics() { consoleStatistics.Suppress() } +func PrintConsoleStatistics() { consoleStatistics.PrintSummary() } + +// QuietMode disables all console output symbols. This is only meant to be used +// for tests that are internal to goconvey where the output is distracting or +// otherwise not needed in the test output. +func QuietMode() { + success, failure, error_, skip, dotSuccess, dotFailure, dotError, dotSkip = "", "", "", "", "", "", "", "" +} + +func monochrome() { + greenColor, yellowColor, redColor, resetColor = "", "", "", "" +} + +func isColorableTerminal() bool { + return strings.Contains(os.Getenv("TERM"), "color") +} + +// This interface allows us to pass the *testing.T struct +// throughout the internals of this tool without ever +// having to import the "testing" package. +type T interface { + Fail() +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/json.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/json.go new file mode 100644 index 0000000000..f8526979f8 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/json.go @@ -0,0 +1,88 @@ +// TODO: under unit test + +package reporting + +import ( + "bytes" + "encoding/json" + "fmt" + "strings" +) + +type JsonReporter struct { + out *Printer + currentKey []string + current *ScopeResult + index map[string]*ScopeResult + scopes []*ScopeResult +} + +func (self *JsonReporter) depth() int { return len(self.currentKey) } + +func (self *JsonReporter) BeginStory(story *StoryReport) {} + +func (self *JsonReporter) Enter(scope *ScopeReport) { + self.currentKey = append(self.currentKey, scope.Title) + ID := strings.Join(self.currentKey, "|") + if _, found := self.index[ID]; !found { + next := newScopeResult(scope.Title, self.depth(), scope.File, scope.Line) + self.scopes = append(self.scopes, next) + self.index[ID] = next + } + self.current = self.index[ID] +} + +func (self *JsonReporter) Report(report *AssertionResult) { + self.current.Assertions = append(self.current.Assertions, report) +} + +func (self *JsonReporter) Exit() { + self.currentKey = self.currentKey[:len(self.currentKey)-1] +} + +func (self *JsonReporter) EndStory() { + self.report() + self.reset() +} +func (self *JsonReporter) report() { + scopes := []string{} + for _, scope := range self.scopes { + serialized, err := json.Marshal(scope) + if err != nil { + self.out.Println(jsonMarshalFailure) + panic(err) + } + var buffer bytes.Buffer + json.Indent(&buffer, serialized, "", " ") + scopes = append(scopes, buffer.String()) + } + self.out.Print(fmt.Sprintf("%s\n%s,\n%s\n", OpenJson, strings.Join(scopes, ","), CloseJson)) +} +func (self *JsonReporter) reset() { + self.scopes = []*ScopeResult{} + self.index = map[string]*ScopeResult{} + self.currentKey = nil +} + +func (self *JsonReporter) Write(content []byte) (written int, err error) { + self.current.Output += string(content) + return len(content), nil +} + +func NewJsonReporter(out *Printer) *JsonReporter { + self := new(JsonReporter) + self.out = out + self.reset() + return self +} + +const OpenJson = ">->->OPEN-JSON->->->" // "⌦" +const CloseJson = "<-<-<-CLOSE-JSON<-<-<" // "⌫" +const jsonMarshalFailure = ` + +GOCONVEY_JSON_MARSHALL_FAILURE: There was an error when attempting to convert test results to JSON. +Please file a bug report and reference the code that caused this failure if possible. + +Here's the panic: + +` diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/printer.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/printer.go new file mode 100644 index 0000000000..3dac0d4d28 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/printer.go @@ -0,0 +1,60 @@ +package reporting + +import ( + "fmt" + "io" + "strings" +) + +type Printer struct { + out io.Writer + prefix string +} + +func (self *Printer) Println(message string, values ...interface{}) { + formatted := self.format(message, values...) + newline + self.out.Write([]byte(formatted)) +} + +func (self *Printer) Print(message string, values ...interface{}) { + formatted := self.format(message, values...) + self.out.Write([]byte(formatted)) +} + +func (self *Printer) Insert(text string) { + self.out.Write([]byte(text)) +} + +func (self *Printer) format(message string, values ...interface{}) string { + var formatted string + if len(values) == 0 { + formatted = self.prefix + message + } else { + formatted = self.prefix + fmt_Sprintf(message, values...) + } + indented := strings.Replace(formatted, newline, newline+self.prefix, -1) + return strings.TrimRight(indented, space) +} + +// Extracting fmt.Sprintf to a separate variable circumvents go vet, which, as of go 1.10 is run with go test. +var fmt_Sprintf = fmt.Sprintf + +func (self *Printer) Indent() { + self.prefix += pad +} + +func (self *Printer) Dedent() { + if len(self.prefix) >= padLength { + self.prefix = self.prefix[:len(self.prefix)-padLength] + } +} + +func NewPrinter(out io.Writer) *Printer { + self := new(Printer) + self.out = out + return self +} + +const space = " " +const pad = space + space +const padLength = len(pad) diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/problems.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/problems.go new file mode 100644 index 0000000000..33d5e14767 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/problems.go @@ -0,0 +1,80 @@ +package reporting + +import "fmt" + +type problem struct { + silent bool + out *Printer + errors []*AssertionResult + failures []*AssertionResult +} + +func (self *problem) BeginStory(story *StoryReport) {} + +func (self *problem) Enter(scope *ScopeReport) {} + +func (self *problem) Report(report *AssertionResult) { + if report.Error != nil { + self.errors = append(self.errors, report) + } else if report.Failure != "" { + self.failures = append(self.failures, report) + } +} + +func (self *problem) Exit() {} + +func (self *problem) EndStory() { + self.show(self.showErrors, redColor) + self.show(self.showFailures, yellowColor) + self.prepareForNextStory() +} +func (self *problem) show(display func(), color string) { + if !self.silent { + fmt.Print(color) + } + display() + if !self.silent { + fmt.Print(resetColor) + } + self.out.Dedent() +} +func (self *problem) showErrors() { + for i, e := range self.errors { + if i == 0 { + self.out.Println("\nErrors:\n") + self.out.Indent() + } + self.out.Println(errorTemplate, e.File, e.Line, e.Error, e.StackTrace) + } +} +func (self *problem) showFailures() { + for i, f := range self.failures { + if i == 0 { + self.out.Println("\nFailures:\n") + self.out.Indent() + } + self.out.Println(failureTemplate, f.File, f.Line, f.Failure, f.StackTrace) + } +} + +func (self *problem) Write(content []byte) (written int, err error) { + return len(content), nil // no-op +} + +func NewProblemReporter(out *Printer) *problem { + self := new(problem) + self.out = out + self.prepareForNextStory() + return self +} + +func NewSilentProblemReporter(out *Printer) *problem { + self := NewProblemReporter(out) + self.silent = true + return self +} + +func (self *problem) prepareForNextStory() { + self.errors = []*AssertionResult{} + self.failures = []*AssertionResult{} +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/reporter.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/reporter.go new file mode 100644 index 0000000000..cce6c5e438 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/reporter.go @@ -0,0 +1,39 @@ +package reporting + +import "io" + +type Reporter interface { + BeginStory(story *StoryReport) + Enter(scope *ScopeReport) + Report(r *AssertionResult) + Exit() + EndStory() + io.Writer +} + +type reporters struct{ collection []Reporter } + +func (self *reporters) BeginStory(s *StoryReport) { self.foreach(func(r Reporter) { r.BeginStory(s) }) } +func (self *reporters) Enter(s *ScopeReport) { self.foreach(func(r Reporter) { r.Enter(s) }) } +func (self *reporters) Report(a *AssertionResult) { self.foreach(func(r Reporter) { r.Report(a) }) } +func (self *reporters) Exit() { self.foreach(func(r Reporter) { r.Exit() }) } +func (self *reporters) EndStory() { self.foreach(func(r Reporter) { r.EndStory() }) } + +func (self *reporters) Write(contents []byte) (written int, err error) { + self.foreach(func(r Reporter) { + written, err = r.Write(contents) + }) + return written, err +} + +func (self *reporters) foreach(action func(Reporter)) { + for _, r := range self.collection { + action(r) + } +} + +func NewReporters(collection ...Reporter) *reporters { + self := new(reporters) + self.collection = collection + return self +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/reporting.goconvey b/vendor/github.com/smartystreets/goconvey/convey/reporting/reporting.goconvey new file mode 100644 index 0000000000..79982854b5 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/reporting.goconvey @@ -0,0 +1,2 @@ +#ignore +-timeout=1s diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/reports.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/reports.go new file mode 100644 index 0000000000..712e6ade62 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/reports.go @@ -0,0 +1,179 @@ +package reporting + +import ( + "encoding/json" + "fmt" + "runtime" + "strings" + + "github.com/smartystreets/goconvey/convey/gotest" +) + +////////////////// ScopeReport //////////////////// + +type ScopeReport struct { + Title string + File string + Line int +} + +func NewScopeReport(title string) *ScopeReport { + file, line, _ := gotest.ResolveExternalCaller() + self := new(ScopeReport) + self.Title = title + self.File = file + self.Line = line + return self +} + +////////////////// ScopeResult //////////////////// + +type ScopeResult struct { + Title string + File string + Line int + Depth int + Assertions []*AssertionResult + Output string +} + +func newScopeResult(title string, depth int, file string, line int) *ScopeResult { + self := new(ScopeResult) + self.Title = title + self.Depth = depth + self.File = file + self.Line = line + self.Assertions = []*AssertionResult{} + return self +} + +/////////////////// StoryReport ///////////////////// + +type StoryReport struct { + Test T + Name string + File string + Line int +} + +func NewStoryReport(test T) *StoryReport { + file, line, name := gotest.ResolveExternalCaller() + name = removePackagePath(name) + self := new(StoryReport) + self.Test = test + self.Name = name + self.File = file + self.Line = line + return self +} + +// name comes in looking like "github.com/smartystreets/goconvey/examples.TestName". +// We only want the stuff after the last '.', which is the name of the test function. +func removePackagePath(name string) string { + parts := strings.Split(name, ".") + return parts[len(parts)-1] +} + +/////////////////// FailureView //////////////////////// + +// This struct is also declared in github.com/smartystreets/assertions. +// The json struct tags should be equal in both declarations. +type FailureView struct { + Message string `json:"Message"` + Expected string `json:"Expected"` + Actual string `json:"Actual"` +} + +////////////////////AssertionResult ////////////////////// + +type AssertionResult struct { + File string + Line int + Expected string + Actual string + Failure string + Error interface{} + StackTrace string + Skipped bool +} + +func NewFailureReport(failure string) *AssertionResult { + report := new(AssertionResult) + report.File, report.Line = caller() + report.StackTrace = stackTrace() + parseFailure(failure, report) + return report +} +func parseFailure(failure string, report *AssertionResult) { + view := new(FailureView) + err := json.Unmarshal([]byte(failure), view) + if err == nil { + report.Failure = view.Message + report.Expected = view.Expected + report.Actual = view.Actual + } else { + report.Failure = failure + } +} +func NewErrorReport(err interface{}) *AssertionResult { + report := new(AssertionResult) + report.File, report.Line = caller() + report.StackTrace = fullStackTrace() + report.Error = fmt.Sprintf("%v", err) + return report +} +func NewSuccessReport() *AssertionResult { + return new(AssertionResult) +} +func NewSkipReport() *AssertionResult { + report := new(AssertionResult) + report.File, report.Line = caller() + report.StackTrace = fullStackTrace() + report.Skipped = true + return report +} + +func caller() (file string, line int) { + file, line, _ = gotest.ResolveExternalCaller() + return +} + +func stackTrace() string { + buffer := make([]byte, 1024*64) + n := runtime.Stack(buffer, false) + return removeInternalEntries(string(buffer[:n])) +} +func fullStackTrace() string { + buffer := make([]byte, 1024*64) + n := runtime.Stack(buffer, true) + return removeInternalEntries(string(buffer[:n])) +} +func removeInternalEntries(stack string) string { + lines := strings.Split(stack, newline) + filtered := []string{} + for _, line := range lines { + if !isExternal(line) { + filtered = append(filtered, line) + } + } + return strings.Join(filtered, newline) +} +func isExternal(line string) bool { + for _, p := range internalPackages { + if strings.Contains(line, p) { + return true + } + } + return false +} + +// NOTE: any new packages that host goconvey packages will need to be added here! +// An alternative is to scan the goconvey directory and then exclude stuff like +// the examples package but that's nasty too. +var internalPackages = []string{ + "goconvey/assertions", + "goconvey/convey", + "goconvey/execution", + "goconvey/gotest", + "goconvey/reporting", +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/statistics.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/statistics.go new file mode 100644 index 0000000000..c3ccd056a0 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/statistics.go @@ -0,0 +1,108 @@ +package reporting + +import ( + "fmt" + "sync" +) + +func (self *statistics) BeginStory(story *StoryReport) {} + +func (self *statistics) Enter(scope *ScopeReport) {} + +func (self *statistics) Report(report *AssertionResult) { + self.Lock() + defer self.Unlock() + + if !self.failing && report.Failure != "" { + self.failing = true + } + if !self.erroring && report.Error != nil { + self.erroring = true + } + if report.Skipped { + self.skipped += 1 + } else { + self.total++ + } +} + +func (self *statistics) Exit() {} + +func (self *statistics) EndStory() { + self.Lock() + defer self.Unlock() + + if !self.suppressed { + self.printSummaryLocked() + } +} + +func (self *statistics) Suppress() { + self.Lock() + defer self.Unlock() + self.suppressed = true +} + +func (self *statistics) PrintSummary() { + self.Lock() + defer self.Unlock() + self.printSummaryLocked() +} + +func (self *statistics) printSummaryLocked() { + self.reportAssertionsLocked() + self.reportSkippedSectionsLocked() + self.completeReportLocked() +} +func (self *statistics) reportAssertionsLocked() { + self.decideColorLocked() + self.out.Print("\n%d total %s", self.total, plural("assertion", self.total)) +} +func (self *statistics) decideColorLocked() { + if self.failing && !self.erroring { + fmt.Print(yellowColor) + } else if self.erroring { + fmt.Print(redColor) + } else { + fmt.Print(greenColor) + } +} +func (self *statistics) reportSkippedSectionsLocked() { + if self.skipped > 0 { + fmt.Print(yellowColor) + self.out.Print(" (one or more sections skipped)") + } +} +func (self *statistics) completeReportLocked() { + fmt.Print(resetColor) + self.out.Print("\n") + self.out.Print("\n") +} + +func (self *statistics) Write(content []byte) (written int, err error) { + return len(content), nil // no-op +} + +func NewStatisticsReporter(out *Printer) *statistics { + self := statistics{} + self.out = out + return &self +} + +type statistics struct { + sync.Mutex + + out *Printer + total int + failing bool + erroring bool + skipped int + suppressed bool +} + +func plural(word string, count int) string { + if count == 1 { + return word + } + return word + "s" +} diff --git a/vendor/github.com/smartystreets/goconvey/convey/reporting/story.go b/vendor/github.com/smartystreets/goconvey/convey/reporting/story.go new file mode 100644 index 0000000000..9e73c971f8 --- /dev/null +++ b/vendor/github.com/smartystreets/goconvey/convey/reporting/story.go @@ -0,0 +1,73 @@ +// TODO: in order for this reporter to be completely honest +// we need to retrofit to be more like the json reporter such that: +// 1. it maintains ScopeResult collections, which count assertions +// 2. it reports only after EndStory(), so that all tick marks +// are placed near the appropriate title. +// 3. Under unit test + +package reporting + +import ( + "fmt" + "strings" +) + +type story struct { + out *Printer + titlesById map[string]string + currentKey []string +} + +func (self *story) BeginStory(story *StoryReport) {} + +func (self *story) Enter(scope *ScopeReport) { + self.out.Indent() + + self.currentKey = append(self.currentKey, scope.Title) + ID := strings.Join(self.currentKey, "|") + + if _, found := self.titlesById[ID]; !found { + self.out.Println("") + self.out.Print(scope.Title) + self.out.Insert(" ") + self.titlesById[ID] = scope.Title + } +} + +func (self *story) Report(report *AssertionResult) { + if report.Error != nil { + fmt.Print(redColor) + self.out.Insert(error_) + } else if report.Failure != "" { + fmt.Print(yellowColor) + self.out.Insert(failure) + } else if report.Skipped { + fmt.Print(yellowColor) + self.out.Insert(skip) + } else { + fmt.Print(greenColor) + self.out.Insert(success) + } + fmt.Print(resetColor) +} + +func (self *story) Exit() { + self.out.Dedent() + self.currentKey = self.currentKey[:len(self.currentKey)-1] +} + +func (self *story) EndStory() { + self.titlesById = make(map[string]string) + self.out.Println("\n") +} + +func (self *story) Write(content []byte) (written int, err error) { + return len(content), nil // no-op +} + +func NewStoryReporter(out *Printer) *story { + self := new(story) + self.out = out + self.titlesById = make(map[string]string) + return self +} diff --git a/vendor/golang.org/x/xerrors/LICENSE b/vendor/golang.org/x/xerrors/LICENSE new file mode 100644 index 0000000000..e4a47e17f1 --- /dev/null +++ b/vendor/golang.org/x/xerrors/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2019 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/golang.org/x/xerrors/PATENTS b/vendor/golang.org/x/xerrors/PATENTS new file mode 100644 index 0000000000..733099041f --- /dev/null +++ b/vendor/golang.org/x/xerrors/PATENTS @@ -0,0 +1,22 @@ +Additional IP Rights Grant (Patents) + +"This implementation" means the copyrightable works distributed by +Google as part of the Go project. + +Google hereby grants to You a perpetual, worldwide, non-exclusive, +no-charge, royalty-free, irrevocable (except as stated in this section) +patent license to make, have made, use, offer to sell, sell, import, +transfer and otherwise run, modify and propagate the contents of this +implementation of Go, where such license applies only to those patent +claims, both currently owned or controlled by Google and acquired in +the future, licensable by Google that are necessarily infringed by this +implementation of Go. This grant does not include claims that would be +infringed only as a consequence of further modification of this +implementation. If you or your agent or exclusive licensee institute or +order or agree to the institution of patent litigation against any +entity (including a cross-claim or counterclaim in a lawsuit) alleging +that this implementation of Go or any code incorporated within this +implementation of Go constitutes direct or contributory patent +infringement, or inducement of patent infringement, then any patent +rights granted to you under this License for this implementation of Go +shall terminate as of the date such litigation is filed. diff --git a/vendor/golang.org/x/xerrors/README b/vendor/golang.org/x/xerrors/README new file mode 100644 index 0000000000..aac7867a56 --- /dev/null +++ b/vendor/golang.org/x/xerrors/README @@ -0,0 +1,2 @@ +This repository holds the transition packages for the new Go 1.13 error values. +See golang.org/design/29934-error-values. diff --git a/vendor/golang.org/x/xerrors/adaptor.go b/vendor/golang.org/x/xerrors/adaptor.go new file mode 100644 index 0000000000..4317f24833 --- /dev/null +++ b/vendor/golang.org/x/xerrors/adaptor.go @@ -0,0 +1,193 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package xerrors + +import ( + "bytes" + "fmt" + "io" + "reflect" + "strconv" +) + +// FormatError calls the FormatError method of f with an errors.Printer +// configured according to s and verb, and writes the result to s. +func FormatError(f Formatter, s fmt.State, verb rune) { + // Assuming this function is only called from the Format method, and given + // that FormatError takes precedence over Format, it cannot be called from + // any package that supports errors.Formatter. It is therefore safe to + // disregard that State may be a specific printer implementation and use one + // of our choice instead. + + // limitations: does not support printing error as Go struct. + + var ( + sep = " " // separator before next error + p = &state{State: s} + direct = true + ) + + var err error = f + + switch verb { + // Note that this switch must match the preference order + // for ordinary string printing (%#v before %+v, and so on). + + case 'v': + if s.Flag('#') { + if stringer, ok := err.(fmt.GoStringer); ok { + io.WriteString(&p.buf, stringer.GoString()) + goto exit + } + // proceed as if it were %v + } else if s.Flag('+') { + p.printDetail = true + sep = "\n - " + } + case 's': + case 'q', 'x', 'X': + // Use an intermediate buffer in the rare cases that precision, + // truncation, or one of the alternative verbs (q, x, and X) are + // specified. + direct = false + + default: + p.buf.WriteString("%!") + p.buf.WriteRune(verb) + p.buf.WriteByte('(') + switch { + case err != nil: + p.buf.WriteString(reflect.TypeOf(f).String()) + default: + p.buf.WriteString("") + } + p.buf.WriteByte(')') + io.Copy(s, &p.buf) + return + } + +loop: + for { + switch v := err.(type) { + case Formatter: + err = v.FormatError((*printer)(p)) + case fmt.Formatter: + v.Format(p, 'v') + break loop + default: + io.WriteString(&p.buf, v.Error()) + break loop + } + if err == nil { + break + } + if p.needColon || !p.printDetail { + p.buf.WriteByte(':') + p.needColon = false + } + p.buf.WriteString(sep) + p.inDetail = false + p.needNewline = false + } + +exit: + width, okW := s.Width() + prec, okP := s.Precision() + + if !direct || (okW && width > 0) || okP { + // Construct format string from State s. + format := []byte{'%'} + if s.Flag('-') { + format = append(format, '-') + } + if s.Flag('+') { + format = append(format, '+') + } + if s.Flag(' ') { + format = append(format, ' ') + } + if okW { + format = strconv.AppendInt(format, int64(width), 10) + } + if okP { + format = append(format, '.') + format = strconv.AppendInt(format, int64(prec), 10) + } + format = append(format, string(verb)...) + fmt.Fprintf(s, string(format), p.buf.String()) + } else { + io.Copy(s, &p.buf) + } +} + +var detailSep = []byte("\n ") + +// state tracks error printing state. It implements fmt.State. +type state struct { + fmt.State + buf bytes.Buffer + + printDetail bool + inDetail bool + needColon bool + needNewline bool +} + +func (s *state) Write(b []byte) (n int, err error) { + if s.printDetail { + if len(b) == 0 { + return 0, nil + } + if s.inDetail && s.needColon { + s.needNewline = true + if b[0] == '\n' { + b = b[1:] + } + } + k := 0 + for i, c := range b { + if s.needNewline { + if s.inDetail && s.needColon { + s.buf.WriteByte(':') + s.needColon = false + } + s.buf.Write(detailSep) + s.needNewline = false + } + if c == '\n' { + s.buf.Write(b[k:i]) + k = i + 1 + s.needNewline = true + } + } + s.buf.Write(b[k:]) + if !s.inDetail { + s.needColon = true + } + } else if !s.inDetail { + s.buf.Write(b) + } + return len(b), nil +} + +// printer wraps a state to implement an xerrors.Printer. +type printer state + +func (s *printer) Print(args ...interface{}) { + if !s.inDetail || s.printDetail { + fmt.Fprint((*state)(s), args...) + } +} + +func (s *printer) Printf(format string, args ...interface{}) { + if !s.inDetail || s.printDetail { + fmt.Fprintf((*state)(s), format, args...) + } +} + +func (s *printer) Detail() bool { + s.inDetail = true + return s.printDetail +} diff --git a/vendor/golang.org/x/xerrors/codereview.cfg b/vendor/golang.org/x/xerrors/codereview.cfg new file mode 100644 index 0000000000..3f8b14b64e --- /dev/null +++ b/vendor/golang.org/x/xerrors/codereview.cfg @@ -0,0 +1 @@ +issuerepo: golang/go diff --git a/vendor/golang.org/x/xerrors/doc.go b/vendor/golang.org/x/xerrors/doc.go new file mode 100644 index 0000000000..eef99d9d54 --- /dev/null +++ b/vendor/golang.org/x/xerrors/doc.go @@ -0,0 +1,22 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package xerrors implements functions to manipulate errors. +// +// This package is based on the Go 2 proposal for error values: +// https://golang.org/design/29934-error-values +// +// These functions were incorporated into the standard library's errors package +// in Go 1.13: +// - Is +// - As +// - Unwrap +// +// Also, Errorf's %w verb was incorporated into fmt.Errorf. +// +// Use this package to get equivalent behavior in all supported Go versions. +// +// No other features of this package were included in Go 1.13, and at present +// there are no plans to include any of them. +package xerrors // import "golang.org/x/xerrors" diff --git a/vendor/golang.org/x/xerrors/errors.go b/vendor/golang.org/x/xerrors/errors.go new file mode 100644 index 0000000000..e88d3772d8 --- /dev/null +++ b/vendor/golang.org/x/xerrors/errors.go @@ -0,0 +1,33 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package xerrors + +import "fmt" + +// errorString is a trivial implementation of error. +type errorString struct { + s string + frame Frame +} + +// New returns an error that formats as the given text. +// +// The returned error contains a Frame set to the caller's location and +// implements Formatter to show this information when printed with details. +func New(text string) error { + return &errorString{text, Caller(1)} +} + +func (e *errorString) Error() string { + return e.s +} + +func (e *errorString) Format(s fmt.State, v rune) { FormatError(e, s, v) } + +func (e *errorString) FormatError(p Printer) (next error) { + p.Print(e.s) + e.frame.Format(p) + return nil +} diff --git a/vendor/golang.org/x/xerrors/fmt.go b/vendor/golang.org/x/xerrors/fmt.go new file mode 100644 index 0000000000..829862ddf6 --- /dev/null +++ b/vendor/golang.org/x/xerrors/fmt.go @@ -0,0 +1,187 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package xerrors + +import ( + "fmt" + "strings" + "unicode" + "unicode/utf8" + + "golang.org/x/xerrors/internal" +) + +const percentBangString = "%!" + +// Errorf formats according to a format specifier and returns the string as a +// value that satisfies error. +// +// The returned error includes the file and line number of the caller when +// formatted with additional detail enabled. If the last argument is an error +// the returned error's Format method will return it if the format string ends +// with ": %s", ": %v", or ": %w". If the last argument is an error and the +// format string ends with ": %w", the returned error implements an Unwrap +// method returning it. +// +// If the format specifier includes a %w verb with an error operand in a +// position other than at the end, the returned error will still implement an +// Unwrap method returning the operand, but the error's Format method will not +// return the wrapped error. +// +// It is invalid to include more than one %w verb or to supply it with an +// operand that does not implement the error interface. The %w verb is otherwise +// a synonym for %v. +func Errorf(format string, a ...interface{}) error { + format = formatPlusW(format) + // Support a ": %[wsv]" suffix, which works well with xerrors.Formatter. + wrap := strings.HasSuffix(format, ": %w") + idx, format2, ok := parsePercentW(format) + percentWElsewhere := !wrap && idx >= 0 + if !percentWElsewhere && (wrap || strings.HasSuffix(format, ": %s") || strings.HasSuffix(format, ": %v")) { + err := errorAt(a, len(a)-1) + if err == nil { + return &noWrapError{fmt.Sprintf(format, a...), nil, Caller(1)} + } + // TODO: this is not entirely correct. The error value could be + // printed elsewhere in format if it mixes numbered with unnumbered + // substitutions. With relatively small changes to doPrintf we can + // have it optionally ignore extra arguments and pass the argument + // list in its entirety. + msg := fmt.Sprintf(format[:len(format)-len(": %s")], a[:len(a)-1]...) + frame := Frame{} + if internal.EnableTrace { + frame = Caller(1) + } + if wrap { + return &wrapError{msg, err, frame} + } + return &noWrapError{msg, err, frame} + } + // Support %w anywhere. + // TODO: don't repeat the wrapped error's message when %w occurs in the middle. + msg := fmt.Sprintf(format2, a...) + if idx < 0 { + return &noWrapError{msg, nil, Caller(1)} + } + err := errorAt(a, idx) + if !ok || err == nil { + // Too many %ws or argument of %w is not an error. Approximate the Go + // 1.13 fmt.Errorf message. + return &noWrapError{fmt.Sprintf("%sw(%s)", percentBangString, msg), nil, Caller(1)} + } + frame := Frame{} + if internal.EnableTrace { + frame = Caller(1) + } + return &wrapError{msg, err, frame} +} + +func errorAt(args []interface{}, i int) error { + if i < 0 || i >= len(args) { + return nil + } + err, ok := args[i].(error) + if !ok { + return nil + } + return err +} + +// formatPlusW is used to avoid the vet check that will barf at %w. +func formatPlusW(s string) string { + return s +} + +// Return the index of the only %w in format, or -1 if none. +// Also return a rewritten format string with %w replaced by %v, and +// false if there is more than one %w. +// TODO: handle "%[N]w". +func parsePercentW(format string) (idx int, newFormat string, ok bool) { + // Loosely copied from golang.org/x/tools/go/analysis/passes/printf/printf.go. + idx = -1 + ok = true + n := 0 + sz := 0 + var isW bool + for i := 0; i < len(format); i += sz { + if format[i] != '%' { + sz = 1 + continue + } + // "%%" is not a format directive. + if i+1 < len(format) && format[i+1] == '%' { + sz = 2 + continue + } + sz, isW = parsePrintfVerb(format[i:]) + if isW { + if idx >= 0 { + ok = false + } else { + idx = n + } + // "Replace" the last character, the 'w', with a 'v'. + p := i + sz - 1 + format = format[:p] + "v" + format[p+1:] + } + n++ + } + return idx, format, ok +} + +// Parse the printf verb starting with a % at s[0]. +// Return how many bytes it occupies and whether the verb is 'w'. +func parsePrintfVerb(s string) (int, bool) { + // Assume only that the directive is a sequence of non-letters followed by a single letter. + sz := 0 + var r rune + for i := 1; i < len(s); i += sz { + r, sz = utf8.DecodeRuneInString(s[i:]) + if unicode.IsLetter(r) { + return i + sz, r == 'w' + } + } + return len(s), false +} + +type noWrapError struct { + msg string + err error + frame Frame +} + +func (e *noWrapError) Error() string { + return fmt.Sprint(e) +} + +func (e *noWrapError) Format(s fmt.State, v rune) { FormatError(e, s, v) } + +func (e *noWrapError) FormatError(p Printer) (next error) { + p.Print(e.msg) + e.frame.Format(p) + return e.err +} + +type wrapError struct { + msg string + err error + frame Frame +} + +func (e *wrapError) Error() string { + return fmt.Sprint(e) +} + +func (e *wrapError) Format(s fmt.State, v rune) { FormatError(e, s, v) } + +func (e *wrapError) FormatError(p Printer) (next error) { + p.Print(e.msg) + e.frame.Format(p) + return e.err +} + +func (e *wrapError) Unwrap() error { + return e.err +} diff --git a/vendor/golang.org/x/xerrors/format.go b/vendor/golang.org/x/xerrors/format.go new file mode 100644 index 0000000000..1bc9c26b97 --- /dev/null +++ b/vendor/golang.org/x/xerrors/format.go @@ -0,0 +1,34 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package xerrors + +// A Formatter formats error messages. +type Formatter interface { + error + + // FormatError prints the receiver's first error and returns the next error in + // the error chain, if any. + FormatError(p Printer) (next error) +} + +// A Printer formats error messages. +// +// The most common implementation of Printer is the one provided by package fmt +// during Printf (as of Go 1.13). Localization packages such as golang.org/x/text/message +// typically provide their own implementations. +type Printer interface { + // Print appends args to the message output. + Print(args ...interface{}) + + // Printf writes a formatted string. + Printf(format string, args ...interface{}) + + // Detail reports whether error detail is requested. + // After the first call to Detail, all text written to the Printer + // is formatted as additional detail, or ignored when + // detail has not been requested. + // If Detail returns false, the caller can avoid printing the detail at all. + Detail() bool +} diff --git a/vendor/golang.org/x/xerrors/frame.go b/vendor/golang.org/x/xerrors/frame.go new file mode 100644 index 0000000000..0de628ec50 --- /dev/null +++ b/vendor/golang.org/x/xerrors/frame.go @@ -0,0 +1,56 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package xerrors + +import ( + "runtime" +) + +// A Frame contains part of a call stack. +type Frame struct { + // Make room for three PCs: the one we were asked for, what it called, + // and possibly a PC for skipPleaseUseCallersFrames. See: + // https://go.googlesource.com/go/+/032678e0fb/src/runtime/extern.go#169 + frames [3]uintptr +} + +// Caller returns a Frame that describes a frame on the caller's stack. +// The argument skip is the number of frames to skip over. +// Caller(0) returns the frame for the caller of Caller. +func Caller(skip int) Frame { + var s Frame + runtime.Callers(skip+1, s.frames[:]) + return s +} + +// location reports the file, line, and function of a frame. +// +// The returned function may be "" even if file and line are not. +func (f Frame) location() (function, file string, line int) { + frames := runtime.CallersFrames(f.frames[:]) + if _, ok := frames.Next(); !ok { + return "", "", 0 + } + fr, ok := frames.Next() + if !ok { + return "", "", 0 + } + return fr.Function, fr.File, fr.Line +} + +// Format prints the stack as error detail. +// It should be called from an error's Format implementation +// after printing any other error detail. +func (f Frame) Format(p Printer) { + if p.Detail() { + function, file, line := f.location() + if function != "" { + p.Printf("%s\n ", function) + } + if file != "" { + p.Printf("%s:%d\n", file, line) + } + } +} diff --git a/vendor/golang.org/x/xerrors/go.mod b/vendor/golang.org/x/xerrors/go.mod new file mode 100644 index 0000000000..870d4f612d --- /dev/null +++ b/vendor/golang.org/x/xerrors/go.mod @@ -0,0 +1,3 @@ +module golang.org/x/xerrors + +go 1.11 diff --git a/vendor/golang.org/x/xerrors/internal/internal.go b/vendor/golang.org/x/xerrors/internal/internal.go new file mode 100644 index 0000000000..89f4eca5df --- /dev/null +++ b/vendor/golang.org/x/xerrors/internal/internal.go @@ -0,0 +1,8 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package internal + +// EnableTrace indicates whether stack information should be recorded in errors. +var EnableTrace = true diff --git a/vendor/golang.org/x/xerrors/wrap.go b/vendor/golang.org/x/xerrors/wrap.go new file mode 100644 index 0000000000..9a3b510374 --- /dev/null +++ b/vendor/golang.org/x/xerrors/wrap.go @@ -0,0 +1,106 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package xerrors + +import ( + "reflect" +) + +// A Wrapper provides context around another error. +type Wrapper interface { + // Unwrap returns the next error in the error chain. + // If there is no next error, Unwrap returns nil. + Unwrap() error +} + +// Opaque returns an error with the same error formatting as err +// but that does not match err and cannot be unwrapped. +func Opaque(err error) error { + return noWrapper{err} +} + +type noWrapper struct { + error +} + +func (e noWrapper) FormatError(p Printer) (next error) { + if f, ok := e.error.(Formatter); ok { + return f.FormatError(p) + } + p.Print(e.error) + return nil +} + +// Unwrap returns the result of calling the Unwrap method on err, if err implements +// Unwrap. Otherwise, Unwrap returns nil. +func Unwrap(err error) error { + u, ok := err.(Wrapper) + if !ok { + return nil + } + return u.Unwrap() +} + +// Is reports whether any error in err's chain matches target. +// +// An error is considered to match a target if it is equal to that target or if +// it implements a method Is(error) bool such that Is(target) returns true. +func Is(err, target error) bool { + if target == nil { + return err == target + } + + isComparable := reflect.TypeOf(target).Comparable() + for { + if isComparable && err == target { + return true + } + if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) { + return true + } + // TODO: consider supporing target.Is(err). This would allow + // user-definable predicates, but also may allow for coping with sloppy + // APIs, thereby making it easier to get away with them. + if err = Unwrap(err); err == nil { + return false + } + } +} + +// As finds the first error in err's chain that matches the type to which target +// points, and if so, sets the target to its value and returns true. An error +// matches a type if it is assignable to the target type, or if it has a method +// As(interface{}) bool such that As(target) returns true. As will panic if target +// is not a non-nil pointer to a type which implements error or is of interface type. +// +// The As method should set the target to its value and return true if err +// matches the type to which target points. +func As(err error, target interface{}) bool { + if target == nil { + panic("errors: target cannot be nil") + } + val := reflect.ValueOf(target) + typ := val.Type() + if typ.Kind() != reflect.Ptr || val.IsNil() { + panic("errors: target must be a non-nil pointer") + } + if e := typ.Elem(); e.Kind() != reflect.Interface && !e.Implements(errorType) { + panic("errors: *target must be interface or implement error") + } + targetType := typ.Elem() + for err != nil { + if reflect.TypeOf(err).AssignableTo(targetType) { + val.Elem().Set(reflect.ValueOf(err)) + return true + } + if x, ok := err.(interface{ As(interface{}) bool }); ok && x.As(target) { + return true + } + err = Unwrap(err) + } + return false +} + +var errorType = reflect.TypeOf((*error)(nil)).Elem() diff --git a/vendor/modules.txt b/vendor/modules.txt index 7b3db62a53..88642f858c 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -142,6 +142,8 @@ github.com/aws/aws-sdk-go/service/iam github.com/aws/aws-sdk-go/service/s3 github.com/aws/aws-sdk-go/service/sts github.com/aws/aws-sdk-go/service/sts/stsiface +# github.com/benbjohnson/clock v1.0.0 +github.com/benbjohnson/clock # github.com/beorn7/perks v1.0.0 github.com/beorn7/perks/quantile # github.com/bitly/go-simplejson v0.5.0 @@ -320,6 +322,8 @@ github.com/googollee/go-engine.io/transport github.com/googollee/go-engine.io/websocket # github.com/googollee/go-socket.io v0.0.0-20181214084611-0ad7206c347a github.com/googollee/go-socket.io +# github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 +github.com/gopherjs/gopherjs/js # github.com/gorilla/context v1.1.1 github.com/gorilla/context # github.com/gorilla/mux v1.6.2 @@ -352,6 +356,8 @@ github.com/jinzhu/inflection github.com/jmespath/go-jmespath # github.com/json-iterator/go v1.1.7 github.com/json-iterator/go +# github.com/jtolds/gls v4.20.0+incompatible +github.com/jtolds/gls # github.com/koding/websocketproxy v0.0.0-20181220232114-7ed82d81a28c github.com/koding/websocketproxy # github.com/konsorten/go-windows-terminal-sequences v1.0.1 @@ -481,6 +487,14 @@ github.com/sirupsen/logrus github.com/skip2/go-qrcode github.com/skip2/go-qrcode/bitset github.com/skip2/go-qrcode/reedsolomon +# github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d +github.com/smartystreets/assertions +github.com/smartystreets/assertions/internal/go-render/render +github.com/smartystreets/assertions/internal/oglematchers +# github.com/smartystreets/goconvey v1.6.4 +github.com/smartystreets/goconvey/convey +github.com/smartystreets/goconvey/convey/gotest +github.com/smartystreets/goconvey/convey/reporting # github.com/spaolacci/murmur3 v1.1.0 github.com/spaolacci/murmur3 # github.com/spf13/pflag v1.0.3 @@ -640,6 +654,9 @@ golang.org/x/text/unicode/bidi golang.org/x/text/unicode/norm # golang.org/x/time v0.0.0-20181108054448-85acf8d2951c golang.org/x/time/rate +# golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 +golang.org/x/xerrors +golang.org/x/xerrors/internal # golang.zx2c4.com/wireguard/wgctrl v0.0.0-20191008142428-8d021180e987 golang.zx2c4.com/wireguard/wgctrl/wgtypes # google.golang.org/api v0.13.0 @@ -872,7 +889,7 @@ yunion.io/x/jsonutils # yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d yunion.io/x/log yunion.io/x/log/hooks -# yunion.io/x/pkg v0.0.0-20200221023330-f129027c3b04 +# yunion.io/x/pkg v0.0.0-20200302034534-fdf44d54b070 yunion.io/x/pkg/errors yunion.io/x/pkg/gotypes yunion.io/x/pkg/prettytable diff --git a/vendor/yunion.io/x/pkg/util/reflectutils/jsonfield.go b/vendor/yunion.io/x/pkg/util/reflectutils/jsonfield.go index a70f7c24a6..b19f02feb3 100644 --- a/vendor/yunion.io/x/pkg/util/reflectutils/jsonfield.go +++ b/vendor/yunion.io/x/pkg/util/reflectutils/jsonfield.go @@ -249,6 +249,30 @@ func (set SStructFieldValueSet) GetStructFieldIndex(name string) int { return -1 } +func (set SStructFieldValueSet) GetStructFieldIndexes(name string) []int { + ret := make([]int, 0) + for i := 0; i < len(set); i += 1 { + jsonInfo := set[i].Info + if jsonInfo.MarshalName() == name { + ret = append(ret, i) + continue + } + if utils.CamelSplit(jsonInfo.FieldName, "_") == utils.CamelSplit(name, "_") { + ret = append(ret, i) + continue + } + if jsonInfo.FieldName == name { + ret = append(ret, i) + continue + } + if jsonInfo.FieldName == utils.Capitalize(name) { + ret = append(ret, i) + continue + } + } + return ret +} + func (set SStructFieldValueSet) GetValue(name string) (reflect.Value, bool) { idx := set.GetStructFieldIndex(name) if idx < 0 { diff --git a/vendor/yunion.io/x/pkg/util/signalutils/dumpstack_others.go b/vendor/yunion.io/x/pkg/util/signalutils/dumpstack_others.go index b0449bc6ca..b83c30853d 100644 --- a/vendor/yunion.io/x/pkg/util/signalutils/dumpstack_others.go +++ b/vendor/yunion.io/x/pkg/util/signalutils/dumpstack_others.go @@ -1,4 +1,5 @@ // +build !windows + package signalutils import (