Created
July 31, 2020 09:44
-
-
Save axw/0452ba180b5ca39d938771804a041fb2 to your computer and use it in GitHub Desktop.
Testing Jaeger service maps
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
package main | |
import ( | |
"context" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"time" | |
"github.com/opentracing/opentracing-go" | |
"github.com/opentracing/opentracing-go/ext" | |
"github.com/uber/jaeger-client-go/config" | |
"golang.org/x/sync/errgroup" | |
) | |
const ( | |
addrServiceA = "localhost:1234" | |
addrServiceB = "localhost:4321" | |
) | |
func newTracer(name string) (opentracing.Tracer, io.Closer, error) { | |
cfg, err := config.FromEnv() | |
if err != nil { | |
return nil, nil, err | |
} | |
cfg.ServiceName = name | |
cfg.Sampler.Type = "const" | |
cfg.Sampler.Param = 1 | |
return cfg.NewTracer() | |
} | |
func runServiceA(ctx context.Context) error { | |
tracer, closer, err := newTracer("jaeger-service-a") | |
if err != nil { | |
return err | |
} | |
defer closer.Close() | |
return http.ListenAndServe(addrServiceA, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header)) | |
serverSpan := tracer.StartSpan("server", ext.RPCServerOption(spanCtx)) | |
defer serverSpan.Finish() | |
clientSpan := tracer.StartSpan("client", opentracing.ChildOf(serverSpan.Context())) | |
defer clientSpan.Finish() | |
ext.SpanKindRPCClient.Set(clientSpan) | |
req, _ := http.NewRequest("GET", fmt.Sprintf("http://%s/", addrServiceB), nil) | |
ext.HTTPUrl.Set(clientSpan, req.URL.String()) | |
ext.HTTPMethod.Set(clientSpan, req.Method) | |
// Inject the client span context into the headers | |
tracer.Inject(clientSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header)) | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
defer resp.Body.Close() | |
ext.HTTPStatusCode.Set(clientSpan, uint16(resp.StatusCode)) | |
w.WriteHeader(resp.StatusCode) | |
io.Copy(w, resp.Body) | |
})) | |
} | |
func runServiceB(ctx context.Context) error { | |
tracer, closer, err := newTracer("jaeger-service-b") | |
if err != nil { | |
return err | |
} | |
defer closer.Close() | |
return http.ListenAndServe(addrServiceB, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header)) | |
serverSpan := tracer.StartSpan("server", ext.RPCServerOption(spanCtx)) | |
defer serverSpan.Finish() | |
dbSpan := tracer.StartSpan("querying database", opentracing.ChildOf(serverSpan.Context())) | |
ext.DBStatement.Set(dbSpan, "SELECT * FROM table") | |
ext.DBType.Set(dbSpan, "sql") | |
ext.PeerHostname.Set(dbSpan, "mysql-host") | |
ext.PeerPort.Set(dbSpan, 3306) | |
ext.PeerService.Set(dbSpan, "mysql") | |
time.Sleep(time.Millisecond) | |
dbSpan.Finish() | |
clientSpan := tracer.StartSpan("client", opentracing.ChildOf(serverSpan.Context())) | |
defer clientSpan.Finish() | |
ext.SpanKindRPCClient.Set(clientSpan) | |
req, _ := http.NewRequest("GET", "https://testing.invalid/", nil) | |
ext.HTTPUrl.Set(clientSpan, req.URL.String()) | |
ext.HTTPMethod.Set(clientSpan, req.Method) | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
defer resp.Body.Close() | |
ext.HTTPStatusCode.Set(clientSpan, uint16(resp.StatusCode)) | |
})) | |
} | |
func main() { | |
g, ctx := errgroup.WithContext(context.Background()) | |
g.Go(func() error { return runServiceA(ctx) }) | |
g.Go(func() error { return runServiceB(ctx) }) | |
if err := g.Wait(); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment