šŸ“” SNMP Extensions — Module 7

Calculated Metrics (func:)

Hands-on

What Are func: Metrics?

Some values you want to display don't exist as raw OIDs. For example:

  • Uptime in hours — SNMP gives you ticks (hundredths of seconds), but you want hours
  • Memory percentage — SNMP gives you used and free bytes, but you want a percentage
  • Bandwidth utilization % — SNMP gives you byte counters and speed, but you want utilization

func: metrics are computed server-side by the Metrics API v2. You define a formula using existing metrics, and Dynatrace calculates the result on the fly.

Syntax

metrics:
  - key: func:my_device.uptime_hours
    query:
      metricSelector: "(my_device.sysuptime:splitBy(\"dt.entity.my_device:device\") / 360000)"
    metadata:
      displayName: Uptime (Hours)
      unit: Hour

Key rules:

  • Prefix the key with func:
  • query.metricSelector uses Metrics API v2 selector syntax
  • Must include :splitBy() with entity dimensions
  • Quotes inside the selector must be escaped: \"

Common Patterns

Uptime in Hours

sysUpTime is in hundredths of seconds. Divide by 360,000 to get hours:

- key: func:my_device.uptime_hours
  query:
    metricSelector: "(my_device.sysuptime:splitBy(\"dt.entity.my_device:device\") / 360000)"
  metadata:
    displayName: Uptime (Hours)
    unit: Hour

Memory Usage Percentage

Calculate from used and free:

- key: func:my_device.memory_usage_pct
  query:
    metricSelector: >-
      (my_device.memory_used:splitBy("dt.entity.my_device:device"))
      / (my_device.memory_used:splitBy("dt.entity.my_device:device")
      + my_device.memory_free:splitBy("dt.entity.my_device:device"))
      * 100
  metadata:
    displayName: Memory Usage
    unit: Percent

Bandwidth Utilization %

Bytes per second as percentage of interface speed:

- key: func:my_device.if.bandwidth_in_pct
  query:
    metricSelector: >-
      ((my_device.if.in.octets.count:splitBy("dt.entity.my_device:interface") * 8)
      :rate(1s))
      / (my_device.if.speed:splitBy("dt.entity.my_device:interface") * 1000000)
      * 100
  metadata:
    displayName: Bandwidth In (%)
    unit: Percent

Critical Gotchas

Gotcha                                    Impact                          Workaround
────────────────────────────────────────  ──────────────────────────────  ──────────────────────
func: metrics are NOT stored in Grail    Cannot query via DQL            Use base metrics in DQL
Cannot use func: with METRIC_KEY alerts  Alert creation fails            Use METRIC_SELECTOR type
Backtick quoting needed in DQL           `func:key` returns no data      Don't use func: in DQL
Work only in screens and dashboards      Not available in Notebooks      Use metricSelector API
Must include :splitBy()                  Missing = wrong aggregation     Always include entity dim

Using func: in Screens

charts:
  - displayName: Uptime
    visualizationType: SINGLE_VALUE
    singleValueConfig:
      metric:
        metricSelector: func:my_device.uptime_hours:splitBy("dt.entity.my_device:device")

  - displayName: Memory %
    visualizationType: GRAPH_CHART
    graphChartConfig:
      metrics:
        - metricSelector: func:my_device.memory_usage_pct:splitBy("dt.entity.my_device:device")
      yAxes:
        - key: mem
          position: LEFT
          visible: true
          min: "0"
          max: "100"

Using func: in Alerts

For alerts on func: metrics, you must use METRIC_SELECTOR query type, not METRIC_KEY:

resource "dynatrace_metric_events" "memory_alert" {
  query_definition {
    type            = "METRIC_SELECTOR"
    metric_selector = "func:my_device.memory_usage_pct:splitBy(\"dt.entity.my_device:device\")"
    aggregation     = "AVG"
  }
  model_properties {
    type               = "STATIC_THRESHOLD"
    threshold          = 90
    alert_condition    = "ABOVE"
    violating_samples  = 3
    samples            = 35
    dealerting_samples = 5
  }
}

What's Next

In Module 8, we'll put it all together — assemble, sign, upload, and configure your complete extension in a Dynatrace environment.

šŸ›  Hands-On Exercise

Edit the YAML in the editor, then click "Check My Work" to validate.

Calculated Metrics with func:

Add three func: calculated metrics to this extension:

  1. Uptime in hours: Divide sysUpTime by 360000 (timeticks to hours)
  2. Memory usage %: used / (used + free) * 100
  3. Bandwidth utilization %: (bytes * 8) rate per second / (speed * 1000000) * 100

Remember: func: metrics use query.metricSelector syntax and must include :splitBy().

extension.yamlYAML
Loading...