Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate and update example from examples repo #494

Merged
merged 5 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.