Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eminetto committed Apr 6, 2023
1 parent fa08169 commit 03116d8
Show file tree
Hide file tree
Showing 8 changed files with 572 additions and 13 deletions.
7 changes: 7 additions & 0 deletions auth/go.work.sum
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 1 addition & 0 deletions feedbacks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/go-chi/httplog v0.3.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/rs/zerolog v1.27.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions feedbacks/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/go-chi/httplog v0.3.0/go.mod h1:/pIXuFSrOdc5heKIJRA5Q2mW7cZCI2RySqFZN
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
Expand Down
2 changes: 2 additions & 0 deletions feedbacks/go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
13 changes: 12 additions & 1 deletion feedbacks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/eminetto/api-o11y/feedbacks/feedback"
"github.com/eminetto/api-o11y/feedbacks/feedback/mysql"
"github.com/eminetto/api-o11y/internal/middleware"
"github.com/eminetto/api-o11y/internal/telemetry"
"github.com/go-chi/chi/v5"
"github.com/go-chi/httplog"
_ "github.com/go-sql-driver/mysql"
Expand Down Expand Up @@ -52,8 +53,18 @@ func main() {
func storeFeedback(fService feedback.UseCase) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
oplog := httplog.LogEntry(r.Context())
ctx := r.Context()
otel, err := telemetry.New(ctx, "feedback")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
oplog.Error().Msg(err.Error())
return
}
defer otel.Shutdown(ctx)
ctx, span := otel.Start(ctx, "store")
defer span.End()
var f feedback.Feedback
err := json.NewDecoder(r.Body).Decode(&f)
err = json.NewDecoder(r.Body).Decode(&f)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
oplog.Error().Msg(err.Error())
Expand Down
25 changes: 21 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@ module github.com/eminetto/api-o11y
go 1.20

require (
github.com/go-chi/chi/v5 v5.0.8
github.com/go-sql-driver/mysql v1.7.0
github.com/google/uuid v1.3.0
github.com/gorilla/context v1.1.1
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.14.0
go.opentelemetry.io/otel/sdk v1.14.0
go.opentelemetry.io/otel/trace v1.14.0
)

require (
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
449 changes: 441 additions & 8 deletions go.sum

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions internal/telemetry/opentelemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package telemetry

import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
)

type Telemetry interface {
Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, Span)
Shutdown(ctx context.Context)
}

type Span interface {
trace.Span
}

type OTel struct {
provider *sdktrace.TracerProvider
tracer trace.Tracer
}

func New(ctx context.Context, serviceName string) (*OTel, error) {
var tp *sdktrace.TracerProvider
var err error
tp, err = createTraceProvider(ctx, serviceName)
if err != nil {
return nil, err
}
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
tracer := tp.Tracer(serviceName)

return &OTel{
provider: tp,
tracer: tracer,
}, nil
}

func (ot *OTel) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, Span) {
if len(opts) == 0 {
return ot.tracer.Start(ctx, name)
}
return ot.tracer.Start(ctx, name, opts[0])
}

func (ot *OTel) Shutdown(ctx context.Context) {
ot.provider.Shutdown(ctx)
}

func createResource(ctx context.Context, serviceName string) (*resource.Resource, error) {
return resource.New(ctx,
resource.WithAttributes(
semconv.ServiceNameKey.String(serviceName),
),
)
}

func createTraceProvider(ctx context.Context, serviceName string) (*sdktrace.TracerProvider, error) {
res, err := createResource(ctx, serviceName)
if err != nil {
return nil, err
}

client := otlptracehttp.NewClient()
exp, err := otlptrace.New(ctx, client)
if err != nil {
return nil, err
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exp),
sdktrace.WithResource(res),
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(serviceName),
semconv.DeploymentEnvironmentKey.String("prod"),
)),
)
return tp, nil
}

0 comments on commit 03116d8

Please sign in to comment.