Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Menghitung metrik berarti menghasilkan metrik baru berdasarkan log atau metrik yang sudah ada untuk kemampuan pengamatan data. Anda menghitung metrik di GDC dengan mengonfigurasi aturan perekaman di resource kustom MonitoringRule. Aturan perekaman melakukan pra-komputasi ekspresi yang sering diperlukan atau memerlukan biaya komputasi yang tinggi. Aturan ini menyimpan hasilnya sebagai deret waktu baru. Mengirim kueri hasil yang telah dihitung sebelumnya lebih cepat daripada mengeksekusi ekspresi asli setiap kali diperlukan. Pra-komputasi ini sangat berguna untuk dasbor dan pemberitahuan kompleks, yang harus mengkueri ekspresi yang sama berulang kali setiap kali diperbarui atau dievaluasi.
Anda mengonfigurasi aturan perekaman di resource kustom MonitoringRule untuk menghitung metrik berdasarkan metrik pemantauan sistem lainnya. Resource kustom MonitoringRule berisi aturan perekaman yang menjelaskan kondisi untuk menghitung metrik baru berdasarkan metrik yang sudah ada.
Sebelum memulai
Untuk mendapatkan izin yang Anda perlukan untuk menghitung metrik dari metrik, minta Admin IAM Project Anda untuk memberi Anda salah satu peran berikut:
Editor Aturan Pemantauan: mengedit atau mengubah MonitoringRule resource kustom. Minta peran Editor Aturan Pemantauan (monitoringrule-editor).
Monitoring Rule Viewer: melihat MonitoringRule resource kustom. Minta peran Monitoring Rule Viewer (monitoringrule-viewer).
Membuat aturan perekaman
Tentukan nama rekaman dan ekspresi valid yang mengevaluasi aturan. Ekspresi harus diselesaikan ke nilai numerik untuk merekamnya sebagai metrik baru. Deploy resource kustom di namespace project Anda pada cluster admin untuk membuat aturan perekaman.
Ikuti langkah-langkah berikut untuk membuat aturan rekaman dan menghitung metrik di namespace project Anda:
Buka atau buat file YAML untuk CR menggunakan template CR MonitoringRule untuk aturan rekaman pemantauan.
Di kolom namespace CR, masukkan namespace project Anda.
Di kolom name, masukkan nama untuk konfigurasi aturan.
Opsional: Anda dapat memilih sumber log di kolom source. Opsi yang valid adalah operational dan audit.
Di kolom interval, masukkan jumlah detik untuk durasi interval evaluasi aturan.
Opsional: Di kolom limit, masukkan jumlah maksimum pemberitahuan. Masukkan 0 untuk pemberitahuan tak terbatas.
Di kolom recordRules, masukkan informasi berikut untuk menghitung metrik:
Di kolom record, masukkan nama rekaman. Nilai ini menentukan deret waktu tempat aturan penulisan data dan harus berupa nama metrik yang valid.
Di kolom expr, masukkan ekspresi PromQL untuk aturan metrik.
Ekspresi ini harus diselesaikan ke nilai numerik untuk direkam sebagai metrik baru.
Opsional: Di kolom labels, tentukan label yang ingin Anda tambahkan atau ganti sebagai pasangan nilai kunci.
Simpan file YAML CR.
Deploy CR di namespace project cluster admin untuk membuat aturan rekaman.
Aturan rekaman MonitoringRule
Resource kustom MonitoringRule berisi aturan perekaman yang menjelaskan kondisi untuk menghitung metrik baru berdasarkan metrik pemantauan sistem yang sudah ada.
File YAML berikut menunjukkan template untuk kolom recordRules CR MonitoringRule.
# Configures either an alert or a target record for precomputationapiVersion:monitoring.gdc.goog/v1alpha1kind:MonitoringRulemetadata:# Choose namespace that matches the project's namespace# Note: The alert or record will be produced in the same namespacenamespace:PROJECT_NAMESPACEname:alerting-configspec:# Rule evaluation intervalinterval:<duration>
# Configure limit for number of alerts (0: no limit)# Optional, Default: 0 (no limit)limit:<int>
# Configure record rules to generate new metrics based on pre-existing metrics.# Record rules precompute expressions that are frequently needed or computationally expensive.# These rules save their result as a new set of time series.recordRules:# Define which timeseries to write to (must be a valid metric name)-record:<string>
# Define PromQL expression to evaluate for this ruleexpr:<string>
# Define labels to add or overwrite# Optional, Map of {key, value} pairslabels:<labelname>:<labelvalue>
...
Ganti PROJECT_NAMESPACE dengan namespace project Anda.
Aturan rekaman LoggingRule
CR LoggingRule berisi aturan rekaman yang menjelaskan kondisi untuk menghitung metrik baru berdasarkan log yang sudah ada.
File YAML berikut menunjukkan template untuk kolom recordRules CR LoggingRule.
# Configures either an alert or a target record for precomputationapiVersion:logging.gdc.goog/v1alpha1kind:LoggingRulemetadata:# Choose namespace that matches the project's namespace# Note: The alert or record will be produced in the same namespacenamespace:g-fleetns-aname:alerting-configspec:# Choose which log source to base alerts on (Operational/Audit/Security Logs)# Optional, Default: Operationalsource:<string>
# Rule evaluation intervalinterval:<duration>
# Configure limit for number of alerts (0: no limit)# Optional, Default: 0 (no limit)limit:<int>
# Configure record rules to generate new metrics based on pre-existing logs.# Record rules generate metrics based on logs.# Use record rules for complex alerts, which query the same expression repeatedly every time they are evaluated.recordRules:# Define which timeseries to write to (must be a valid metric name)-record:<string>
# Define LogQL expression to evaluate for this rule# https://grafana.com/docs/loki/latest/rules/expr:<string>
# Define labels to add or overwrite# Optional, Map of {key, value} pairslabels:<labelname>:<labelvalue>
...
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-04 UTC."],[[["\u003cp\u003eCalculating metrics in GDC involves creating recording rules within a \u003ccode\u003eMonitoringRule\u003c/code\u003e custom resource, which precompute expressions for faster querying.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eMonitoringRule\u003c/code\u003e custom resource defines conditions to calculate new metrics using pre-existing system monitoring metrics, improving efficiency for dashboards and complex alerts.\u003c/p\u003e\n"],["\u003cp\u003eTo manage \u003ccode\u003eMonitoringRule\u003c/code\u003e custom resources, users need either the Monitoring Rule Editor or Monitoring Rule Viewer IAM role.\u003c/p\u003e\n"],["\u003cp\u003eCreating recording rules requires defining a record name, a PromQL expression that resolves to a numeric value, and deploying the custom resource in the project namespace.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eLoggingRule\u003c/code\u003e custom resources can also define recording rules to calculate new metrics based on pre-existing logs, utilizing LogQL expressions and offering options for specifying the log source.\u003c/p\u003e\n"]]],[],null,["# Calculate metrics from metrics data\n\nCalculating metrics means generating new metrics based on pre-existing logs or metrics for data observability. You calculate metrics in GDC by configuring recording rules in a `MonitoringRule` custom resource. Recording rules precompute expressions that are frequently needed or computationally expensive. These rules save their result as a new set of time series. Querying the precomputed result is faster than executing the original expression whenever needed. This precomputation is especially useful for dashboards and complex alerts, which must query the same expression repeatedly every time they refresh or are evaluated.\n\nYou configure recording rules in the `MonitoringRule` custom resource to calculate metrics based on other system monitoring metrics. The `MonitoringRule` custom resource contains recording rules that describe the conditions to calculate new metrics based on pre-existing metrics.\n\nBefore you begin\n----------------\n\nTo get the permissions you need to calculate metrics from metrics, ask your Project IAM Admin to grant you one of the following roles:\n\n- **Monitoring Rule Editor** : edits or modifies `MonitoringRule` custom resources. Request the Monitoring Rule Editor (`monitoringrule-editor`) role.\n- **Monitoring Rule Viewer** : views `MonitoringRule` custom resources. Request the Monitoring Rule Viewer (`monitoringrule-viewer`) role.\n\nCreate recording rules\n----------------------\n\nDefine a record name and a valid expression that evaluates the rule. The expression must resolve to a numeric value to record it as a new metric. Deploy the custom resource in your project namespace on the admin cluster to create the recording rules.\n| **Important:** To create rules, verify that a project is deployed with workloads. To have data your rules can act on, verify that you configured scraping metrics.\n\nFor more information about recording rules, see \u003chttps://grafana.com/docs/loki/latest/rules/\u003e\n\nWork through the following steps to create record rules and calculate metrics in your project's namespace:\n\n1. Open or create the YAML file for the CR using [the `MonitoringRule` CR](#monitoringrule-recordrules) template for monitoring record rules.\n2. In the `namespace` field of the CR, enter your project's namespace.\n3. In the `name` field, enter the name for the rule configuration.\n4. Optional: You can choose the log source in the `source` field. Valid options are `operational` and `audit`.\n5. In the `interval` field, enter the number of seconds for the duration of the rule evaluation interval.\n6. Optional: In the `limit` field, enter the maximum number of alerts. Enter `0` for unlimited alerts.\n7. In the `recordRules` field, enter the following information to calculate metrics:\n\n - In the `record` field, enter the record name. This value defines the time series in which to write the record rule and it must be a valid metric name.\n - In the `expr` field, enter a PromQL expression for the metric rule.\n\n This expression must resolve to a numeric value to be recorded as a new metric.\n - Optional: In the `labels` field, define the labels that you want to add or overwrite as key-value pairs.\n\n8. Save the YAML file of the CR.\n\n9. Deploy the CR in your project's namespace of the admin cluster to create the record rules.\n\nThe `MonitoringRule` record rules\n---------------------------------\n\nA `MonitoringRule` custom resource contains recording rules that describe the conditions to calculate new metrics based on pre-existing system monitoring metrics.\n\nThe following YAML file shows a template for the `recordRules` field of the `MonitoringRule` CR. \n\n # Configures either an alert or a target record for precomputation\n apiVersion: monitoring.gdc.goog/v1alpha1\n kind: MonitoringRule\n metadata:\n # Choose namespace that matches the project's namespace\n # Note: The alert or record will be produced in the same namespace\n namespace: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003ePROJECT_NAMESPACE\u003c/span\u003e\u003c/var\u003e\n name: alerting-config\n spec:\n # Rule evaluation interval\n interval: \u003cduration\u003e\n\n # Configure limit for number of alerts (0: no limit)\n # Optional, Default: 0 (no limit)\n limit: \u003cint\u003e\n\n # Configure record rules to generate new metrics based on pre-existing metrics.\n # Record rules precompute expressions that are frequently needed or computationally expensive.\n # These rules save their result as a new set of time series.\n recordRules:\n # Define which timeseries to write to (must be a valid metric name)\n - record: \u003cstring\u003e\n\n # Define PromQL expression to evaluate for this rule\n expr: \u003cstring\u003e\n\n # Define labels to add or overwrite\n # Optional, Map of {key, value} pairs\n labels:\n \u003clabelname\u003e: \u003clabelvalue\u003e\n ...\n\nReplace \u003cvar translate=\"no\"\u003ePROJECT_NAMESPACE\u003c/var\u003e with the namespace of your project.\n\nThe `LoggingRule` record rules\n------------------------------\n\nA `LoggingRule` CR contains record rules that describe the conditions to calculate new metrics based on pre-existing logs.\n\nThe following YAML file shows a template for the `recordRules` field of the `LoggingRule` CR. \n\n # Configures either an alert or a target record for precomputation\n apiVersion: logging.gdc.goog/v1alpha1\n kind: LoggingRule\n metadata:\n # Choose namespace that matches the project's namespace\n # Note: The alert or record will be produced in the same namespace\n namespace: g-fleetns-a\n name: alerting-config\n spec:\n # Choose which log source to base alerts on (Operational/Audit/Security Logs)\n # Optional, Default: Operational\n source: \u003cstring\u003e\n\n # Rule evaluation interval\n interval: \u003cduration\u003e\n\n # Configure limit for number of alerts (0: no limit)\n # Optional, Default: 0 (no limit)\n limit: \u003cint\u003e\n\n # Configure record rules to generate new metrics based on pre-existing logs.\n # Record rules generate metrics based on logs.\n # Use record rules for complex alerts, which query the same expression repeatedly every time they are evaluated.\n recordRules:\n # Define which timeseries to write to (must be a valid metric name)\n - record: \u003cstring\u003e\n\n # Define LogQL expression to evaluate for this rule\n # https://grafana.com/docs/loki/latest/rules/\n expr: \u003cstring\u003e\n\n # Define labels to add or overwrite\n # Optional, Map of {key, value} pairs\n labels:\n \u003clabelname\u003e: \u003clabelvalue\u003e\n ..."]]