Skip to content

Commit

Permalink
Migrate and update example from examples repo (#494)
Browse files Browse the repository at this point in the history
* Migrate and update example from examples repo

* Simplify example, fix TraceContext, addSpan
  • Loading branch information
JamieDanielson committed Nov 15, 2021
1 parent 0042316 commit a94c56a
Show file tree
Hide file tree
Showing 7 changed files with 3,851 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/node-tracing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
51 changes: 51 additions & 0 deletions examples/node-tracing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# node-tracing example

## Overview

This example illustrates a simple comment-wall application using Honeycomb's [Beeline for Node](https://docs.honeycomb.io/getting-data-in/javascript/beeline-nodejs/).

It contains examples of:

- Baseline Beeline usage in an Express app
- Capture of custom metadata on Beeline-generated events
- Definition of custom spans to augment traces
- In-app tracing (across functions within the same service)

## Usage:

Find your Honeycomb API key at https://ui.honeycomb.io/account, then:

## Install and Setup

```bash
npm run setup # installs packages and local beeline as dependency
```

## Running the main application

Run our sample `wall` service with:

```bash
# Will run on port 3000
$ HONEYCOMB_API_KEY=abc123 npm start
```

### Interacting with your application

You may either use the web UI [`http://localhost:3000`](http://localhost:3000) to read and write messages:

| ![index](./images/index.png) | ![new message](./images/message.png) |
| :--------------------------: | :----------------------------------: |
| View contents of wall | Write new message on wall |

Or `curl` the contents of your wall directly:

```bash
# Fetch the contents of your wall
curl localhost:3000
```

```bash
# Write a new message to your wall
curl localhost:3000 -d "message=i'm #tracing with @honeycombio"
```
82 changes: 82 additions & 0 deletions examples/node-tracing/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* eslint-env node */
"use strict";
const beeline = require("honeycomb-beeline");

const HONEYCOMB_DATASET = process.env.HONEYCOMB_DATASET || "tracing-example";
const SERVICE_NAME = process.env.SERVICE_NAME || "wall";
const HONEYCOMB_API_KEY = process.env.HONEYCOMB_API_KEY || "abc123";

beeline({
writeKey: HONEYCOMB_API_KEY,
dataset: HONEYCOMB_DATASET,
serviceName: SERVICE_NAME,
httpTraceParserHook: beeline.w3c.httpTraceParserHook, // in case of mixed services
httpTracePropagationHook: beeline.w3c.httpTracePropagationHook, // in case of mixed services
});

const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const PORT = 3000;

const contents = ["first post"];

app.use(bodyParser.urlencoded({ extended: false, type: "*/*" }));

// = MIDDLEWARE ====================================================
// Wraps HTTP handlers to output evidence of HTTP calls + trace IDs
// to STDOUT for debugging.
// =================================================================
app.use((req, res, next) => {
let traceContext = beeline.honeycomb.marshalTraceContext(beeline.getTraceContext());
let { traceId } = beeline.honeycomb.unmarshalTraceContext(traceContext);
console.log(
"Handling request with:",
JSON.stringify({
method: req.method,
path: req.path,
traceId: traceId,
})
);

next();
});

// = HANDLER =======================================================
// Returns the current contents of our "wall".
// =================================================================
app.get("/", (req, res) => {
res.send(`${contents.join("<br />\n")}
<br /><br /><a href="/message">+ New Post</a>`);
});

// = HANDLER =======================================================
// Returns a simple HTML form for posting a new message to our wall.
// =================================================================
app.get("/message", (req, res) => {
res.send(`<form method="POST" action="/">
<input type="text" autofocus name="message" /><input type="submit" />
</form>`);
});

// = HANDLER =======================================================
// Processes a string from the client and saves the message contents.
// =================================================================
app.post("/", async (req, res) => {
if (typeof req.body.message !== "string") {
beeline.addTraceContext("error", "non-string body");
res.status(500).send("not a string body");
return;
}

const postSpan = beeline.startSpan({ name: "Posting a message" });
let body = req.body.message.trim();

contents.push(body);
beeline.addTraceContext({ message: body });
beeline.finishSpan(postSpan);

res.redirect("/");
});

app.listen(PORT, () => console.log(`'wall' service listening on port ${PORT}}!`));
Binary file added examples/node-tracing/images/index.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/node-tracing/images/message.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a94c56a

Please sign in to comment.