ambrite-mdd library
Use this library in your server side (node) TypeScript (or javascript) code.
Uses Stackdriver to LOG and MONTIOR.
Logging
Log-line based reporting, featuring:
- text payloads
- json payloads
- severities
- logBook name
Monitoring
Write logs to custom metrics. NOTE: don't use too many different log names.
- custom names
- custom labels
- time-series based
Installation
npm install -s ambrite-mdd
Note: includes TypeScript types.
Usage
Logging
Installation
Import the library, Logger part:
import { Logger } from "ambrite-mdd"
Add new logBook (or reference an old one)
Setup a new log:
const log = new Logger(projectId, logName)
where projectId
is the name of the GCP project and logName
is the name of the log to write to.
Write a log
And finally to write a log:
log.debug("some string")
or generic:
log.level(payload)
LogLevel
where the level is one of:
log.log(...)
uses DEFAULTlog.debug(...)
uses DEBUGlog.notice(...)
uses NOTICElog.warn(...)
uses WARNINGlog.error(...)
uses ERRORlog.critical(...)
uses CRITICALlog.alert(...)
uses ALERTlog.emergency(...)
uses EMERGENCY
All these log levels are supported by stackdriver.
Payload
Currently, the payload you submit to the function is directly used in the stackdriver methods.
Specifically, you can use:
Text payloads; log.debug("Hello")
JSON payloads: log.debug({foo: {bar: 5}})
Retrieving logs
- Go to the GCP console.
- Go to the Logging subsection
- Switch to advanced filter
- start typing:
logName
(should auto-complete) - then type
=
and a drop down with all logBooks should appear, so you can just choose the log you need
Example:
logName="projects/testProject/logs/testLog"
or generically:
logName="projects/{projectID}/logs/{logName}"
Monitoring
Installation
Import the library, Monitoring part:
import { Monitoring } from "ambrite-mdd"
Create a new metric (or reference an old one)
const monitoring = projectId;const metric = Monitoring;
This creates a new metric on our project. Note that we could add more/other metrics with different tags.
Do NOT create too many metrics, but try to use labels to make differences.
This is all you need to configure a metric.
Writing to a metric
Now, in the code, you add a new point to the metric by:
monitoring;
This adds the point 123 to the metric
.
Note that this API allows us to simply define various metrics and ad-hoc choose the metric to write to.
Retrieving Metrics
- Go to your GCP Project
- Navigate to Monitoring
- Go to the metrics explorer
- Under the
global
resource, you should find your metric
Note: Might take a minute or two to feed through all the systems.