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

bristol-stackdriver

v0.1.0

Published

[![npm](https://img.shields.io/npm/v/bristol-stackdriver.svg?maxAge=1000)](https://www.npmjs.com/package/bristol-stackdriver) [![dependency Status](https://img.shields.io/david/taxfyle/bristol-stackdriver.svg?maxAge=1000)](https://david-dm.org/taxfyle/bri

Downloads

1,488

Readme

Stackdriver target + formatter for Bristol

npm dependency Status devDependency Status Build Status Coveralls npm npm

This package provides a Bristol target + formatter that sends logs to Google Cloud Stackdriver.

Install

With npm:

npm install bristol-stackdriver

Or with yarn

yarn add bristol-stackdriver

Usage

Import the target and formatter and add them to your Bristol instance.

import Bristol from 'bristol'
import * as stackdriver from 'bristol-stackdriver'

const logger = new Bristol()
logger
  .addTarget(
    stackdriver.target({
      // Name your log if you want.
      logName: 'default',
      // How often should we flush logs to Stackdriver? (milliseconds)
      writeInterval: 500,
      // If you want to have the logs be grouped under a specific resource.
      resource: {
        type: 'global',
        labels: {}
      },
      // Key filename.
      keyFilename: 'path/to/keyfile.json',
      // Alternatively, pass in the credentials directly.
      credentials: require('path/to/keyfile.json'),
      // Google Cloud Project ID
      projectId: 'my-gcloud-project',
      // Service context for reporting errors to Stackdriver Error Reporting.
      // This is optional; if you don't care then don't include the `serviceContext`.
      serviceContext: {
        service: require('package.json').name,
        version: require('package.json').version
      }
    })
  )
  .withFormatter(stackdriver.formatter())

logger.info('Hello Stackdriver!', { shoop: 'da whoop' })
logger.error('Uh-oh', new Error('best check me out'))

API

bristol-stackdriver has a few tricks up its' sleeve. The examples only present relevant configuration changes for brevity.

Custom severities

Bristol allows you to customize your severities. The Stackdriver target needs to know how to map them to Stackdriver-specific severities.

You are not required to use/map them all, but those which are not mapped will be logged as DEFAULT.

logger.setSeverities(['panic', 'ohno', 'hmm', 'cool', 'whatever'])
logger.addTarget(
  stackdriver.target({
    severityMap: {
      panic: 'EMERGENCY',
      ohno: 'ERROR',
      hmm: 'WARNING',
      cool: 'INFO',
      whatever: 'DEBUG'
    }
  })
)

// Gets logged to Stackdriver as `EMERGENCY`.
logger.panic('i can see the light', new Error('good bye'))

Adding labels to log entries

Stackdriver supports labels. To add labels to your log messages, use the sd:labels field when logging.

logger.info('Starting flux capacitor', {
  'sd:labels': { phase: 'startup', module: 'flux-capacitor' }
})
logger.info('Starting reactor', {
  'sd:labels': { phase: 'startup', module: 'reactor' }
})

logger.error(
  'Reactor meltdown!!!1111ONEONEONE',
  new Error('core temp too high'),
  {
    'sd:labels': { module: 'reactor' }
  }
)

Reporting errors to Stackdriver Error Reporting

If you configure the target with a serviceContext, whenever an Error gets logged Stackdriver will automatically send it to Error Reporting.

logger.addTarget(
  stackdriver.target({
    serviceContext: {
      service: require('package.json').name,
      version: require('package.json').version
    }
  })
)

logger.error(
  'well looks like I dum diddely darn done goofed',
  new Error('ya done goofed')
)

Attaching user and httpContext for Error Reporting

Error Reporting can show request/response information about an error, as well as the ID of the user.

In order for the transport to submit that info, you need to attach it using sd:req, sd:res (or sd:httpContext) and sd:user.

http.createServer((req, res) => {
  res.statusCode = 400
  logger.error(new Error('Go away'), {
    'sd:req': req,
    'sd:res': res,
    'sd:user': req.user.id
  })
})

Alternatively, if you want to specify the HTTP context yourself.

http.createServer((req, res) => {
  res.statusCode = 400
  logger.error(new Error('Go away'), {
    'sd:httpContext': {
      method: req.method
      /* ... */
    }
  })
})

In fact, using sd:req and sd:res is the exact same as doing:

import { collectHttpContext } from 'bristol-stackdriver'

logger.error(new Error('Go away'), {
  'sd:httpContext': collectHttpContext(req, res)
})

Callbacks when flushing to Stackdriver (fails)

If you want to (for whatever reason) know when the target flushes to Stackdriver—or when it fails—you can attach some callbacks.

logger.addTarget(
  stackdriver.target({
    onFlushed: () => console.log('flushed to stackdriver'),
    onFlushError: err => console.error('flushing to stackdriver failed', err)
  })
)

Contributing

You will need a Google Cloud Project ID + keyfile to run the tests!

You can either add the project ID + keyfile (JSON stringified) to your own environment (GCLOUD_PROJECT=your-project-id GCLOUD_CREDENTIALS="{\"type\": \"...\"}"), or you can create an env.yaml in the repository root, and add the following:

test:
  GCLOUD_PROJECT: your-project-id
  GCLOUD_CREDENTIALS: >
    {
      "type": "...",
      <..remaining key...>
    }

Authors

Taxfyle Engineering — @taxfyle

Or, more specifically, this handsome devil. This is the guy to complain to if stuff isn't working.