npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-custom-metrics

v0.12.1

Published

upload stackdriver custom metrics to google stackdriver

Downloads

3

Readme

google-custom-metrics

Build Status Coverage Status

Upload legacy Stackdriver custom metrics to Google Stackdriver.

If you don't know what this means, you probably don't need this package. If you do, this is a small library to send via HTTP arbitrary, application-defined metrics previously sent to Stackdriver to the new Stackdriver. Stats can be created on the fly, they just show up in the dashboard like they used to.

The code is designed to be easy to drop into any existing code: given body, a legacy stackdriver custom metrics upload object, and creds, the GCP service account credentials object, upload the custom metrics to Google Stackdriver with:

var gm = require('google-custom-metrics');

var body = getLegacyStackdriverUploadObject();
var creds = getGoogleServiceAccountCredentialsObject();

var hostInfo = gm.getPlatformDetails(creds);
var batches = gm.convertStackdriverUploadToGoogleStackdriver(hostInfo, body);
gm.uploadCustomMetricsToGoogleStackdriver(creds, batches, function(err, replies) {
    // replies is an array
})

Specifics

  • without platform details, stats are tagged with just instance_name and the resource labels will be empty
  • metrics can be created on the fly as custom metrics
  • convention is to also check process.env.GOOGLE_APPLICATIONS_CREDENTIALS for the google monitoring service account credentials

Api

convertStackdriverUploadToGoogleStackdriver( [options,] postBody )

Convert a legacy Stackdriver metrics upload into an array of Google Stackdriver uploads. Each upload the the complete POST request object that will be serialized and sent.

postBody is the object that would be serialized and sent to Stackdriver, in the form

{ timestamp: 123456789,
  proto_version: 1,
  data: [
    { name: 'stat-name',
      value: 1234.5,
      collected_at: 123456789,
      instance: 'i-0001' },
    ...
  ]

Options can be omitted, or be an object with the instance_name to identify samples, or the object returned from getPlatformDetails() that includes information used to tie a metric to the AWS or GCE instance that it was collected on. The default instance name is the hostname -s.

The response will be an array of Google Stackdriver POST bodies, something like (for 'global' type un-associated metrics, the resource.labels attributes would be empty, and resource.type would be 'global'):

{
  timeSeries: [
    {
      metric: {
        type: 'custom.googleapis.com/stat-name',
        labels: {
          instance_name: options.instance_name,
        }
      },
      resource: {
        type: 'aws_ec2_instance',
        labels: {
          instance_id: 'i-0123abcd',
          instance_name: 'my-aws-vm-host',
          aws_account: 'my-account-id',
          region: 'aws:us-east-1',
        }
      },
      points: [
        {
          interval: {
            endTime: '2017-12-01T12:34:56.789Z',
          },
          value: {
            doubleValue: 1234.5
          }
        }
      ]
    }
  ]
}

uploadCustomMetricsToGoogleStackdriver( creds, uploadBodies, callback(err, replies) )

Post each upload body to Google monitoring. creds are used to authenticate the requests; the callback is invoked with any error and the array of responses received from Google. Each response contains eg { statusCode: 200, body: { ... } }, the http status code and the reply body as received from Google. In case of error, body.error.message will often contain the error message.

getPlatformDetails( [creds] [,callerJson] )

Return information about the instance the code is running on. This information is used to associate the metrics to the host it was collected on. Creds are optional; if provided, the Google Cloud project_id will be added to the details included with the custom metrics. If creds are passed, an optional second parameter may be used to pass the json to parse for patform information. The json may be a string, a Buffer, or an already parsed object.

If the platform details are omitted from the converted upload, 'global' type custom metrics are uploaded with just the metrics.labels.instance_name set to the hostname.

Change Log

  • 0.12.1 - update getPlatformDetails docs, make work with node v0.10
  • 0.12.0 - work around non-js-compatible GCE platform json, omit the GCE zone path
  • 0.11.0 - have getPlatformDetails() accept a json bundle to parse for details
  • 0.10.1 - split out microreq
  • 0.9.0 - initial published version

Todo

  • make tests pass with node-v0.10