Skip to content

Commit

Permalink
Merge branch 'master' into feature/moira-prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrergeru committed Aug 28, 2023
2 parents 517e6f9 + 4e9751c commit 89abfc3
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 41 deletions.
9 changes: 9 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# Check if any of the specified files are staged for commit
if git diff --name-only --cached | grep -E 'api/'; then
echo "Format swaggo annotations (swag fmt)"
swag fmt
fi

make lint
4 changes: 2 additions & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ For full local deployment of all services, including web, graphite and metrics r
docker-compose up
```

Before pushing your changes don't forget about linter:
Before pushing, don't forget to write this command please, it will activate the pre-commit hook on the linter and auto formatting swagger documentation:

```bash
make lint
git config --local core.hooksPath .githooks/
```
27 changes: 27 additions & 0 deletions .github/workflows/swagger-delete.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Delete spec version from SwaggerHub
defaults:
run:
working-directory: .
on:
push:
branches:
- master
- release/[0-9]+.[0-9]+.[0-9]+

jobs:
removespec:
name: Delete api from SwaggerHub
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.16.0'
- run: npm i --location=global swaggerhub-cli
- run: |
VERSION=`echo ${GITHUB_REF_NAME}| sed 's#[^a-zA-Z0-9_\.\-]#_#g'`
SWAGGERHUB_API_KEY=${{secrets.SWAGGERHUB_TOKEN}} swaggerhub api:unpublish "Moira/moira-alert/${VERSION}" || true
SWAGGERHUB_API_KEY=${{secrets.SWAGGERHUB_TOKEN}} swaggerhub api:delete "Moira/moira-alert/${VERSION}" || true
# The `|| true` at the end of the calls is necessary to keep the job from crashing
# when deleting documentation that hasn't been created yet, but if you see something wrong happening,
# remove `|| true` from the command
60 changes: 60 additions & 0 deletions .github/workflows/swagger-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Publish spec version to SwaggerHub

on:
push:
branches:
- master
- release/[0-9]+.[0-9]+.[0-9]+

jobs:
validate-spec:
name: Validate spec file
runs-on: ubuntu-22.04
defaults:
run:
working-directory: .

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v4
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- run: make install-swag

- uses: actions/setup-node@v3
with:
node-version: '16.16.0'
- run: npm install --location=global @openapitools/openapi-generator-cli
- run: make spec
- run: make validate-spec

- name: Save build artifact
uses: actions/upload-artifact@v3
with:
name: specfile
path: docs/swagger.yaml

publishspec:
name: Upload generated OpenAPI description
runs-on: ubuntu-22.04
needs: validate-spec
defaults:
run:
working-directory: .

steps:
- uses: actions/checkout@v3

- name: Download spec file artifact
uses: actions/download-artifact@v3
with:
name: specfile
path: docs

- uses: actions/setup-node@v3
- run: npm i --location=global swaggerhub-cli
- run: |
VERSION=`echo ${GITHUB_REF_NAME}| sed 's#[^a-zA-Z0-9_\.\-]#_#g'`
SWAGGERHUB_API_KEY=${{secrets.SWAGGERHUB_TOKEN}} swaggerhub api:create "Moira/moira-alert/${VERSION}" -f ./docs/swagger.yaml --published=publish --visibility=public
28 changes: 28 additions & 0 deletions .github/workflows/swagger-validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Validate OpenAPI on PR

on:
- pull_request

jobs:
mergespec:
name: Validate spec file
runs-on: ubuntu-22.04
defaults:
run:
working-directory: .

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v4
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- run: make install-swag

- uses: actions/setup-node@v3
with:
node-version: '16.16.0'
- run: npm install --location=global @openapitools/openapi-generator-cli
- run: make spec
- run: make validate-spec
2 changes: 1 addition & 1 deletion api/dto/event_history_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dto
import "net/http"

type ContactEventItem struct {
TimeStamp int64 `json:"timestamp"`
TimeStamp int64 `json:"timestamp" format:"int64"`
Metric string `json:"metric"`
State string `json:"state"`
OldState string `json:"old_state"`
Expand Down
6 changes: 3 additions & 3 deletions api/dto/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

type EventsList struct {
Page int64 `json:"page" example:"0"`
Size int64 `json:"size" example:"100"`
Total int64 `json:"total" example:"10"`
Page int64 `json:"page" example:"0" format:"int64"`
Size int64 `json:"size" example:"100" format:"int64"`
Total int64 `json:"total" example:"10" format:"int64"`
List []moira.NotificationEvent `json:"list"`
}

Expand Down
4 changes: 2 additions & 2 deletions api/dto/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type NotificationsList struct {
Total int64 `json:"total" example:"0"`
Total int64 `json:"total" example:"0" format:"int64"`
List []*moira.ScheduledNotification `json:"list"`
}

Expand All @@ -17,7 +17,7 @@ func (*NotificationsList) Render(w http.ResponseWriter, r *http.Request) error {
}

type NotificationDeleteResponse struct {
Result int64 `json:"result" example:"0"`
Result int64 `json:"result" example:"0" format:"int64"`
}

func (*NotificationDeleteResponse) Render(w http.ResponseWriter, r *http.Request) error {
Expand Down
14 changes: 7 additions & 7 deletions api/dto/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ var targetNameRegex = regexp.MustCompile("t(\\d+)")
var asteriskPattern = "*"

type TriggersList struct {
Page *int64 `json:"page,omitempty"`
Size *int64 `json:"size,omitempty"`
Total *int64 `json:"total,omitempty"`
Page *int64 `json:"page,omitempty" format:"int64"`
Size *int64 `json:"size,omitempty" format:"int64"`
Total *int64 `json:"total,omitempty" format:"int64"`
Pager *string `json:"pager,omitempty"`
List []moira.TriggerCheck `json:"list"`
}
Expand All @@ -36,7 +36,7 @@ func (*TriggersList) Render(http.ResponseWriter, *http.Request) error {

type Trigger struct {
TriggerModel
Throttling int64 `json:"throttling" example:"0"`
Throttling int64 `json:"throttling" example:"0" format:"int64"`
}

// TriggerModel is moira.Trigger api representation
Expand All @@ -60,7 +60,7 @@ type TriggerModel struct {
// When there are no metrics for trigger, Moira will switch metric to TTLState state after TTL seconds
TTLState *moira.TTLState `json:"ttl_state,omitempty" example:"NODATA"`
// When there are no metrics for trigger, Moira will switch metric to TTLState state after TTL seconds
TTL int64 `json:"ttl,omitempty" example:"600"`
TTL int64 `json:"ttl,omitempty" example:"600" format:"int64"`
// Determines when Moira should monitor trigger
Schedule *moira.ScheduleData `json:"sched,omitempty"`
// Used if you need more complex logic than provided by WARN/ERROR values
Expand Down Expand Up @@ -396,7 +396,7 @@ func (*MetricsMaintenance) Bind(*http.Request) error {
}

type TriggerMaintenance struct {
Trigger *int64 `json:"trigger" example:"1594225165"`
Trigger *int64 `json:"trigger" example:"1594225165" format:"int64"`
Metrics map[string]int64 `json:"metrics"`
}

Expand All @@ -405,7 +405,7 @@ func (*TriggerMaintenance) Bind(*http.Request) error {
}

type ThrottlingResponse struct {
Throttling int64 `json:"throttling" example:"0"`
Throttling int64 `json:"throttling" example:"0" format:"int64"`
}

func (*ThrottlingResponse) Render(http.ResponseWriter, *http.Request) error {
Expand Down
4 changes: 2 additions & 2 deletions api/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func NewHandler(db moira.Database, log moira.Logger, index moira.Searcher, confi
// @title Moira Alert
// @version master
// @description This is an API description for [Moira Alert API](https://moira.readthedocs.io/en/latest/overview.html)
// @description Check us out on [Github](https://github.com/moira-alert) or look up our [guide on getting started with Moira](https://moira.readthedocs.io)
// @contact.name Contact Moira Team
// @description Check us out on [Github](https://github.com/moira-alert) or look up our [guide](https://moira.readthedocs.io) on getting started with Moira
// @contact.name Moira Team
// @contact.email opensource@skbkontur.com
// @license.name MIT
// @BasePath /api
Expand Down
8 changes: 7 additions & 1 deletion api/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ func RequestLogger(logger moira.Logger) func(next http.Handler) http.Handler {
func getErrorResponseIfItHas(writer http.ResponseWriter) *api.ErrorResponse {
writerWithBody := writer.(*responseWriterWithBody)
var errResp = &api.ErrorResponse{}
json.NewDecoder(&writerWithBody.body).Decode(errResp) //nolint
if err := json.NewDecoder(&writerWithBody.body).Decode(errResp); err != nil {
return &api.ErrorResponse{
HTTPStatusCode: http.StatusInternalServerError,
Err: err,
}
}

return errResp
}

Expand Down
46 changes: 23 additions & 23 deletions datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (
// NotificationEvent represents trigger state changes event
type NotificationEvent struct {
IsTriggerEvent bool `json:"trigger_event,omitempty" example:"true"`
Timestamp int64 `json:"timestamp" example:"1590741878"`
Timestamp int64 `json:"timestamp" example:"1590741878" format:"int64"`
Metric string `json:"metric" example:"carbon.agents.*.metricsReceived"`
Value *float64 `json:"value,omitempty" example:"70"`
Values map[string]float64 `json:"values,omitempty"`
Expand All @@ -60,7 +60,7 @@ type NotificationEvent struct {
// NotificationEventHistoryItem is in use to store notifications history of channel
// (see database/redis/contact_notifications_history.go
type NotificationEventHistoryItem struct {
TimeStamp int64 `json:"timestamp"`
TimeStamp int64 `json:"timestamp" format:"int64"`
Metric string `json:"metric"`
State State `json:"state"`
OldState State `json:"old_state"`
Expand All @@ -71,7 +71,7 @@ type NotificationEventHistoryItem struct {
// EventInfo - a base for creating messages.
type EventInfo struct {
Maintenance *MaintenanceInfo `json:"maintenance,omitempty"`
Interval *int64 `json:"interval,omitempty" example:"0"`
Interval *int64 `json:"interval,omitempty" example:"0" format:"int64"`
}

// CreateMessage - creates a message based on EventInfo.
Expand Down Expand Up @@ -222,9 +222,9 @@ type PlottingData struct {
// ScheduleData represents subscription schedule
type ScheduleData struct {
Days []ScheduleDataDay `json:"days"`
TimezoneOffset int64 `json:"tzOffset" example:"-60"`
StartOffset int64 `json:"startOffset" example:"0"`
EndOffset int64 `json:"endOffset" example:"1439"`
TimezoneOffset int64 `json:"tzOffset" example:"-60" format:"int64"`
StartOffset int64 `json:"startOffset" example:"0" format:"int64"`
EndOffset int64 `json:"endOffset" example:"1439" format:"int64"`
}

// ScheduleDataDay represents week day of schedule
Expand All @@ -241,7 +241,7 @@ type ScheduledNotification struct {
Plotting PlottingData `json:"plotting"`
Throttled bool `json:"throttled" example:"false"`
SendFail int `json:"send_fail" example:"0"`
Timestamp int64 `json:"timestamp" example:"1594471927"`
Timestamp int64 `json:"timestamp" example:"1594471927" format:"int64"`
}

// MatchedMetric represents parsed and matched metric data
Expand All @@ -256,8 +256,8 @@ type MatchedMetric struct {

// MetricValue represents metric data
type MetricValue struct {
RetentionTimestamp int64 `json:"step,omitempty"`
Timestamp int64 `json:"ts"`
RetentionTimestamp int64 `json:"step,omitempty" format:"int64"`
Timestamp int64 `json:"ts" format:"int64"`
Value float64 `json:"value"`
}

Expand All @@ -281,16 +281,16 @@ type Trigger struct {
TriggerType string `json:"trigger_type" example:"rising"`
Tags []string `json:"tags" example:"server,disk"`
TTLState *TTLState `json:"ttl_state,omitempty" example:"NODATA"`
TTL int64 `json:"ttl,omitempty" example:"600"`
TTL int64 `json:"ttl,omitempty" example:"600" format:"int64"`
Schedule *ScheduleData `json:"sched,omitempty"`
Expression *string `json:"expression,omitempty" example:""`
PythonExpression *string `json:"python_expression,omitempty"`
Patterns []string `json:"patterns" example:""`
TriggerSource TriggerSource `json:"trigger_source,omitempty" example:"graphite_local"`
MuteNewMetrics bool `json:"mute_new_metrics" example:"false"`
AloneMetrics map[string]bool `json:"alone_metrics" example:"t1:true"`
CreatedAt *int64 `json:"created_at"`
UpdatedAt *int64 `json:"updated_at"`
CreatedAt *int64 `json:"created_at" format:"int64"`
UpdatedAt *int64 `json:"updated_at" format:"int64"`
CreatedBy string `json:"created_by"`
UpdatedBy string `json:"updated_by"`
}
Expand Down Expand Up @@ -335,7 +335,7 @@ func (triggerSource TriggerSource) FillInIfNotSet(isRempte bool) TriggerSource {
// TriggerCheck represents trigger data with last check data and check timestamp
type TriggerCheck struct {
Trigger
Throttling int64 `json:"throttling" example:"0"`
Throttling int64 `json:"throttling" example:"0" format:"int64"`
LastCheck CheckData `json:"last_check"`
Highlights map[string]string `json:"highlights"`
}
Expand All @@ -353,13 +353,13 @@ type CheckData struct {
// check and targets that fetched this metric
// {"t1": "metric.name.1", "t2": "metric.name.2"}
MetricsToTargetRelation map[string]string `json:"metrics_to_target_relation" example:"t1:metric.name.1,t2:metric.name.2"`
Score int64 `json:"score" example:"100"`
Score int64 `json:"score" example:"100" format:"int64"`
State State `json:"state" example:"OK"`
Maintenance int64 `json:"maintenance,omitempty" example:"0"`
Maintenance int64 `json:"maintenance,omitempty" example:"0" format:"int64"`
MaintenanceInfo MaintenanceInfo `json:"maintenance_info"`
Timestamp int64 `json:"timestamp,omitempty" example:"1590741916"`
EventTimestamp int64 `json:"event_timestamp,omitempty" example:"1590741878"`
LastSuccessfulCheckTimestamp int64 `json:"last_successful_check_timestamp" example:"1590741916"`
Timestamp int64 `json:"timestamp,omitempty" example:"1590741916" format:"int64"`
EventTimestamp int64 `json:"event_timestamp,omitempty" example:"1590741878" format:"int64"`
LastSuccessfulCheckTimestamp int64 `json:"last_successful_check_timestamp" example:"1590741916" format:"int64"`
Suppressed bool `json:"suppressed,omitempty" example:"true"`
SuppressedState State `json:"suppressed_state,omitempty"`
Message string `json:"msg,omitempty"`
Expand All @@ -377,14 +377,14 @@ func (checkData *CheckData) RemoveMetricsToTargetRelation() {

// MetricState represents metric state data for given timestamp
type MetricState struct {
EventTimestamp int64 `json:"event_timestamp" example:"1590741878"`
EventTimestamp int64 `json:"event_timestamp" example:"1590741878" format:"int64"`
State State `json:"state" example:"OK"`
Suppressed bool `json:"suppressed" example:"false"`
SuppressedState State `json:"suppressed_state,omitempty"`
Timestamp int64 `json:"timestamp" example:"1590741878"`
Timestamp int64 `json:"timestamp" example:"1590741878" format:"int64"`
Value *float64 `json:"value,omitempty" example:"70"`
Values map[string]float64 `json:"values,omitempty"`
Maintenance int64 `json:"maintenance,omitempty" example:"0"`
Maintenance int64 `json:"maintenance,omitempty" example:"0" format:"int64"`
MaintenanceInfo MaintenanceInfo `json:"maintenance_info"`
// AloneMetrics map[string]string `json:"alone_metrics"` // represents a relation between name of alone metrics and their targets
}
Expand All @@ -403,9 +403,9 @@ func (metricState *MetricState) GetMaintenance() (MaintenanceInfo, int64) {
// MaintenanceInfo represents user and time set/unset maintenance
type MaintenanceInfo struct {
StartUser *string `json:"setup_user"`
StartTime *int64 `json:"setup_time" example:"0"`
StartTime *int64 `json:"setup_time" example:"0" format:"int64"`
StopUser *string `json:"remove_user"`
StopTime *int64 `json:"remove_time" example:"0"`
StopTime *int64 `json:"remove_time" example:"0" format:"int64"`
}

// Set maintanace start and stop users and times
Expand Down

0 comments on commit 89abfc3

Please sign in to comment.