Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yuri Shkuro
committed
Nov 1, 2018
1 parent
7d43c03
commit cb77806
Showing
175 changed files
with
12,907 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 @@ | ||
.vscode/ |
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,19 @@ | ||
CREATE DATABASE IF NOT EXISTS chapter04; | ||
|
||
USE chapter04; | ||
|
||
CREATE TABLE IF NOT EXISTS chapter04.people ( | ||
name VARCHAR(100), | ||
title VARCHAR(10), | ||
description VARCHAR(100), | ||
PRIMARY KEY (name) | ||
); | ||
|
||
DELETE FROM chapter04.people; | ||
|
||
INSERT INTO chapter04.people VALUES ('Gru', 'Felonius', 'Where are the minions?'); | ||
INSERT INTO chapter04.people VALUES ('Nefario', 'Dr.', 'Why ... why are you so old?'); | ||
INSERT INTO chapter04.people VALUES ('Agnes', '', 'Your unicorn is so fluffy!'); | ||
INSERT INTO chapter04.people VALUES ('Edith', '', "Don't touch anything!"); | ||
INSERT INTO chapter04.people VALUES ('Vector', '', 'Committing crimes with both direction and magnitude!'); | ||
INSERT INTO chapter04.people VALUES ('Dave', 'Minion', 'Ngaaahaaa! Patalaki patalaku Big Boss!!'); |
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 @@ | ||
vendor/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,11 @@ | ||
[[constraint]] | ||
name = "github.com/uber/jaeger-client-go" | ||
version = "^2.14.0" | ||
|
||
[[constraint]] | ||
name = "github.com/go-sql-driver/mysql" | ||
version = "1.4.0" | ||
|
||
[[constraint]] | ||
name = "github.com/opentracing-contrib/go-stdlib" | ||
branch = "master" |
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,57 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/PacktPublishing/Distributed-Tracing/chapter-04/go/exercise1/people" | ||
) | ||
|
||
var repo *people.Repository | ||
|
||
func main() { | ||
repo = people.NewRepository() | ||
defer repo.Close() | ||
|
||
http.HandleFunc("/sayHello/", handleSayHello) | ||
|
||
log.Print("Listening on http://localhost:8080/") | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func handleSayHello(w http.ResponseWriter, r *http.Request) { | ||
name := strings.TrimPrefix(r.URL.Path, "/sayHello/") | ||
greeting, err := SayHello(name) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.Write([]byte(greeting)) | ||
} | ||
|
||
// SayHello creates a greeting for the named person. | ||
func SayHello(name string) (string, error) { | ||
person, err := repo.GetPerson(name) | ||
if err != nil { | ||
return "", err | ||
} | ||
return FormatGreeting( | ||
person.Name, | ||
person.Title, | ||
person.Description, | ||
), nil | ||
} | ||
|
||
// FormatGreeting combines information about a person into a greeting string. | ||
func FormatGreeting(name, title, description string) string { | ||
response := "Hello, " | ||
if title != "" { | ||
response += title + " " | ||
} | ||
response += name + "!" | ||
if description != "" { | ||
response += " " + description | ||
} | ||
return response | ||
} |
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,65 @@ | ||
package people | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
|
||
"github.com/PacktPublishing/Distributed-Tracing/chapter-04/go/lib/model" | ||
) | ||
|
||
const dburl = "root:mysqlpwd@tcp(127.0.0.1:3306)/chapter04" | ||
|
||
// Repository retrieves information about people. | ||
type Repository struct { | ||
db *sql.DB | ||
} | ||
|
||
// NewRepository creates a new Repository backed by MySQL database. | ||
func NewRepository() *Repository { | ||
db, err := sql.Open("mysql", dburl) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = db.Ping() | ||
if err != nil { | ||
log.Fatalf("Cannot ping the db: %v", err) | ||
} | ||
return &Repository{ | ||
db: db, | ||
} | ||
} | ||
|
||
// GetPerson tries to find the person in the database by name. | ||
// If not found, it still returns a Person object with only name | ||
// field populated. | ||
func (r *Repository) GetPerson(name string) (model.Person, error) { | ||
query := "select title, description from people where name = ?" | ||
rows, err := r.db.Query(query, name) | ||
if err != nil { | ||
return model.Person{}, err | ||
} | ||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var title, descr string | ||
err := rows.Scan(&title, &descr) | ||
if err != nil { | ||
return model.Person{}, err | ||
} | ||
return model.Person{ | ||
Name: name, | ||
Title: title, | ||
Description: descr, | ||
}, nil | ||
} | ||
return model.Person{ | ||
Name: name, | ||
}, nil | ||
} | ||
|
||
// Close calls close on the underlying db connection. | ||
func (r *Repository) Close() { | ||
r.db.Close() | ||
} |
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,80 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"strings" | ||
|
||
opentracing "github.com/opentracing/opentracing-go" | ||
otlog "github.com/opentracing/opentracing-go/log" | ||
|
||
"github.com/PacktPublishing/Distributed-Tracing/chapter-04/go/exercise2/people" | ||
"github.com/PacktPublishing/Distributed-Tracing/chapter-04/go/lib/tracing" | ||
) | ||
|
||
var repo *people.Repository | ||
var tracer opentracing.Tracer | ||
|
||
func main() { | ||
repo = people.NewRepository() | ||
defer repo.Close() | ||
|
||
tr, closer := tracing.Init("go-2-hello") | ||
defer closer.Close() | ||
tracer = tr | ||
|
||
http.HandleFunc("/sayHello/", handleSayHello) | ||
|
||
log.Print("Listening on http://localhost:8080/") | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func handleSayHello(w http.ResponseWriter, r *http.Request) { | ||
span := tracer.StartSpan("say-hello") | ||
defer span.Finish() | ||
|
||
name := strings.TrimPrefix(r.URL.Path, "/sayHello/") | ||
greeting, err := SayHello(name, span) | ||
if err != nil { | ||
span.SetTag("error", true) | ||
span.LogFields(otlog.Error(err)) | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
span.SetTag("response", greeting) | ||
w.Write([]byte(greeting)) | ||
} | ||
|
||
// SayHello creates a greeting for the named person. | ||
func SayHello(name string, span opentracing.Span) (string, error) { | ||
person, err := repo.GetPerson(name) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
span.LogKV( | ||
"name", person.Name, | ||
"title", person.Title, | ||
"description", person.Description, | ||
) | ||
|
||
return FormatGreeting( | ||
person.Name, | ||
person.Title, | ||
person.Description, | ||
), nil | ||
} | ||
|
||
// FormatGreeting combines information about a person into a greeting string. | ||
func FormatGreeting(name, title, description string) string { | ||
response := "Hello, " | ||
if title != "" { | ||
response += title + " " | ||
} | ||
response += name + "!" | ||
if description != "" { | ||
response += " " + description | ||
} | ||
return response | ||
} |
Oops, something went wrong.