# fastify-gcloud-trace

> Google Cloud Trace API Connector for Fastify

Latest version **2.0.0** (published 2020-04-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-gcloud-trace
pnpm add fastify-gcloud-trace
yarn add fastify-gcloud-trace
bun add fastify-gcloud-trace
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2020-04-09 |
| First published | 2020-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 3 |
| Unpacked size | 13.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Makoto Kinoshita |
| Maintainers | mkinoshi |
| Keywords | plugin, stackdriver, google cloud, trace, fastify |

## Links

- npm: https://www.npmjs.com/package/fastify-gcloud-trace
- Repository: https://github.com/mkinoshi/fastify-gcloud-trace
- Homepage: https://github.com/mkinoshi/fastify-gcloud-trace#readme
- Issues: https://github.com/mkinoshi/fastify-gcloud-trace/issues
- npm.io page: https://npm.io/package/fastify-gcloud-trace

## Dependencies (3)

- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2
- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^1.6.1
- [@google-cloud/trace-agent](https://npm.io/package/@google-cloud/trace-agent.md) ^4.2.5

## Recent versions

- 2.0.0 (latest) — 2020-04-09
- 1.0.4 — 2020-04-06
- 1.0.3 — 2020-04-06
- 1.0.2 — 2020-04-05
- 1.0.1 — 2020-04-05
- 1.0.0 — 2020-04-04

## README

# fastify-gcloud-trace

Google Cloud Trace API Connector for Fastify

[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Build Status](https://travis-ci.com/mkinoshi/fastify-gcloud-trace.svg?branch=master)](https://travis-ci.org/fastify/fastify-plugin)

`fastify-gcloud-trace` is a plugin that connects your application with Stackdriver Trace API for [Fastify](https://github.com/fastify/fastify). It is build on top of [Stackdriver Trace](https://github.com/googleapis/cloud-trace-nodejs) package, so you can configure Stackdriver Trace API by passing options to `traceApiOptions`.

This plugin measures how long each event takes in one request, and generates trace results. Here is an example trace result that you can find on Google Cloud Console:

![IMAGE](https://user-images.githubusercontent.com/10353744/78500008-74323280-772a-11ea-8eaf-beb5b9cd9493.png)

## Install

```js
npm i fastify-gcloud-trace --save
```

or

```js
yarn add fastify-gcloud-trace
```

## Usage

Register the plugin with your project in the following way and that's it!

```js
const fastify = require('fastify')();

// Please register this plugin at the beginning unless there is a specific reason not to.
fastify.register(
  require('fastify-gcloud-trace')({
    traceApiOptions: {
      // Pass options for Stackdriver Trace API
      ignoreMethods: ['OPTIONS'],
    },
  }),
);

fastify.listen(3000, err => {
  if (err) throw err;
});
```

This plugin attaches a Trace object to each request, and the object is accessible as `rootSpan` in a `request.gtrace` object. Therefore, you can access the trace instance in your application code, and perform all functionalities defined in the Stackdriver Trace API. For example, you can create a childSpan in the following way. It is important to note that `rootSpan` is only defined when traceAPI generates a "traced" root span, so your application code has to handle the case where `rootSpan` is `null`. You can find the different types of span [here](https://googleapis.dev/nodejs/trace/latest/classes/UntracedRootSpanData.html).

```js
fastify.get('/foo', (req, reply) => {
  const span = req.gtrace.rootSpan
    ? req.gtrace.rootSpan.createChildSpan({name: 'Perform Heavy Calculation'})
    : null;
  // Do something
  if (span) {
    span.endSpan();
  }
  reply.send({hello: 'world'});
});
```

## Options

This is the list of available options.

- `traceApiOptions` - The options for the Trace API. The details are [here](https://googleapis.dev/nodejs/trace/latest/).
- `tracePluginOptions`:
  - `enabled` - If it is `true`, it generates a trace. The default value is `true`.
  - `nameOverride` - You can pass a function to overide a name for a trace. The function should take a [Request](https://www.fastify.io/docs/latest/Request/) as an argument.

## Limitations

It only supports StackDriver Trace API right now. If there is enough demand, I would also build support for [OpenCensus](https://opencensus.io/exporters/supported-exporters/node.js/) packages unless Google adds automatic support for Fasify.

## License Licensed under [MIT](./LICENSE).

---
_Source: https://npm.io/package/fastify-gcloud-trace · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
