-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate and update example from examples repo (#494)
* Migrate and update example from examples repo * Simplify example, fix TraceContext, addSpan
- Loading branch information
1 parent
0042316
commit a94c56a
Showing
7 changed files
with
3,851 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}!`)); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.