Created
August 19, 2019 07:40
-
-
Save axw/841bf72943a0bf348c0bc1ae29a9d2b3 to your computer and use it in GitHub Desktop.
Elastic APM Go - path generator
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 ( | |
"fmt" | |
"go.elastic.co/apm" | |
) | |
type node struct { | |
tracer *apm.Tracer | |
} | |
func makeNode(name, version, env string) *node { | |
t, err := apm.NewTracerOptions(apm.TracerOptions{ | |
ServiceName: name, | |
ServiceVersion: version, | |
ServiceEnvironment: env, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
return &node{tracer: t} | |
} | |
func flushAll(nodes ...*node) { | |
for _, n := range nodes { | |
n.tracer.Flush(nil) | |
} | |
} | |
func makePath(nodes ...*node) { | |
var traceContext apm.TraceContext | |
for i, n := range nodes { | |
tx := n.tracer.StartTransactionOptions( | |
fmt.Sprintf("GET /%s", n.tracer.Service.Name), "request", | |
apm.TransactionOptions{TraceContext: traceContext}, | |
) | |
defer tx.End() | |
if i < len(nodes)-1 { | |
name := fmt.Sprintf("GET %s", nodes[i+1].tracer.Service.Name) | |
span := tx.StartSpan(name, "external.http", nil) | |
defer span.End() | |
traceContext = span.TraceContext() | |
} | |
} | |
} | |
func main() { | |
/* | |
n1 n4 (n2 -> n3 -> n4) | |
\ / | |
---- n3 ---- | |
/ \ | |
n2 n5 (n1 -> n3 -> n5) | |
*/ | |
n1 := makeNode("n1", "1.0.0", "production") | |
n2 := makeNode("n2", "1.0.0", "production") | |
n3 := makeNode("n3", "1.0.0", "production") | |
n4 := makeNode("n4", "1.0.0", "production") | |
n5 := makeNode("n5", "1.0.0", "production") | |
defer flushAll(n1, n2, n3, n4, n5) | |
makePath(n2, n3, n4) | |
makePath(n1, n3, n5) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment