# Configures either an alert or a target record for precomputation.apiVersion:logging.gdc.goog/v1kind:LoggingRulemetadata:# Choose a namespace that matches the project namespace.# The alert or record is produced in the same namespace.namespace:PROJECT_NAMESPACEname:LOGGING_RULE_NAMEspec:# Choose the log source to base alerts on (operational or audit logs).# Optional.# Valid options: 'operational' and 'audit'# Default value: 'operational'source:operational# Rule evaluation interval.interval:60s# Configure the limit for the number of alerts.# A value of '0' means no limit.# Optional.# Default value: '0'limit:0# Configure recording rules to generate new metrics based on existing logs.# Recording rules precompute expressions that are frequently needed or computationally expensive.# Results are saved as a new set of time series.recordRules:# Define the time series where you want to write the recording rule.# The value must be a valid metric name.-record:MyMetricsName# Define the LogQL expression to evaluate for this rule.expr:rate({service_name="bob-service"} [1m])# Define labels to add or overwrite.# Map of key-value pairs.# Optional.labels:verb:read
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[],[],null,["# Create metrics from logs\n\nThis page describes how to generate new metrics from existing logs by deploying\na `LoggingRule` custom resource in Google Distributed Cloud (GDC) air-gapped appliance.\n\nRecording rules allow you to precompute frequently used or computationally\nexpensive expressions, improving performance for dashboards and alerts. Defined\nwithin `LoggingRule` custom resources, these rules generate new time series\nmetrics from existing logs, enhancing data observability.\n\nBy storing precomputed results, recording rules eliminate redundant calculations\neach time an expression is needed. This method significantly speeds up query\ntimes, particularly for complex dashboards and alerts that require repeated\nevaluations of the same expression. Essentially, `LoggingRule` resources let you\nderive new metrics from existing logging data by specifying the necessary\ncalculations within their recording rules.\n\nBefore you begin\n----------------\n\nTo get the permissions that you need to manage `LoggingRule` custom\nresources, ask your Organization IAM Admin or Project IAM Admin to grant you one\nof the associated `LoggingRule` roles.\n\nDepending on the level of access and permissions you need, you might obtain\ncreator, editor, or viewer roles for this resource in an organization or a\nproject. For more information, see [Prepare IAM permissions](/distributed-cloud/hosted/docs/latest/appliance/platform/pa-user/obs-iam-permissions).\n\nCreate recording rules\n----------------------\n\nTo create a recording rule, define a name for the recorded log and a valid\nexpression that produces a numeric value. This expression, when evaluated,\ngenerates the new metric. Deploy the `LoggingRule` custom resource in your\nproject namespace on the Management API server to activate the recording rule.\n\nFor more information about recording rules, see\n\u003chttps://grafana.com/docs/loki/latest/rules/\u003e.\n| **Important:** Before creating recording rules, ensure you have a deployed project with active workloads and configured logging targets. In this way, the rules have data to operate on. For more information, see [Collect logs](/distributed-cloud/hosted/docs/latest/appliance/platform/pa-user/collect-logs).\n\nFollow these steps to create recording rules in your project namespace:\n\n1. Determine the GDC project from which you want to create recording rules.\n2. Create a YAML file defining the `LoggingRule` custom resource.\n\n [The complete `LoggingRule` specification](#loggingrule-recording-rules)\n shows an example for log-based recording rules. For more information, see\n the [API reference documentation](/distributed-cloud/hosted/docs/latest/appliance/apis/service/logging/v1/logging-v1#loggingrule).\n3. Replace the following values in the YAML file according to your needs:\n\n4. Save the YAML file.\n\n5. Apply the `LoggingRule` configuration to the Management API server within\n the same namespace as your recording rules:\n\n kubectl --kubeconfig \u003cvar translate=\"no\"\u003eKUBECONFIG_PATH\u003c/var\u003e apply -f \u003cvar translate=\"no\"\u003eLOGGING_RULE_NAME\u003c/var\u003e.yaml\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eKUBECONFIG_PATH\u003c/var\u003e: the path to the kubeconfig file for the Management API server.\n - \u003cvar translate=\"no\"\u003eLOGGING_RULE_NAME\u003c/var\u003e: the name of the `LoggingRule` definition file.\n\nComplete `LoggingRule` specification\n------------------------------------\n\nA `LoggingRule` custom resource contains recording rules that describe the\nconditions to create new metrics based on existing logs for observability.\n\nThe following YAML file shows a template for the `LoggingRule` custom\nresource. For more information, see the\n[API reference documentation](/distributed-cloud/hosted/docs/latest/appliance/apis/service/logging/v1/logging-v1#loggingrule). \n\n # Configures either an alert or a target record for precomputation.\n apiVersion: logging.gdc.goog/v1\n kind: LoggingRule\n metadata:\n # Choose a namespace that matches the project namespace.\n # The alert or record is 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: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eLOGGING_RULE_NAME\u003c/span\u003e\u003c/var\u003e\n spec:\n # Choose the log source to base alerts on (operational or audit logs).\n # Optional.\n # Valid options: 'operational' and 'audit'\n # Default value: 'operational'\n source: operational\n\n # Rule evaluation interval.\n interval: 60s\n\n # Configure the limit for the number of alerts.\n # A value of '0' means no limit.\n # Optional.\n # Default value: '0'\n limit: 0\n\n # Configure recording rules to generate new metrics based on existing logs.\n # Recording rules precompute expressions that are frequently needed or computationally expensive.\n # Results are saved as a new set of time series.\n recordRules:\n # Define the time series where you want to write the recording rule.\n # The value must be a valid metric name.\n - record: MyMetricsName\n\n # Define the LogQL expression to evaluate for this rule.\n expr: rate({service_name=\"bob-service\"} [1m])\n\n # Define labels to add or overwrite.\n # Map of key-value pairs.\n # Optional.\n labels:\n verb: read\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003ePROJECT_NAMESPACE\u003c/var\u003e: your project namespace.\n- \u003cvar translate=\"no\"\u003eLOGGING_RULE_NAME\u003c/var\u003e: the name of the `LoggingRule` definition file."]]