Skip to content

Commit

Permalink
otel
Browse files Browse the repository at this point in the history
  • Loading branch information
baely committed Aug 8, 2024
1 parent 73ff18d commit dcb1d95
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 14 deletions.
44 changes: 38 additions & 6 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"cloud.google.com/go/pubsub"
"github.com/go-chi/chi"
"github.com/google/uuid"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"

"github.com/baely/balance/internal/database"
"github.com/baely/balance/internal/integrations"
Expand All @@ -31,10 +33,10 @@ func newServer() *Server {

r := chi.NewRouter()

r.HandleFunc("/account-balance", RetrieveAccountBalance)
r.HandleFunc("/webhook", TriggerBalanceUpdate)
r.HandleFunc("/register", RegisterWebhook)
r.HandleFunc("/process", ProcessTransaction)
r.Handle("/account-balance", otelhttp.NewHandler(http.HandlerFunc(RetrieveAccountBalance), "RetrieveAccountBalance"))
r.Handle("/webhook", otelhttp.NewHandler(http.HandlerFunc(TriggerBalanceUpdate), "TriggerBalanceUpdate"))
r.Handle("/register", otelhttp.NewHandler(http.HandlerFunc(RegisterWebhook), "RegisterWebhook"))
r.Handle("/process", otelhttp.NewHandler(http.HandlerFunc(ProcessTransaction), "ProcessTransaction"))

return &Server{
http.Server{
Expand Down Expand Up @@ -92,13 +94,19 @@ func TriggerBalanceUpdate(w http.ResponseWriter, r *http.Request) {

guid, _ := uuid.NewRandom()

spanContext := trace.SpanContextFromContext(r.Context())
traceID := spanContext.TraceID().String()

msg := &pubsub.Message{
ID: guid.String(),
Data: body,
Attributes: map[string]string{
"trace-id": traceID,
},
}

// Push event to pubsub topic
ctx := context.Background()
ctx := r.Context()
res := topic.Publish(ctx, msg)
_, err = res.Get(ctx)
if err != nil {
Expand Down Expand Up @@ -140,6 +148,27 @@ func ProcessTransaction(w http.ResponseWriter, r *http.Request) {
return
}

span := trace.SpanFromContext(r.Context())

traceID, ok := r.Header["trace-id"]
if !ok {
fmt.Println("trace-id not found")
traceID = []string{uuid.NewString()}
}

spanCtx := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID([]byte(traceID[0])),
})

link := trace.Link{
SpanContext: spanCtx,
}

// link to span
span.AddLink(link)

ctx := trace.ContextWithSpanContext(context.Background(), spanCtx)

upClient := integrations.NewUpClient(os.Getenv("UP_TOKEN"))

// Retrieve transaction details
Expand Down Expand Up @@ -230,9 +259,12 @@ func ProcessTransaction(w http.ResponseWriter, r *http.Request) {

client := integrations.GetClient()
topic := client.Topic("transactions")
res := topic.Publish(context.Background(), &pubsub.Message{
res := topic.Publish(ctx, &pubsub.Message{
ID: uuid.NewString(),
Data: data,
Attributes: map[string]string{
"trace-id": traceID[0],
},
})
id, err := res.Get(context.Background())
if err != nil {
Expand Down
39 changes: 32 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,45 @@ require (
cloud.google.com/go/iam v1.1.8 // indirect
cloud.google.com/go/longrunning v0.5.7 // indirect
cloud.google.com/go/storage v1.42.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.4 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/honeycombio/otel-config-go v1.17.0 // indirect
github.com/lufia/plan9stats v0.0.0-20240513124658-fba389f38bae // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/sethvargo/go-envconfig v1.1.0 // indirect
github.com/shirou/gopsutil/v4 v4.24.6 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.52.0 // indirect
go.opentelemetry.io/contrib/instrumentation/host v0.53.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
go.opentelemetry.io/contrib/instrumentation/runtime v0.53.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.28.0 // indirect
go.opentelemetry.io/contrib/propagators/ot v1.28.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/sdk v1.28.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
Expand All @@ -42,8 +67,8 @@ require (
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240610135401-a8a62080eff3 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240610135401-a8a62080eff3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240610135401-a8a62080eff3 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
Loading

0 comments on commit dcb1d95

Please sign in to comment.