diff --git a/docs/sources/http_api/annotations.md b/docs/sources/http_api/annotations.md index 2f148e9aded..7aab127cb0c 100644 --- a/docs/sources/http_api/annotations.md +++ b/docs/sources/http_api/annotations.md @@ -120,6 +120,37 @@ Content-Type: application/json {"message":"Annotation added"} ``` +## Create Annotation in Graphite format + +Creates an annotation by using Graphite-compatible event format. The `when` and `data` fields are optional. If `when` is not specified then the current time will be used as annotation's timestamp. The `tags` field can also be in prior to Graphite `0.10.0` +format (string with multiple tags being separated by a space). + +`POST /api/annotations/graphite` + +**Example Request**: + +```json +POST /api/annotations/graphite HTTP/1.1 +Accept: application/json +Content-Type: application/json + +{ + "what": "Event - deploy", + "tags": ["deploy", "production"], + "when": 1467844481, + "data": "deploy of master branch happened at Wed Jul 6 22:34:41 UTC 2016" +} +``` + +**Example Response**: + +```json +HTTP/1.1 200 +Content-Type: application/json + +{"message":"Graphite annotation added"} +``` + ## Update Annotation `PUT /api/annotations/:id` diff --git a/pkg/api/annotations.go b/pkg/api/annotations.go index be069f2b07e..e6454e9cf86 100644 --- a/pkg/api/annotations.go +++ b/pkg/api/annotations.go @@ -1,7 +1,6 @@ package api import ( - "fmt" "strings" "time" @@ -41,9 +40,22 @@ func GetAnnotations(c *middleware.Context) Response { return Json(200, items) } +type CreateAnnotationError struct { + message string +} + +func (e *CreateAnnotationError) Error() string { + return e.message +} + func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response { repo := annotations.GetRepository() + if cmd.Text == "" { + err := &CreateAnnotationError{"text field should not be empty"} + return ApiError(500, "Failed to save annotation", err) + } + item := annotations.Item{ OrgId: c.OrgId, UserId: c.UserId, @@ -55,6 +67,10 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response Tags: cmd.Tags, } + if item.Epoch == 0 { + item.Epoch = time.Now().Unix() + } + if err := repo.Save(&item); err != nil { return ApiError(500, "Failed to save annotation", err) } @@ -82,21 +98,22 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response return ApiSuccess("Annotation added") } -type GraphiteAnnotationError struct { - message string -} - -func (e *GraphiteAnnotationError) Error() string { - return e.message -} - func formatGraphiteAnnotation(what string, data string) string { - return fmt.Sprintf("%s\n%s", what, data) + text := what + if data != "" { + text = text + "\n" + data + } + return text } func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotationsCmd) Response { repo := annotations.GetRepository() + if cmd.What == "" { + err := &CreateAnnotationError{"what field should not be empty"} + return ApiError(500, "Failed to save Graphite annotation", err) + } + if cmd.When == 0 { cmd.When = time.Now().Unix() } @@ -106,18 +123,22 @@ func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotati var tagsArray []string switch tags := cmd.Tags.(type) { case string: - tagsArray = strings.Split(tags, " ") + if tags != "" { + tagsArray = strings.Split(tags, " ") + } else { + tagsArray = []string{} + } case []interface{}: for _, t := range tags { if tagStr, ok := t.(string); ok { tagsArray = append(tagsArray, tagStr) } else { - err := &GraphiteAnnotationError{"tag should be a string"} + err := &CreateAnnotationError{"tag should be a string"} return ApiError(500, "Failed to save Graphite annotation", err) } } default: - err := &GraphiteAnnotationError{"unsupported tags format"} + err := &CreateAnnotationError{"unsupported tags format"} return ApiError(500, "Failed to save Graphite annotation", err) } @@ -133,7 +154,7 @@ func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotati return ApiError(500, "Failed to save Graphite annotation", err) } - return ApiSuccess("Graphite Annotation added") + return ApiSuccess("Graphite annotation added") } func UpdateAnnotation(c *middleware.Context, cmd dtos.UpdateAnnotationsCmd) Response {