Skip to content

Commit 0ab57ab

Browse files
committed
[add] Sample distributed tracing of a sample Golang HTTP App that uses a Redis backend
1 parent ef56b18 commit 0ab57ab

12 files changed

+1212
-1
lines changed

.dockerignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.editorconfig
2+
.git
3+
AUTHORS.md
4+
CONTRIBUTING.md
5+
LICENSE
6+
NOTICE
7+
README.md
8+
arm/
9+
powerpc/
10+
mips/
11+
opentelemetry-go-http-sample

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@
1212
*.out
1313

1414
# Dependency directories (remove the comment below to include it)
15-
# vendor/
15+
vendor/
16+
17+
opentelemetry-go-http-sample
18+
19+
.idea
20+
bin/

Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM golang:1.15.6-buster AS build
2+
WORKDIR /src
3+
COPY . .
4+
RUN make build
5+
ENTRYPOINT ["/src/bin//opentelemetry-go-http-sample"]

Makefile

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Go parameters
2+
GOCMD=GO111MODULE=on go
3+
GOBUILD=$(GOCMD) build
4+
GOINSTALL=$(GOCMD) install
5+
GOCLEAN=$(GOCMD) clean
6+
GOTEST=$(GOCMD) test
7+
GOGET=$(GOCMD) get
8+
GOMOD=$(GOCMD) mod
9+
GOFMT=$(GOCMD) fmt
10+
11+
.PHONY: all test coverage
12+
all: test coverage build
13+
14+
build:
15+
$(GOBUILD) -o bin/opentelemetry-go-http-sample .
16+
17+
start-docker:
18+
@docker-compose -f docker-compose.yml up
19+
20+
stop-docker:
21+
@docker-compose -f docker-compose.yml down
22+
23+
24+
build-docker:
25+
@docker-compose -f docker-compose.yml build
26+
27+
checkfmt:
28+
@echo 'Checking gofmt';\
29+
bash -c "diff -u <(echo -n) <(gofmt -d .)";\
30+
EXIT_CODE=$$?;\
31+
if [ "$$EXIT_CODE" -ne 0 ]; then \
32+
echo '$@: Go files must be formatted with gofmt'; \
33+
fi && \
34+
exit $$EXIT_CODE
35+
36+
lint:
37+
$(GOGET) github.com/golangci/golangci-lint/cmd/golangci-lint
38+
golangci-lint run
39+
40+
get:
41+
$(GOGET) -t -v ./...
42+
43+
test: get
44+
$(GOFMT) ./...
45+
$(GOTEST) ./...
46+
47+
coverage: get test
48+
$(GOTEST) -race -coverprofile=coverage.txt -covermode=atomic .
49+
50+
fmt:
51+
$(GOFMT) ./...
52+
53+

Readme.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# OpenTelemetry Golang net/http + go-redis example
2+
3+
4+
This demo showcases OpenTelemetry distributed tracing of a sample
5+
Golang HTTP App that uses a Redis backend,
6+
using W3C TraceContext as the de-facto standard for trace propagation across process boundaries.
7+
8+
This setup relies on Jaeger to receive and visualize traces.
9+
10+
Make sure to check https://opentelemetry.io/docs/go/ for further reference and examples.
11+
12+
## 1 ) How to build and the sample app using docker
13+
```
14+
git clone github.com/filipecosta90/opentelemetry-go-http-sample
15+
cd opentelemetry-go-http-sample
16+
make build-docker
17+
make start-docker
18+
```
19+
20+
21+
## 2 ) Generating sample traces
22+
You should now jave your sample app listening on port :7777, redis on port :6379,
23+
and Jaeger on ports :14268, :16686.
24+
Let's generate some sample traces:
25+
26+
```
27+
$ curl -X POST -d "{\"Name\":\"John Doe\", \"Username\":\"johndoe\", \"About\":\"Redis Geek\"}" http://localhost:7777/author/1
28+
$ curl -X GET http://localhost:7777/author/1
29+
```
30+
31+
## 3) Go look for your traces!
32+
33+
The Jaeger visualization URL is at (notice the port number):
34+
35+
http://[hostname]:16686/search
36+
37+
Put in `sampleHTTPServer` value into the service name, and search for your recent traces!
38+
Click one, and you should see the following:
39+
40+
![sample jaeger output](./docs/sample-jaeger-output.jpg)

author.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// The Author struct represents the data in the JSON/JSONB column.
6+
// We can use struct tags to control how each field is encoded.
7+
type Author struct {
8+
Name string `json:"Name"`
9+
Username string `json:"Username"`
10+
About string `json:"About"`
11+
}
12+
13+
func authorToHsetCmdArgs(author Author) []interface{} {
14+
nonNullValues := make([]interface{}, 0)
15+
if author.Name != "" {
16+
nonNullValues = append(nonNullValues, "Name", author.Name)
17+
}
18+
if author.Username != "" {
19+
nonNullValues = append(nonNullValues, "Username", author.Username)
20+
}
21+
if author.About != "" {
22+
nonNullValues = append(nonNullValues, "About", author.About)
23+
}
24+
return nonNullValues
25+
}
26+
27+
func getRedisAuthorKey(authorId int) string {
28+
return fmt.Sprintf("author:%d", authorId)
29+
}
30+
31+
func UpdateAuthorFromRedisReply(author Author, redisReply map[string]string) (updatedAuthor Author, err error) {
32+
updatedAuthor = author
33+
if name, ok := redisReply["Name"]; ok {
34+
updatedAuthor.Name = name
35+
}
36+
if username, ok := redisReply["Username"]; ok {
37+
updatedAuthor.Username = username
38+
}
39+
if about, ok := redisReply["About"]; ok {
40+
updatedAuthor.About = about
41+
}
42+
return
43+
}

docker-compose.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: '3'
2+
services:
3+
redis:
4+
image: redis:6.0.5
5+
ports:
6+
- 6379:6379/tcp
7+
8+
server:
9+
build: .
10+
depends_on:
11+
- redis
12+
- jaeger
13+
ports:
14+
- 7777:7777
15+
environment:
16+
REDISENDPOINT: redis:6379
17+
JAEGERAGENTENDPOINT: http://jaeger:14268
18+
command: ['./opentelemetry-go-http-sample']
19+
20+
jaeger:
21+
image: jaegertracing/opentelemetry-all-in-one
22+
ports:
23+
- 14268:14268
24+
- 16686:16686

docs/sample-jaeger-output.jpg

120 KB
Loading

go.mod

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/filipecosta90/opentelemetry-go-http-sample
2+
3+
go 1.13
4+
5+
require (
6+
github.com/go-redis/redis/extra/redisotel v0.2.0
7+
github.com/go-redis/redis/v8 v8.4.2
8+
github.com/golangci/golangci-lint v1.33.0 // indirect
9+
github.com/gorilla/mux v1.8.0
10+
github.com/namsral/flag v1.7.4-pre
11+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.15.0
12+
go.opentelemetry.io/otel v0.15.0
13+
go.opentelemetry.io/otel/exporters/trace/jaeger v0.15.0
14+
go.opentelemetry.io/otel/sdk v0.15.0
15+
)

0 commit comments

Comments
 (0)