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

Update examples #184

Merged
merged 4 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
field_value: << parameters.goversion >>
- run:
name: Build example
command: go build examples/read_json_log.go
command: go build examples/json_reader/read_json_log.go

publish_github:
executor: github
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions examples/webapp/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:alpine

WORKDIR /app

COPY . ./

RUN go mod download
RUN go build -o ./shoutr

RUN apk add --update --no-cache ca-certificates
ENTRYPOINT ["./shoutr"]
62 changes: 62 additions & 0 deletions examples/webapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
## golang-webapp

Shoutr is an example Golang web application. You can register for accounts, sign
in and shout your opinions on the Internet. It has two tiers: A Golang web app
and a MySQL database.

## Install

Clone the repository.

Create database in MySQL.

```
$ mysql -uroot -e 'create database shoutr;'
```

## Run app

```
$ export HONEYCOMB_API_KEY=<apikey>
$ go run .
```

## (Alternatively) Run in Docker

The whole webapp can be run in Docker (Compose).

Set your [Honeycomb API key](https://ui.honeycomb.io/account) to
`HONEYCOMB_API_KEY`, or edit the `docker-compose.yml`. The `shoutr` database in
MySQL will be created automatically.

```
$ export HONEYCOMB_API_KEY=<apikey>
```

Then:

```
$ docker-compose build && docker-compose up
```

## Event Fields

| **Name** | **Description** | **Example Value** |
| --- | --- | --- |
| `flash.value` | Contents of the rendered flash message | `Your shout is too long!` |
| `request.content_length`| Length of the content (in bytes) of the sent HTTP request | `952` |
| `request.host` | Host the request was sent to | `localhost` |
| `request.method` | HTTP method | `POST` |
| `request.path` | Path of the request | `/shout` |
| `request.proto` | HTTP protocol version | `HTTP/1.1` |
| `request.remote_addr` | The IP and port that answered the request | `172.18.0.1:40484` |
| `request.user_agent`| User agent | `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36` |
| `response.status_code` | Status code written back to user | `200` |
| `runtime.memory_inuse` | Amount of memory in use (in bytes) by the whole process | `4,971,776` |
| `runtime.num_goroutines` | Number of goroutines in the process | `7` |
| `shout.content` | Content of the user's comment | `Hello world!` |
| `shout.content_length` | Length (in characters) of the user's comment | `80` |
| `system.hostname` | System hostname | `1ba87a98788c` |
| `timers.total_time_ms` | The total amount of time the request took to serve | `180` |
| `timers.mysql_insert_user_ms` | The time the `INSERT INTO users` query took | `50` |
| `user.id`| User ID | `2` |
68 changes: 68 additions & 0 deletions examples/webapp/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"fmt"
"log"
"os"
"time"

"github.com/jmoiron/sqlx"
)

var (
maxConnectRetries = 10
db *sqlx.DB
)

func init() {
var err error
dbUser := "root"
dbPass := ""
dbName := "shoutr"
dbHost := os.Getenv("DB_HOST")
if dbHost == "" {
dbHost = "localhost"
}

for i := 0; i < maxConnectRetries; i++ {
db, err = sqlx.Connect("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", dbUser, dbPass, dbHost, dbName))
if err != nil {
log.Print("Error connecting to database: ", err)
} else {
break
}
if i == maxConnectRetries-1 {
panic("Couldn't connect to DB")
}
time.Sleep(1 * time.Second)
}

log.Print("Bootstrapping database...")

_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS users (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(64) NOT NULL,
last_name VARCHAR(64) NOT NULL,
username VARCHAR(64) NOT NULL,
email VARCHAR(64),
PRIMARY KEY (id),
UNIQUE KEY (username)
);`)
if err != nil {
panic(err)
}

_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS shouts (
id INT NOT NULL AUTO_INCREMENT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
user_id INT,
content VARCHAR(140) NOT NULL,
PRIMARY KEY (id)
);
`)
if err != nil {
panic(err)
}
}
32 changes: 32 additions & 0 deletions examples/webapp/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '3'

services:
app:
build: .
ports:
- "8888:8888"
networks:
- main
depends_on:
- db
environment:
DB_HOST: db
HONEYCOMB_API_KEY:

db:
image: mysql
networks:
- main
volumes:
- example-golang-webapp:/var/lib/mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes" # value is arbitrary
MYSQL_DATABASE: "shoutr" # create DB shoutr automatically
ENV: "dev"

volumes:
example-golang-webapp:

networks:
main:
driver: bridge
25 changes: 25 additions & 0 deletions examples/webapp/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module github.com/honeycombio/libhoney-go/examples/webapp

go 1.17

require (
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible
github.com/go-sql-driver/mysql v1.6.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/schema v1.2.0
github.com/gorilla/sessions v1.2.1
github.com/honeycombio/libhoney-go v1.15.8
JamieDanielson marked this conversation as resolved.
Show resolved Hide resolved
github.com/jmoiron/sqlx v1.3.5
)

require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/facebookgo/limitgroup v0.0.0-20150612190941-6abd8d71ec01 // indirect
github.com/facebookgo/muster v0.0.0-20150708232844-fd3d7953fd52 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
gopkg.in/alexcesaro/statsd.v2 v2.0.0 // indirect
)
54 changes: 54 additions & 0 deletions examples/webapp/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
github.com/DataDog/zstd v1.5.0 h1:+K/VEwIAaPcHiMtQvpLD4lqW7f0Gk3xdYZmI1hD+CXo=
github.com/DataDog/zstd v1.5.0/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a h1:yDWHCSQ40h88yih2JAcL6Ls/kVkSE8GFACTGVnMPruw=
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a/go.mod h1:7Ga40egUymuWXxAe151lTNnCv97MddSOVsjpPPkityA=
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c h1:8ISkoahWXwZR41ois5lSJBSVw4D0OV19Ht/JSTzvSv0=
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64=
github.com/facebookgo/limitgroup v0.0.0-20150612190941-6abd8d71ec01 h1:IeaD1VDVBPlx3viJT9Md8if8IxxJnO+x0JCGb054heg=
github.com/facebookgo/limitgroup v0.0.0-20150612190941-6abd8d71ec01/go.mod h1:ypD5nozFk9vcGw1ATYefw6jHe/jZP++Z15/+VTMcWhc=
github.com/facebookgo/muster v0.0.0-20150708232844-fd3d7953fd52 h1:a4DFiKFJiDRGFD1qIcqGLX/WlUMD9dyLSLDt+9QZgt8=
github.com/facebookgo/muster v0.0.0-20150708232844-fd3d7953fd52/go.mod h1:yIquW87NGRw1FU5p5lEkpnt/QxoH5uPAOUlOVkAUuMg=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg=
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpmbhCOZJ293Lz68O7PYrF2EzeiFMwCLk=
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc=
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/honeycombio/libhoney-go v1.15.8 h1:TECEltZ48K6J4NG1JVYqmi0vCJNnHYooFor83fgKesA=
github.com/honeycombio/libhoney-go v1.15.8/go.mod h1:+tnL2etFnJmVx30yqmoUkVyQjp7uRJw0a2QGu48lSyY=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
gopkg.in/alexcesaro/statsd.v2 v2.0.0 h1:FXkZSCZIH17vLCO5sO2UucTHsH9pc+17F6pl3JVCwMc=
gopkg.in/alexcesaro/statsd.v2 v2.0.0/go.mod h1:i0ubccKGzBVNBpdGV5MocxyA/XlLUJzA7SLonnE4drU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading