Skip to content

Commit

Permalink
Merge branch 'master' into feature/nodata_protector_mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
beevee committed Jul 24, 2019
2 parents 4d0e256 + 115cf0b commit e7e0112
Show file tree
Hide file tree
Showing 55 changed files with 1,977 additions and 146 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.git
.idea

vendor
!vendor/vendor.json
build
local

**/*_test.go
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
- redis-server
language: go
go:
- 1.12.5
- 1.12.6
git:
depth: 1000
addons:
Expand Down
11 changes: 7 additions & 4 deletions Dockerfile.api
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
FROM golang:1.12.5 as builder
FROM golang:1.12.6 as builder

WORKDIR /go/src/github.com/moira-alert/moira
COPY . /go/src/github.com/moira-alert/moira/
RUN go get github.com/kardianos/govendor

COPY ./vendor/vendor.json /go/src/github.com/moira-alert/moira/vendor/vendor.json
WORKDIR /go/src/github.com/moira-alert/moira
RUN govendor sync

COPY . /go/src/github.com/moira-alert/moira/

ARG GO_VERSION="GoVersion"
ARG GIT_COMMIT="git_Commit"
ARG MoiraVersion="MoiraVersion"
Expand All @@ -16,7 +20,6 @@ FROM alpine
RUN apk add --no-cache ca-certificates && update-ca-certificates

COPY pkg/api/api.yml /etc/moira/api.yml
COPY pkg/api/web.json /etc/moira/web.json

COPY --from=builder /go/src/github.com/moira-alert/moira/build/api /usr/bin/api
COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /usr/local/go/lib/time/
Expand Down
10 changes: 7 additions & 3 deletions Dockerfile.checker
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
FROM golang:1.12.5 as builder
FROM golang:1.12.6 as builder

WORKDIR /go/src/github.com/moira-alert/moira
COPY . /go/src/github.com/moira-alert/moira/
RUN go get github.com/kardianos/govendor

COPY ./vendor/vendor.json /go/src/github.com/moira-alert/moira/vendor/vendor.json
WORKDIR /go/src/github.com/moira-alert/moira
RUN govendor sync

COPY . /go/src/github.com/moira-alert/moira/

ARG GO_VERSION="GoVersion"
ARG GIT_COMMIT="git_Commit"
ARG MoiraVersion="MoiraVersion"
Expand Down
10 changes: 7 additions & 3 deletions Dockerfile.filter
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
FROM golang:1.12.5 as builder
FROM golang:1.12.6 as builder

WORKDIR /go/src/github.com/moira-alert/moira
COPY . /go/src/github.com/moira-alert/moira/
RUN go get github.com/kardianos/govendor

COPY ./vendor/vendor.json /go/src/github.com/moira-alert/moira/vendor/vendor.json
WORKDIR /go/src/github.com/moira-alert/moira
RUN govendor sync

COPY . /go/src/github.com/moira-alert/moira/

ARG GO_VERSION="GoVersion"
ARG GIT_COMMIT="git_Commit"
ARG MoiraVersion="MoiraVersion"
Expand Down
10 changes: 7 additions & 3 deletions Dockerfile.notifier
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
FROM golang:1.12.5 as builder
FROM golang:1.12.6 as builder

WORKDIR /go/src/github.com/moira-alert/moira
COPY . /go/src/github.com/moira-alert/moira/
RUN go get github.com/kardianos/govendor

COPY ./vendor/vendor.json /go/src/github.com/moira-alert/moira/vendor/vendor.json
WORKDIR /go/src/github.com/moira-alert/moira
RUN govendor sync

COPY . /go/src/github.com/moira-alert/moira/

ARG GO_VERSION="GoVersion"
ARG GIT_COMMIT="git_Commit"
ARG MoiraVersion="MoiraVersion"
Expand Down
55 changes: 39 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
# Moira 2.0 [![Documentation Status](https://readthedocs.org/projects/moira/badge/?version=latest)](http://moira.readthedocs.io/en/latest/?badge=latest) [![Telegram](https://img.shields.io/badge/telegram-join%20chat-3796cd.svg)](https://t.me/moira_alert) [![Go Report Card](https://goreportcard.com/badge/github.com/moira-alert/moira)](https://goreportcard.com/report/github.com/moira-alert/moira)
# Moira 2.0 [![Build Status](https://travis-ci.org/moira-alert/moira.svg?branch=master)](https://travis-ci.org/moira-alert/moira) [![Coverage Status](https://coveralls.io/repos/github/moira-alert/moira/badge.svg?branch=master)](https://coveralls.io/github/moira-alert/moira?branch=master) [![Documentation Status](https://readthedocs.org/projects/moira/badge/?version=latest)](http://moira.readthedocs.io/en/latest/?badge=latest) [![Telegram](https://img.shields.io/badge/telegram-join%20chat-3796cd.svg)](https://t.me/moira_alert) [![Go Report Card](https://goreportcard.com/badge/github.com/moira-alert/moira)](https://goreportcard.com/report/github.com/moira-alert/moira)

Moira is a real-time alerting tool, based on [Graphite](https://graphite.readthedocs.io) data.

## Build status
| branch | status |
| ------ | ------ |
| master | [![Build Status](https://travis-ci.org/moira-alert/moira.svg?branch=master)](https://travis-ci.org/moira-alert/moira) [![Coverage Status](https://coveralls.io/repos/github/moira-alert/moira/badge.svg?branch=master)](https://coveralls.io/github/moira-alert/moira?branch=master) |
| develop | [![Build Status](https://travis-ci.org/moira-alert/moira.svg?branch=develop)](https://travis-ci.org/moira-alert/moira) [![Coverage Status](https://coveralls.io/repos/github/moira-alert/moira/badge.svg?branch=develop)](https://coveralls.io/github/moira-alert/moira?branch=develop) |


## Installation

Docker Compose is the easiest way to try:

```
```bash
git clone https://github.com/moira-alert/docker-compose.git
cd docker-compose
docker-compose pull
Expand All @@ -22,29 +15,61 @@ docker-compose up

Feed data in Graphite format to `localhost:2003`:

```
```bash
echo "local.random.diceroll 4 `date +%s`" | nc localhost 2003
```

Configure triggers at `localhost:8080` using your browser.

Other installation methods are available, see [documentation](https://moira.readthedocs.io/en/latest/installation/index.html).

## Development

To build and run tests, first get all dependencies:

```bash
go get github.com/kardianos/govendor
govendor sync
```

Then you need local redis listening on port 6379.
Easiest way to get redis is via docker:

```bash
docker run -p 6379:6379 -d redis:alpine
```

To run test use ``go test ./...`` or run [GoConvey](http://goconvey.co/):

```bash
go get github.com/smartystreets/goconvey
goconvey
```

For full local deployment of all services, including web, graphite and metrics relay (may be slow on first launch) use:

```bash
docker-compose up
```

Before push your changes don't forget about linter:

```bash
make lint
```

## Getting Started

See our [user guide](https://moira.readthedocs.io/en/latest/user_guide/index.html) that is based on a number of real-life scenarios, from simple and universal to complicated and specific.


## Why 2.0?
## Why 2.0

Moira 2.0 is different from the first version in two important ways:

1. We got rid of Python, because it was slow. Checker and API services are now written in Go, based on [carbonapi](https://github.com/go-graphite/carbonapi) implementation of Graphite functions.
2. We got rid of Angular, because our main stack is React now. We just don't know how to do Angular anymore. We also revamped the UI.


## What is in the other repositories?
## What is in the other repositories

Code in this repository is the backend part of Moira monitoring application.

Expand All @@ -53,12 +78,10 @@ Code in this repository is the backend part of Moira monitoring application.
* [moira-trigger-role](https://github.com/moira-alert/moira-trigger-role) is the Ansible role you can use to manage triggers.
* [python-moira-client](https://github.com/moira-alert/python-moira-client) is the Python API client.


## Contact us

If you have any questions, you can ask us on [Telegram](https://t.me/moira_alert).


## Thanks

![SKB Kontur](https://kontur.ru/theme/ver-1652188951/common/images/logo_english.png)
Expand Down
16 changes: 16 additions & 0 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,19 @@ type Config struct {
EnableCORS bool
Listen string
}

// WebConfig is container for web ui configuration parameters
type WebConfig struct {
SupportEmail string `json:"supportEmail,omitempty"`
RemoteAllowed bool `json:"remoteAllowed"`
Contacts []WebContact `json:"contacts"`
}

// WebContact is container for web ui contact validation
type WebContact struct {
ContactType string `json:"type"`
ContactLabel string `json:"label"`
ValidationRegex string `json:"validation,omitempty"`
Placeholder string `json:"placeholder,omitempty"`
Help string `json:"help,omitempty"`
}
12 changes: 12 additions & 0 deletions api/controller/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ func RemoveTag(database moira.Database, tagName string) (*dto.MessageResponse, *
if len(triggerIDs) > 0 {
return nil, api.ErrorInvalidRequest(fmt.Errorf("this tag is assigned to %v triggers. Remove tag from triggers first", len(triggerIDs)))
}

subscriptions, err := database.GetTagsSubscriptions([]string{tagName})
if err != nil {
return nil, api.ErrorInternalServer(err)
}

for _, s := range subscriptions {
if s != nil {
return nil, api.ErrorInvalidRequest(fmt.Errorf("this tag is assigned to %v subscriptions. Remove tag from subscriptions first", len(subscriptions)))
}
}

if err = database.RemoveTag(tagName); err != nil {
return nil, api.ErrorInternalServer(err)
}
Expand Down
26 changes: 23 additions & 3 deletions api/controller/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ func TestDeleteTag(t *testing.T) {
database := mock_moira_alert.NewMockDatabase(mockCtrl)
tag := "MyTag"

Convey("Test no trigger ids by tag", t, func() {
Convey("Test no trigger ids and subscriptions by tag", t, func() {
database.EXPECT().GetTagTriggerIDs(tag).Return(nil, nil)
database.EXPECT().GetTagsSubscriptions([]string{tag}).Return([]*moira.SubscriptionData{}, nil)
database.EXPECT().RemoveTag(tag).Return(nil)
resp, err := RemoveTag(database, tag)
So(err, ShouldBeNil)
So(resp, ShouldResemble, &dto.MessageResponse{Message: "tag deleted"})
})

Convey("Test has trigger ids by tag", t, func() {
Convey("Test has trigger ids and subscriptions by tag", t, func() {
database.EXPECT().GetTagTriggerIDs(tag).Return([]string{"123"}, nil)
resp, err := RemoveTag(database, tag)
So(err, ShouldResemble, api.ErrorInvalidRequest(fmt.Errorf("this tag is assigned to %v triggers. Remove tag from triggers first", 1)))
Expand All @@ -65,10 +66,29 @@ func TestDeleteTag(t *testing.T) {
So(resp, ShouldBeNil)
})

Convey("Error delete tag", t, func() {
Convey("verification of error handling when receiving subscriptions", t, func() {
expected := fmt.Errorf("can not read subscriptions")
database.EXPECT().GetTagTriggerIDs(tag).Return(nil, nil)
database.EXPECT().GetTagsSubscriptions([]string{tag}).Return(nil, expected)
resp, err := RemoveTag(database, tag)
So(err, ShouldResemble, api.ErrorInternalServer(expected))
So(resp, ShouldBeNil)
})

Convey("check create error if subscription exists", t, func() {
data := []*moira.SubscriptionData{{ID: "TestSubscription"}}
database.EXPECT().GetTagTriggerIDs(tag).Return(nil, nil)
database.EXPECT().GetTagsSubscriptions([]string{tag}).Return(data, nil)
resp, err := RemoveTag(database, tag)
So(err, ShouldResemble, api.ErrorInvalidRequest(fmt.Errorf("this tag is assigned to 1 subscriptions. Remove tag from subscriptions first")))
So(resp, ShouldBeNil)
})

Convey("verification of error handling during tag removal", t, func() {
expected := fmt.Errorf("can not delete tag")
database.EXPECT().GetTagTriggerIDs(tag).Return(nil, nil)
database.EXPECT().RemoveTag(tag).Return(expected)
database.EXPECT().GetTagsSubscriptions([]string{tag}).Return(nil, nil)
resp, err := RemoveTag(database, tag)
So(err, ShouldResemble, api.ErrorInternalServer(expected))
So(resp, ShouldBeNil)
Expand Down
10 changes: 10 additions & 0 deletions api/handler/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package handler

import "net/http"

func getWebConfig(configContent []byte) http.HandlerFunc {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
writer.Write(configContent)
})
}
16 changes: 2 additions & 14 deletions api/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handler

import (
"fmt"
"net/http"

"github.com/go-chi/chi"
Expand All @@ -22,7 +21,7 @@ const contactKey moiramiddle.ContextKey = "contact"
const subscriptionKey moiramiddle.ContextKey = "subscription"

// NewHandler creates new api handler request uris based on github.com/go-chi/chi
func NewHandler(db moira.Database, log moira.Logger, index moira.Searcher, config *api.Config, metricSourceProvider *metricSource.SourceProvider, configFile []byte) http.Handler {
func NewHandler(db moira.Database, log moira.Logger, index moira.Searcher, config *api.Config, metricSourceProvider *metricSource.SourceProvider, webConfigContent []byte) http.Handler {
database = db
searchIndex = index
router := chi.NewRouter()
Expand All @@ -36,7 +35,7 @@ func NewHandler(db moira.Database, log moira.Logger, index moira.Searcher, confi

router.Route("/api", func(router chi.Router) {
router.Use(moiramiddle.DatabaseContext(database))
router.Get("/config", webConfig(configFile))
router.Get("/config", getWebConfig(webConfigContent))
router.Route("/user", user)
router.Route("/trigger", triggers(metricSourceProvider, searchIndex))
router.Route("/tag", tag)
Expand All @@ -53,17 +52,6 @@ func NewHandler(db moira.Database, log moira.Logger, index moira.Searcher, confi
return router
}

func webConfig(content []byte) http.HandlerFunc {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if content == nil {
render.Render(writer, request, api.ErrorInternalServer(fmt.Errorf("web config file was not loaded")))
return
}
writer.Header().Set("Content-Type", "application/json")
writer.Write(content)
})
}

func notFoundHandler(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("X-Content-Type-Options", "nosniff")
writer.Header().Set("Content-Type", "application/json")
Expand Down

0 comments on commit e7e0112

Please sign in to comment.