More changes to tracing.go redoing the whole bloody thing
MODIFY
tracing/go.mod
tracing/go.mod
@@ -9,9 +9,10 @@ require (
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.32.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.32.0
go.opentelemetry.io/otel v1.7.0
+ go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.7.0
go.opentelemetry.io/otel/sdk v1.7.0
go.opentelemetry.io/otel/trace v1.7.0
- golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
+ golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
gopkg.in/Clever/kayvee-go.v6 v6.27.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.3 // indirect

MODIFY
tracing/go.sum
tracing/go.sum
@@ -34,6 +34,8 @@ go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.32.0 h1:mac9BKR
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.32.0/go.mod h1:5eCOqeGphOyz6TsY3ZDNjE33SM/TFAK3RGuCL2naTgY=
go.opentelemetry.io/otel v1.7.0 h1:Z2lA3Tdch0iDcrhJXDIlC94XE+bxok1F9B+4Lz/lGsM=
go.opentelemetry.io/otel v1.7.0/go.mod h1:5BdUoMIz5WEs0vt0CUEMtSSaTSHBBVwrhnz7+nrD5xk=
+go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.7.0 h1:8hPcgCg0rUJiKE6VWahRvjgLUrNl7rW2hffUEPKXVEM=
+go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.7.0/go.mod h1:K4GDXPY6TjUiwbOh+DkKaEdCF8y+lvMoM6SeAPyfCCM=
go.opentelemetry.io/otel/metric v0.30.0 h1:Hs8eQZ8aQgs0U49diZoaS6Uaxw3+bBE3lcMUKBFIk3c=
go.opentelemetry.io/otel/metric v0.30.0/go.mod h1:/ShZ7+TS4dHzDFmfi1kSXMhMVubNoP0oIaBp70J6UXU=
go.opentelemetry.io/otel/sdk v1.7.0 h1:4OmStpcKVOfvDOgCt7UriAPtKolwIhxpnSNI/yK+1B0=
@@ -41,8 +43,8 @@ go.opentelemetry.io/otel/sdk v1.7.0/go.mod h1:uTEOTwaqIVuTGiJN7ii13Ibp75wJmYUDe3
go.opentelemetry.io/otel/trace v1.7.0 h1:O37Iogk1lEkMRXewVtZ1BBTVn5JEp8GrJvP92bJqC6o=
go.opentelemetry.io/otel/trace v1.7.0/go.mod h1:fzLSB9nqR2eXzxPXb2JW9IKE+ScyXA48yyE4TNvoHqU=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
-golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8=
+golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/Clever/kayvee-go.v6 v6.27.0 h1:UBbWrJ6l4ghaxpM5JZ5X23cCXWrqKAaO0RyCHctJ7b0=
gopkg.in/Clever/kayvee-go.v6 v6.27.0/go.mod h1:G0m6nBZj7Kdz+w2hiIaawmhXl5zp7E/K0ashol3Kb2A=

MODIFY
tracing/tracing.go
tracing/tracing.go
@@ -5,15 +5,22 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
+ "io"
+ "log"
"net/http"
"os"
"strconv"
- "time"
"github.com/davecgh/go-spew/spew"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
+ "go.opentelemetry.io/otel/attribute"
+ "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
+ "go.opentelemetry.io/otel/sdk/resource"
+ "go.opentelemetry.io/otel/trace"
+
+ semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
@@ -22,6 +29,11 @@ import (
"gopkg.in/Clever/kayvee-go.v6/logger"
)
+const (
+ instrumentationName = "github.com/Clever/wag/instrumentation"
+ instrumentationVersion = "v0.1.0"
+)
+
// propagator to use.
var propagator propagation.TextMapPropagator = propagation.TraceContext{} // traceparent header
@@ -29,6 +41,30 @@ var propagator propagation.TextMapPropagator = propagation.TraceContext{} // tra
// hasn't been updated when it is updated we can use otlp.DefaultCollectorPort
var defaultCollectorPort uint16 = 4317
+func newExporter(w io.Writer) (trace.SpanExporter, error) {
+ return stdouttrace.New(
+ stdouttrace.WithWriter(w),
+ // Use human-readable output.
+ stdouttrace.WithPrettyPrint(),
+ // Do not print timestamps for the demo.
+ // stdouttrace.WithoutTimestamps(),
+ )
+}
+
+// newResource returns a resource describing this application.
+func newResource() *resource.Resource {
+ r, _ := resource.Merge(
+ resource.Default(),
+ resource.NewWithAttributes(
+ semconv.SchemaURL,
+ semconv.ServiceNameKey.String("fib"),
+ semconv.ServiceVersionKey.String("v0.1.0"),
+ attribute.String("environment", "demo"),
+ ),
+ )
+ return r
+}
+
// SetupGlobalTraceProviderAndExporter sets up an exporter to export,
// as well as the opentelemetry global trace provider for trace generators to use.
// The exporter and provider are returned in order for the caller to defer shutdown.
@@ -39,6 +75,7 @@ func SetupGlobalTraceProviderAndExporter(ctx context.Context) (sdktrace.SpanExpo
// If we're running locally, then turn off sampling. Otherwise sample
// 1% or whatever TRACING_SAMPLING_PROBABILITY specifies.
+ DefaultCollectorHost := "localhost"
samplingProbability := 0.01
isLocal := os.Getenv("_IS_LOCAL") == "true"
if isLocal {
@@ -52,40 +89,60 @@ func SetupGlobalTraceProviderAndExporter(ctx context.Context) (sdktrace.SpanExpo
samplingProbability = samplingProbabilityFromEnv
}
- addr := fmt.Sprintf("%s:%d", otlp.DefaultCollectorHost, defaultCollectorPort)
+ addr := fmt.Sprintf("%s:%d", DefaultCollectorHost, defaultCollectorPort)
// Every 15 seconds we'll try to connect to opentelemetry collector at
// the default location of localhost:4317
// When running in production this is a sidecar, and when running
// locally this is a locally running opetelemetry-collector.
- driver := otlpgrpc.NewDriver(
- otlpgrpc.WithReconnectionPeriod(15*time.Second),
- otlpgrpc.WithEndpoint(addr),
- otlpgrpc.WithInsecure(),
- )
- fmt.Println("---driver---")
- spew.Dump(driver)
- exporter, err := otlp.NewExporter(
- ctx,
- driver,
- )
+ // driver := otlpgrpc.NewDriver(
+ // otlpgrpc.WithReconnectionPeriod(15*time.Second),
+ // otlpgrpc.WithEndpoint(addr),
+ // otlpgrpc.WithInsecure(),
+ // )
+ // fmt.Println("---driver---")
+ // spew.Dump(driver)
+ // exporter, err := otlp.NewExporter(
+ // ctx,
+ // driver,
+ // )
+ // if err != nil {
+ // return nil, nil, fmt.Errorf("error creating exporter: %v", err)
+ // }
+ // fmt.Println("---exporter---")
+ // spew.Dump(exporter)
+
+ l := log.New(os.Stdout, "", 0)
+ f, err := os.Create("traces.txt")
+ if err != nil {
+ l.Fatal(err)
+ }
+ defer f.Close()
+ exp, err := newExporter(f)
if err != nil {
- return nil, nil, fmt.Errorf("error creating exporter: %v", err)
+ l.Fatal(err)
}
- fmt.Println("---exporter---")
- spew.Dump(exporter)
- tp := newTracerProvider(exporter, samplingProbability)
+ tp := trace.NewTracerProvider(
+ samplingProbability,
+ trace.WithBatcher(exp),
+ trace.WithResource(newResource()),
+ )
fmt.Println("---trace provider---")
spew.Dump(tp)
+ defer func() {
+ if err := tp.Shutdown(context.Background()); err != nil {
+ l.Fatal(err)
+ }
+ }()
otel.SetTracerProvider(tp)
logger.FromContext(ctx).InfoD("starting-tracer", logger.M{
"address": addr,
"sampling-rate": samplingProbability,
})
- return exporter, tp, nil
+ return exp, tp, nil
}
// SetupGlobalTraceProviderAndExporterForTest is meant to be used in unit testing,
@@ -158,9 +215,11 @@ type roundTripper struct {
}
func (rt roundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
+
return otelhttp.NewTransport(
rt.baseTransport,
otelhttp.WithTracerProvider(otel.GetTracerProvider()),
+ // otelhttp.WithTracerProvider(tracer),
otelhttp.WithPropagators(propagator),
otelhttp.WithSpanNameFormatter(func(method string, r *http.Request) string {
v, ok := r.Context().Value(rt.spanNameCtxValue).(string)

More changes to tracing.go redoing the whole bloody thing