一键导入
promtracker
Guidelines for integrating and using promtracker for Prometheus metrics in Motoko canisters.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Guidelines for integrating and using promtracker for Prometheus metrics in Motoko canisters.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | promtracker |
| description | Guidelines for integrating and using promtracker for Prometheus metrics in Motoko canisters. |
This skill provides guidelines for agents to integrate and use promtracker in Motoko canisters on the Internet Computer.
import PT "mo:promtracker";
// Import only the modules you actually use to enable dot-notation
import { Counter; Tracker } "mo:promtracker";
import Http "mo:promtracker/mixins/http"; // For simple setup
import Http_ "mo:promtracker/Http"; // For custom manual setup
Importing Counter, Gauge, Heatmap, Tracker explicitly enables dot-notation like ctr.add(1) instead of PT.Counter.add(ctr, 1). Only import the modules that are actually used in your code.
The Renderer is always required to collect and render metrics. The Tracker is only needed if you want to use counters, gauges, or heatmaps that persist across upgrades.
Use this if you only need system metrics or custom pull values. No Tracker is required.
persistent actor Main {
// Renderer is transient and re-initialized on upgrade
transient let renderer = PT.Renderer();
// Expose metrics via HTTP
include Http(renderer.renderExposition, "/metrics");
// Initialize renderer with pull values
renderer.addValue(PT.allSystemMetrics);
renderer.addCanisterLabel(Main);
};
Use this if you need stateful metrics (Counters, Gauges, Heatmaps). The Tracker should be persistent.
persistent actor Main {
// Tracker is persistent to maintain metric values across upgrades
let pt = Tracker.new();
// Renderer is transient and re-initialized on upgrade
transient let renderer = PT.Renderer();
// Expose metrics via HTTP
include Http(renderer.renderExposition, "/metrics");
// Initialize renderer
renderer.addValue(pt.toValue());
renderer.addValue(PT.allSystemMetrics);
renderer.addCanisterLabel(Main);
};
In addition to HTTP exposition, promtracker provides other mixins:
Allows other canisters to query metrics using Candid via pt_query().
import Query "mo:promtracker/mixins/query";
// inside actor
include Query(renderer.read);
Provides an uptime function that returns the number of seconds since the canister was started/upgraded.
import Uptime "mo:promtracker/mixins/uptime";
// inside actor
include Uptime();
renderer.addValue(PT.newValue("uptime_seconds", [], uptime));
If you need to serve other custom routes alongside /metrics, you must implement http_request manually instead of using the Http mixin.
import PT "mo:promtracker";
import Http "mo:promtracker/Http";
persistent actor Main {
transient let renderer = PT.Renderer();
public query func http_request(req : Http.Request) : async Http.Response {
let ?path = req.url.split(#char '?').next() else return Http.render400();
switch (req.method, path) {
case ("GET", "/metrics") {
// Use renderer.renderExposition() for Prometheus metrics
Http.renderPlainText(renderer.renderExposition());
};
case ("GET", "/hello") {
Http.renderPlainText("Hello, world!");
};
case (_) Http.render400();
};
};
// Initialize renderer as usual
renderer.addValue(PT.allSystemMetrics);
};
Add pre-defined system and RTS metrics to the renderer.
renderer.addValue(PT.allSystemMetrics);
// Or specific ones:
renderer.addValue(PT.cyclesBalanceMetric);
renderer.addValue(PT.canisterVersionMetric);
Use counters for values that only increase.
let requestCounter = pt.newCounter("requests_total", [("method", "GET")]);
requestCounter.add(1);
Use gauges for values that can go up and down. Gauges in promtracker support watermarks (min/max/average over a period).
// Gauge with limits for watermarks (optional)
let memoryGauge = pt.newGauge("memory_usage_bytes", [], [1024, 1024 * 1024]);
memoryGauge.update(500000);
Use heatmaps to track distribution of values (e.g., latencies) using power-of-2 buckets.
let latencyHeatmap = pt.newHeatmap("request_latency_seconds", []);
latencyHeatmap.add(120); // Adds value to corresponding bucket
Use pull values to dynamically read a value when metrics are requested.
renderer.addValue(PT.newValue("custom_metric", [], func() = 42));
transient variable for the Renderer and initialize it in the actor body (or constructor).Tracker only if you need stateful metrics like counters or gauges. For simple system metric monitoring, the Renderer alone is sufficient.mo:promtracker/mixins/http) for simple cases. If you need multiple custom HTTP routes, implement http_request manually using renderer.renderExposition().pt.newTracker([("subsystem", "api")]) to create nested trackers for better organization.Http mixin and Http.renderPlainText helper handle this automatically.Counter, Gauge, Heatmap, Tracker) that you actually use.