Skip to content

Add API for Alert Management Alert Metric Images

Sean Arnold requested to merge 340852-alert-metric-images into master

What does this MR do and why?

This MR adds a new API to upload, view, update and delete Metric Images for AlertManagement::Alert objects. These images are intended to provide context around the Alert, such as additional metrics from other reporting tools the user may be using, much like you can do with Incident metrics.

To enable this, there are 3 main parts to this MR:

  • A new AlertManagement::MetricImage model, and associated tables
  • A new API to enable CRUD for the AlertManagement::MetricImage model
  • A new Workhorse route to enable accelerated uploading of this images.

Database changes

Up
== 20220120033115 CreateAlertManagementAlertMetricImages: migrating ===========
-- create_table(:alert_management_alert_metric_images, {})
   -> 0.0278s
-- transaction_open?()
   -> 0.0000s
-- current_schema()
   -> 0.0003s
-- transaction_open?()
   -> 0.0000s
-- execute("ALTER TABLE alert_management_alert_metric_images\nADD CONSTRAINT check_4d811d9007\nCHECK ( char_length(url) <= 255 )\nNOT VALID;\n")
   -> 0.0013s
-- current_schema()
   -> 0.0002s
-- execute("SET statement_timeout TO 0")
   -> 0.0007s
-- execute("ALTER TABLE alert_management_alert_metric_images VALIDATE CONSTRAINT check_4d811d9007;")
   -> 0.0009s
-- execute("RESET statement_timeout")
   -> 0.0007s
-- transaction_open?()
   -> 0.0000s
-- current_schema()
   -> 0.0003s
-- transaction_open?()
   -> 0.0000s
-- execute("ALTER TABLE alert_management_alert_metric_images\nADD CONSTRAINT check_70fafae519\nCHECK ( char_length(file) <= 255 )\nNOT VALID;\n")
   -> 0.0012s
-- current_schema()
   -> 0.0003s
-- execute("ALTER TABLE alert_management_alert_metric_images VALIDATE CONSTRAINT check_70fafae519;")
   -> 0.0010s
-- transaction_open?()
   -> 0.0000s
-- current_schema()
   -> 0.0003s
-- transaction_open?()
   -> 0.0000s
-- execute("ALTER TABLE alert_management_alert_metric_images\nADD CONSTRAINT check_2587666252\nCHECK ( char_length(url_text) <= 128 )\nNOT VALID;\n")
   -> 0.0010s
-- current_schema()
   -> 0.0002s
-- execute("ALTER TABLE alert_management_alert_metric_images VALIDATE CONSTRAINT check_2587666252;")
   -> 0.0010s
== 20220120033115 CreateAlertManagementAlertMetricImages: migrated (0.0964s) ==
Down
== 20220120033115 CreateAlertManagementAlertMetricImages: reverting ===========
-- drop_table(:alert_management_alert_metric_images, {:if_exists=>true})
   -> 0.0216s
== 20220120033115 CreateAlertManagementAlertMetricImages: reverted (0.0217s) ==

API Request & responses

Upload Image

Curl
curl --location --request POST 'http://gdk.test:3000/api/v4/projects/6/alert_management_alerts/12/metric_images' \
--header 'PRIVATE-TOKEN: glpat-DEpkrRZUMVgm6ixjRGCr' \
--form 'file=@"/Users/seanarnold/Downloads/s7mnOhs.jpg"' \
--form 'url="http://gitlab.com"' \
--form 'url_text="Grafana metric"'
Response
{
    "id": 4,
    "created_at": "2022-01-27T02:25:13.343Z",
    "filename": "s7mnOhs.jpg",
    "file_path": "http://gdk.test:3000/uploads/-/system/alert_management_metric_image/file/4/s7mnOhs.jpg",
    "url": "http://gitlab.com",
    "url_text": "Grafana metric"
}

View images

Curl
curl --location --request GET 'http://gdk.test:3000/api/v4/projects/6/alert_management_alerts/12/metric_images' \
--header 'PRIVATE-TOKEN: glpat-DEpkrRZUMVgm6ixjRGCr'
Response
[
    {
        "id": 2,
        "created_at": "2022-01-27T02:17:39.419Z",
        "filename": "s7mnOhs.jpg",
        "file_path": "http://gdk.test:3000/uploads/-/system/alert_management_metric_image/file/2/s7mnOhs.jpg",
        "url": "http://gitlab.com",
        "url_text": "Grafana metric"
    },
    {
        "id": 3,
        "created_at": "2022-01-27T02:18:24.055Z",
        "filename": "s7mnOhs.jpg",
        "file_path": "http://gdk.test:3000/uploads/-/system/alert_management_metric_image/file/3/s7mnOhs.jpg",
        "url": "http://gitlab.com",
        "url_text": "Grafana metric"
    }
]

Update images

Curl
curl --location --request PUT 'http://gdk.test:3000/api/v4/projects/6/alert_management_alerts/12/metric_images/2' \
--header 'PRIVATE-TOKEN: glpat-DEpkrRZUMVgm6ixjRGCr' \
--form 'url="http://gitlab.com"'
Response
{
    "id": 2,
    "created_at": "2022-01-27T02:17:39.419Z",
    "filename": "s7mnOhs.jpg",
    "file_path": "http://gdk.test:3000/uploads/-/system/alert_management_metric_image/file/2/s7mnOhs.jpg",
    "url": "http://gitlab.com",
    "url_text": "Grafana metric"
}

Delete images

Curl
curl --location --request DELETE 'http://gdk.test:3000/api/v4/projects/6/alert_management_alerts/12/metric_images/2' \
--header 'PRIVATE-TOKEN: glpat-DEpkrRZUMVgm6ixjRGCr' \
--form 'url="http://gitlab.com"'
Response
<204 response>

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

  1. Create an Alert for a project: AlertManagement::Alert.create(title: 'Alert', project: Project.last, started_at: Time.current)
  2. Call the API as described above.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #340852 (closed)

Edited by Sean Arnold

Merge request reports