Skip to content

Add useCurrentTime flag to HTTP metrics output timestamping#289

Merged
nmaguiar merged 3 commits into
masterfrom
copilot/add-use-current-time-flag
May 28, 2026
Merged

Add useCurrentTime flag to HTTP metrics output timestamping#289
nmaguiar merged 3 commits into
masterfrom
copilot/add-use-current-time-flag

Conversation

Copilot AI commented May 21, 2026

Copy link
Copy Markdown
Contributor

nOutput_HTTP_Metrics always emitted OpenMetrics timestamps based on recorded attribute/warning retrieval times. This change adds an opt-in useCurrentTime flag to emit scrape-time timestamps (Date.now()) instead.

  • Configuration

    • Added new boolean option in config/objects/nOutput_HTTP_Metrics.js:
      • useCurrentTime (default: false)
  • Timestamp selection for value/warning parsing

    • Updated _parse (current/last values) and _parsew (warnings) to select timestamp via:
      • recorded time when useCurrentTime=false (existing behavior)
      • Date.now() when useCurrentTime=true
  • Self metrics timestamping

    • Updated both OpenMetrics self-output paths in /metrics handler to pass an explicit timestamp only when useCurrentTime=true:
      • __ow_metrics_fromObj2OpenMetrics(ow.metrics.getAll(), parent.nameSelf, parent.useCurrentTime ? Date.now() : undefined)

Example:

useCurrentTime: true
var d = parent.useCurrentTime ? Date.now() : (new Date(r.date)).getTime();
Original prompt

Summary

Modify config/objects/nOutput_HTTP_Metrics.js to support a new boolean option flag useCurrentTime (defaults to false). When set to true, the current time (Date.now()) should be used as the timestamp passed to ow.metrics.fromObj2OpenMetrics / __ow_metrics_fromObj2OpenMetrics, instead of the recorded retrieval time stored in the attribute value.

Changes Required

1. Constructor — accept and store the new flag

In the nOutput_HTTP_Metrics constructor (where other flags like includeSelf, includeCVals, etc. are read from aMap), add:

this.useCurrentTime = _$(aMap.useCurrentTime, "useCurrentTime").isBoolean().default(false);

2. _parse function — use current time when flag is set

Current code (around line 294–303):

var _parse = (e, n) => {
    return ow.obj.fromObj2Array(e).map(r => {
        var d = (new Date(r.date)).getTime();
        delete r.date;
        var m = {}; m[r.name] = r.val;
        traverse(m, (k, v, p, o) => {
            if (isNull(v)) delete o[k]
        })
        return __ow_metrics_fromObj2OpenMetrics(m, n, d);
    }).join("");
}

Change to:

var _parse = (e, n) => {
    return ow.obj.fromObj2Array(e).map(r => {
        var d = parent.useCurrentTime ? Date.now() : (new Date(r.date)).getTime();
        delete r.date;
        var m = {}; m[r.name] = r.val;
        traverse(m, (k, v, p, o) => {
            if (isNull(v)) delete o[k]
        })
        return __ow_metrics_fromObj2OpenMetrics(m, n, d);
    }).join("");
}

3. _parsew function — use current time when flag is set

Current code (around line 306–319):

var _parsew = (e, n) => {
    var _e = []
    Object.keys(e).forEach(k => {
        _e = _e.concat(e[k].map(w => {
            var d = (new Date(w.lastupdate)).getTime()
            var m = {}; m[w.title] = clone(w)
            delete m[w.title].lastupdate
            delete m[w.title].notifications
            m[w.title].num = 1
            return __ow_metrics_fromObj2OpenMetrics(m, n, d)
        }))
    })
    return _e.join("")
}

Change to:

var _parsew = (e, n) => {
    var _e = []
    Object.keys(e).forEach(k => {
        _e = _e.concat(e[k].map(w => {
            var d = parent.useCurrentTime ? Date.now() : (new Date(w.lastupdate)).getTime()
            var m = {}; m[w.title] = clone(w)
            delete m[w.title].lastupdate
            delete m[w.title].notifications
            m[w.title].num = 1
            return __ow_metrics_fromObj2OpenMetrics(m, n, d)
        }))
    })
    return _e.join("")
}

4. Self-metrics calls — pass current time when flag is set

In the /metrics route handler, the self case calls __ow_metrics_fromObj2OpenMetrics(ow.metrics.getAll(), parent.nameSelf) without any timestamp. When useCurrentTime is true, pass Date.now() as the timestamp:

Change both occurrences (in the switch(req.params.type) block and the if (parent.includeSelf) block):

// from:
__ow_metrics_fromObj2OpenMetrics(ow.metrics.getAll(), parent.nameSelf)
// to:
__ow_metrics_fromObj2OpenMetrics(ow.metrics.getAll(), parent.nameSelf, parent.useCurrentTime ? Date.now() : undefined)

Usage example (YAML config)

useCurrentTime: true   # pass current scrape time instead of recorded retrieval time

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Change objects/nOutput*Metrics.js so that given an option flag to use the current time instead of the recorded time of retrieval.

Here is the help of the function:
ow.metrics.fromObj2OpenMetrics(aObj, aPrefix, aTimestamp, aHelpMap, aConvMap) : String

  • Given aObj will return a string of open metric (prometheus) metric strings. Optionally you can provide a prefix (defaults to "metric")
  • and/or aTimestamp (a number in seconds or a label field, that will be used for all aObj values) and aConvMap composed of a key with a map of possible values and corresponding
  • translation to numbers. Note: prefixes should not start with a digit.

Assistant: Let me start by examining the relevant files in the repository.

Assistant: Now I have a full picture of the file. Here's the plan for the changes to config/objects/nOutput_HTTP_Metrics.js:

  1. Constructor — add a new useCurrentTime option flag (defaults to false).
  2. _parse (line 296) — use Date.now() instead of (new Date(r.date)).getTime() when the flag is set.
  3. _parsew (line 310) — use Date.now() instead of (new Date(w.lastupdate)).getTime() when the flag is set.
  4. self metrics calls (lines 457 & 463) — pass Date.now() as the timestamp when the flag is set (currently no timestamp is passed at all for self-metrics).

Would you like me to open a PR with these changes, or just show you the diff first?

User: Yes

Assistant: We're waiting for your response to the confirmation dialog to proceed with creating the pull request.

This pull request was created from Copilot chat.

Copilot AI changed the title [WIP] Modify nOutput_HTTP_Metrics to support useCurrentTime flag Add useCurrentTime flag to HTTP metrics output timestamping May 21, 2026
Copilot AI requested a review from nmaguiar May 21, 2026 15:46
@abrizida abrizida marked this pull request as ready for review May 27, 2026 08:56

@abrizida abrizida left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and working in my environment.

@nmaguiar nmaguiar merged commit 6a3db97 into master May 28, 2026
1 check passed
@nmaguiar nmaguiar deleted the copilot/add-use-current-time-flag branch May 28, 2026 05:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants