Changeset - 623836db99af
[Not reviewed]
default
0 1 1
drewp@bigasterisk.com - 19 months ago 2023-06-08 22:05:59
drewp@bigasterisk.com
fix ts warning
2 files changed with 13 insertions and 14 deletions:
0 comments (0 inline, 0 general)
light9/web/lib/parse-prometheus-text-format.d.ts
Show inline comments
 
new file 100644
 
declare module "parse-prometheus-text-format" {
 
  function parsePrometheusTextFormat(s: string): any;
 
  export default parsePrometheusTextFormat;
 
}
light9/web/metrics/StatsLine.ts
Show inline comments
 
@@ -49,25 +49,25 @@ export class StatsLine extends LitElemen
 
      if (propName == "name") {
 
        const reload = () => {
 
          fetch("/service/"+this.name + "/metrics").then((resp) => {
 
            if (resp.ok) {
 
              resp
 
                .text()
 
                .then((msg) => {
 
                  this.stats = parsePrometheusTextFormat(msg) as Metrics;
 
                  this.extractProcessStats(this.stats);
 
                  setTimeout(reload, 1000);
 
                })
 
                .catch((err) => {
 
                  log(`${this.name} failing`, err)
 
                  log(`${this.name} failing`, err);
 
                  setTimeout(reload, 1000);
 
                });
 
            } else {
 
              if (resp.status == 502) {
 
                setTimeout(reload, 5000);
 
              }
 
              // 404: likely not mapped to a responding server
 
            }
 
          });
 
        };
 
        reload();
 
      }
 
@@ -186,25 +186,24 @@ export class StatsLine extends LitElemen
 
        ${cols.map(th)}
 
      </tr>
 
      <tr>
 
        ${cols.map(td)}
 
      </tr>
 
    </table>`;
 
  }
 

	
 
  drawLevel(d: Metric, path: string[]) {
 
    return html`[NEW ${JSON.stringify(d)} ${path}]`;
 
  }
 

	
 

	
 
  valueDisplay(m: Metric, v: Value): TemplateResult {
 
    if (m.type == "GAUGE") {
 
      return html`${v.value}`;
 
    } else if (m.type == "COUNTER") {
 
      return html`${v.value}`;
 
    } else if (m.type == "HISTOGRAM") {
 
      return this.histoDisplay(v.buckets!);
 
    } else if (m.type == "UNTYPED") {
 
      return html`${v.value}`;
 
    } else if (m.type == "SUMMARY") {
 
      if (!v.count) {
 
        return html`err: summary without count`;
 
@@ -240,40 +239,36 @@ export class StatsLine extends LitElemen
 
      const h = clamp((delta / maxDelta) * maxBarH, 1, maxBarH);
 
      lines.push(
 
        html`<div
 
          title="bucket=${level} count=${count}"
 
          style="background: yellow; margin-right: 1px; width: 8px; height: ${h}px; display: inline-block"
 
        ></div>`
 
      );
 
    }
 
    return html`${firstLevel} ${lines} ${lastLevel}`;
 
  }
 

	
 
  tightLabel(labs: { [key: string]: string }): string {
 
    const d: { [key: string]: string } = {}
 
    const d: { [key: string]: string } = {};
 
    for (let k in labs) {
 
      if (k == 'app_name') continue;
 
      if (k == 'output') continue;
 
      if (k=='status_code'&&labs[k]=="200") continue;
 
      d[k] = labs[k]
 
      if (k == "app_name") continue;
 
      if (k == "output") continue;
 
      if (k == "status_code" && labs[k] == "200") continue;
 
      d[k] = labs[k];
 
    }
 
    const ret = JSON.stringify(d)
 
    return ret == "{}" ? "" : ret
 
    const ret = JSON.stringify(d);
 
    return ret == "{}" ? "" : ret;
 
  }
 
  tightMetric(name: string): string {
 
    return name
 
    .replace('starlette', '⭐')
 
    .replace("_request" ,"_req")
 
    .replace("_duration" ,"_dur")
 
    .replace('_seconds', '_s')
 
    return name.replace("starlette", "⭐").replace("_request", "_req").replace("_duration", "_dur").replace("_seconds", "_s");
 
  }
 
  render() {
 
    const now = Date.now() / 1000;
 

	
 
    const displayedStats = this.stats.filter(nonBoring);
 
    return html`
 
      <div>
 
        <table>
 
          ${displayedStats.map(
 
      (row, rowNum) => html`
 
              <tr>
 
                <th>${this.tightMetric(row.name)}</th>
0 comments (0 inline, 0 general)