정의 파일에서 Cloud Monitoring 대시보드 만들기

전언


Google Cloud의 Cloud Monitoring에서는 웹의 GUI 그래픽으로 대시보드를 만들 수 있습니다.이것은 매우 편리한 기능이다
  • 여러 환경에서 동일한 대시보드를 준비하려면(예를 들어 개발 환경, 공식 환경에서 동일한 대시보드를 제작하려는 경우) 각 환경에서 대시보드를 제작, 업데이트해야 합니다.
  • 대시보드 버전 관리를 수행할 수 없으며 업데이트에 대한 의견을 구현하기 어렵습니다.
  • 잠깐만, 이것만 해도 간지러운 데가 닿지 않아.
    이러한 단점을 보완하기 위해 클라우드 모니터링에는 정의 파일에서 대시보드를 생성할 수 있는 처리 대시보드의 API가 준비되어 있습니다.
    https://cloud.google.com/monitoring/dashboards/api-dashboard?hl=ja

    대시보드 제작 프로세스


    그럼에도 불구하고 제로에서 JSON 파일을 만드는 것은 매우 번거롭다
  • 웹 GUI(Cloud Connose)를 사용하여 대시보드 수동 제작
  • 제작된 대시보드에서 정의 파일 내보내기
  • 정의 파일을 사용하여 API에서 대시보드 재생성
  • 이런 절차가 좋겠죠.
    대시보드가 API로 제작되면 정의 파일을 수정하고 대시보드를 업데이트합니다.

    전제 조건

  • 클라우드 모니터에 대한 권한 있음

  • Google Cloud SDK 설치

  • jq 명령이 설치되었음
  • Cloud Constore에서 대시보드 수동 제작


    먼저 클라우드 콘스토어에서 차트를 보면서 대시보드를 만듭니다.
    도표의 배치도 도형으로 할 수 있으니 우선 최종적으로 필요한 계기판을 만들어 보세요.

    기존 대시보드에서 정의 파일 내보내기


    대시보드 에셋 이름 가져오기


    대시보드를 제작한 후 먼저 gcloud 명령을 사용하여 대시보드의 자원 이름을 얻습니다.
    gcloud monitoring dashboards list --format="table[box](displayName,name)"
    
  • 상기 명령 결과의 displayName는 디스플레이 이름이고 name는 자원 이름이다.
  • 명령 실행 예
    $ gcloud monitoring dashboards list --format="table[box](displayName,name)"
    ┌────────────────────┬───────────────────────────────────────────────────────────────────────┐
    │    DISPLAY_NAME    │                                  NAME                                 │
    ├────────────────────┼───────────────────────────────────────────────────────────────────────┤
    │ Sample Dashboard   │ projects/xx4500xxxxxx/dashboards/1dxxf15x-773x-49cx-b97x-035xxfbf6bax │
    │ Sample Dashboard 2 │ projects/xx4500xxxxxx/dashboards/exxx699x-ac7x-4xx8-bxx4-xxf78xxx334d │
    └────────────────────┴───────────────────────────────────────────────────────────────────────┘
    
    https://cloud.google.com/sdk/gcloud/reference/monitoring/dashboards/list?hl=ja

    대시보드 정의 내보내기


    다음에 가져온 자원 이름을 지정하고 대시보드 정의를 내보냅니다.
    형식에 JSON을 지정합니다.
    gcloud monitoring dashboards describe $DASHBOARD_RESOURCE_NAME --format="json" | jq "del(.etag,.name)" > ./dashboard.json
    
  • $DASHBOARD_RESOURCE_NAME에서 방금 얻은 계기판의 자원 이름을 지정합니다.
  • etag, name 필드는 대시보드의 새 버전을 삭제할 필요가 없습니다.
  • 명령 실행 예
    $ gcloud monitoring dashboards describe projects/xx4500xxxxxx/dashboards/1dxxf15x-773x-49cx-b97x-035xxfbf6bax --format="json" | jq "del(.etag,.name)" 
    {
      "displayName": "Sample Dashboard",
      "mosaicLayout": {
        "columns": 12,
        "tiles": [
          {
            "height": 4,
            "widget": {
              "title": "Audited Log bytes",
              "xyChart": {
                "chartOptions": {
                  "mode": "COLOR"
                },
                "dataSets": [
                  {
                    "minAlignmentPeriod": "60s",
                    "plotType": "STACKED_AREA",
                    "timeSeriesQuery": {
                      "timeSeriesFilter": {
                        "aggregation": {
                          "alignmentPeriod": "60s",
                          "perSeriesAligner": "ALIGN_RATE"
                        },
                        "filter": "metric.type=\"logging.googleapis.com/byte_count\" resource.type=\"audited_resource\"",
                        "secondaryAggregation": {
                          "alignmentPeriod": "60s",
                          "perSeriesAligner": "ALIGN_MEAN"
                        }
                      }
                    }
                  }
                ],
                "timeshiftDuration": "0s",
                "yAxis": {
                  "label": "y1Axis",
                  "scale": "LINEAR"
                }
              }
            },
            "width": 5
          },
          {
            "height": 4,
            "widget": {
              "title": "Log entries [MEAN]",
              "xyChart": {
                "chartOptions": {
                  "mode": "COLOR"
                },
                "dataSets": [
                  {
                    "minAlignmentPeriod": "60s",
                    "plotType": "LINE",
                    "timeSeriesQuery": {
                      "timeSeriesFilter": {
                        "aggregation": {
                          "alignmentPeriod": "60s",
                          "perSeriesAligner": "ALIGN_RATE"
                        },
                        "filter": "metric.type=\"logging.googleapis.com/log_entry_count\" resource.type=\"global\"",
                        "secondaryAggregation": {
                          "alignmentPeriod": "60s",
                          "perSeriesAligner": "ALIGN_MEAN"
                        }
                      }
                    }
                  }
                ],
                "timeshiftDuration": "0s",
                "yAxis": {
                  "label": "y1Axis",
                  "scale": "LINEAR"
                }
              }
            },
            "width": 4,
            "xPos": 5
          }
        ]
      }
    }
    
    이렇게 하면 정의 파일을 가져올 수 있습니다.
    https://cloud.google.com/sdk/gcloud/reference/monitoring/dashboards/describe?hl=ja

    정의 파일에서 대시보드 만들기


    앞의 순서에서 대시보드 정의 파일을 내보냈으며 이 파일을 사용하여 대시보드를 다시 만듭니다.

    gcloud

    gcloud 명령을 사용하여 대시보드를 제작할 수 있습니다.
    gcloud monitoring dashboards create --config-from-file ./dashboard.json
    
    명령 집행 예
    gcloud monitoring dashboards create --config-from-file ./dashboard.json
    Created [83xxc447-8x14-4x4a-90cx-0bx064dxx0c9].--config-from-file 옵션은 이전에 내보낸 정의 파일의 경로를 지정합니다.이번에 JSON 파일을 사용했는데 gcloud 명령이면 YAML 파일도 가능합니다.
    https://cloud.google.com/sdk/gcloud/reference/monitoring/dashboards/create

    Terraform


    Terraform도 지원됩니다.
    resource "google_monitoring_dashboard" "dashboard" {
      dashboard_json = file("./dashboard.json")
    }
    
    https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_dashboard

    참고 자료


    https://stackoverflow.com/questions/61592910/how-to-export-and-import-google-cloud-monitoring-dashboards-between-projects-usi

    좋은 웹페이지 즐겨찾기