Skip to content

Commit 940d432

Browse files
committed
Add roundtripper
1 parent de96b3d commit 940d432

6 files changed

Lines changed: 45 additions & 33 deletions

File tree

api/cmd/experiments/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ func main() {
1212
if err != nil {
1313
log.Fatal("Error loading .env file")
1414
}
15-
_ = di.NewContainer("http-sms")
15+
_ = di.NewContainer("http-sms", "")
1616
}

api/cmd/replay/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func main() {
2020
log.Fatal("Error loading .env file")
2121
}
2222

23-
container := di.NewContainer("http-sms")
23+
container := di.NewContainer("http-sms", "")
2424
eventRepo := container.EventRepository()
2525
logger := container.Logger()
2626

api/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
cloud.google.com/go/cloudtasks v1.9.0
77
firebase.google.com/go v3.13.0+incompatible
88
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.10.2
9+
github.com/NdoleStudio/go-otelroundtripper v0.0.6
910
github.com/NdoleStudio/lemonsqueezy-go v0.0.5
1011
github.com/carlmjohnson/requests v0.22.3
1112
github.com/cheggaaa/pb/v3 v3.0.8
@@ -32,6 +33,7 @@ require (
3233
github.com/thedevsaddam/govalidator v1.9.10
3334
github.com/uptrace/uptrace-go v1.11.8
3435
go.opentelemetry.io/otel v1.11.2
36+
go.opentelemetry.io/otel/metric v0.34.0
3537
go.opentelemetry.io/otel/sdk v1.11.2
3638
go.opentelemetry.io/otel/trace v1.11.2
3739
google.golang.org/api v0.108.0
@@ -114,7 +116,6 @@ require (
114116
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.11.2 // indirect
115117
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.2 // indirect
116118
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.2 // indirect
117-
go.opentelemetry.io/otel/metric v0.34.0 // indirect
118119
go.opentelemetry.io/otel/sdk/metric v0.34.0 // indirect
119120
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
120121
go.uber.org/atomic v1.10.0 // indirect

api/go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0
7272
github.com/Masterminds/sprig v2.16.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
7373
github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60=
7474
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
75+
github.com/NdoleStudio/go-otelroundtripper v0.0.6 h1:y9nUERqMQI34tfLE59sH/dEAEcEgjKu35fr9Wnbp3uY=
76+
github.com/NdoleStudio/go-otelroundtripper v0.0.6/go.mod h1:gOnZQu6fktDW7PNvWX184+Gbgi0IRDZXVYdLkBwT7Zw=
7577
github.com/NdoleStudio/lemonsqueezy-go v0.0.5 h1:T4QlX1y3EDfypyoXcTYnjPy/JNyBldRyj1VzshfWp78=
7678
github.com/NdoleStudio/lemonsqueezy-go v0.0.5/go.mod h1:6w70wYe8SCLNGRspN4cgCiEXRIH5hsWixVd8jJWstqc=
7779
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
@@ -508,6 +510,7 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.11.2 h1:fqR1kli93643au1RKo0
508510
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.11.2/go.mod h1:5Qn6qvgkMsLDX+sYK64rHb1FPhpn0UtxF+ouX1uhyJE=
509511
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.2 h1:ERwKPn9Aer7Gxsc0+ZlutlH1bEEAUXAUhqm3Y45ABbk=
510512
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.2/go.mod h1:jWZUM2MWhWCJ9J9xVbRx7tzK1mXKpAlze4CeulycwVY=
513+
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.34.0 h1:O1E9/qhspQSz3O6/dSGLNBND2TO9mUaSvlhcKJMv278=
511514
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.2 h1:BhEVgvuE1NWLLuMLvC6sif791F45KFHi5GhOs1KunZU=
512515
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.2/go.mod h1:bx//lU66dPzNT+Y0hHA12ciKoMOH9iixEwCqC1OeQWQ=
513516
go.opentelemetry.io/otel/metric v0.34.0 h1:MCPoQxcg/26EuuJwpYN1mZTeCYAUGx8ABxfW07YkjP8=

api/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"github.com/NdoleStudio/httpsms/pkg/di"
88
)
99

10+
// Version is injected at runtime
11+
var Version string
12+
1013
// @title HTTP SMS API
1114
// @version 1.0
1215
// @description API to send SMS messages using android [SmsManager](https://developer.android.com/reference/android/telephony/SmsManager) via HTTP
@@ -29,6 +32,6 @@ func main() {
2932
di.LoadEnv()
3033
}
3134

32-
container := di.NewContainer("http-sms")
35+
container := di.NewContainer("http-sms", Version)
3336
container.Logger().Info(container.App().Listen(":8000").Error())
3437
}

api/pkg/di/container.go

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"strconv"
99
"time"
1010

11+
"github.com/NdoleStudio/go-otelroundtripper"
12+
"go.opentelemetry.io/otel/metric/global"
13+
1114
lemonsqueezy "github.com/NdoleStudio/lemonsqueezy-go"
1215
"github.com/hashicorp/go-retryablehttp"
1316

@@ -20,7 +23,6 @@ import (
2023
cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
2124

2225
"go.opentelemetry.io/otel"
23-
"go.opentelemetry.io/otel/attribute"
2426
"go.opentelemetry.io/otel/sdk/resource"
2527
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
2628

@@ -58,24 +60,26 @@ import (
5860
type Container struct {
5961
projectID string
6062
db *gorm.DB
63+
version string
6164
app *fiber.App
6265
eventDispatcher *services.EventDispatcher
6366
logger telemetry.Logger
6467
}
6568

6669
// NewContainer creates a new dependency injection container
67-
func NewContainer(projectID string) (container *Container) {
70+
func NewContainer(projectID string, version string) (container *Container) {
6871
// Set location to UTC
6972
now.DefaultConfig = &now.Config{
7073
TimeLocation: time.UTC,
7174
}
7275

7376
container = &Container{
7477
projectID: projectID,
78+
version: version,
7579
logger: logger(3).WithService(fmt.Sprintf("%T", container)),
7680
}
7781

78-
container.InitializeTraceProvider("0.0.1", os.Getenv("GCP_PROJECT_ID"))
82+
container.InitializeTraceProvider()
7983

8084
container.RegisterMessageListeners()
8185
container.RegisterMessageRoutes()
@@ -572,19 +576,31 @@ func (container *Container) HTTPClient(name string) *http.Client {
572576
container.logger.Debug(fmt.Sprintf("creating %s %T", name, http.DefaultClient))
573577
return &http.Client{
574578
Timeout: 60 * time.Second,
575-
Transport: container.RetryHTTPRoundTripper(),
579+
Transport: container.HTTPRoundTripper(name),
576580
}
577581
}
578582

579-
//func (container *Container) HTTPRoundTripper(name string) http.RoundTripper {
580-
// container.logger.Debug(fmt.Sprintf("Debug: initializing %s %T", name, http.DefaultTransport))
581-
// return otelroundtripper.New(
582-
// otelroundtripper.WithName(name),
583-
// otelroundtripper.WithParent(container.RetryHTTPRoundTripper()),
584-
// otelroundtripper.WithMeter(global.Meter(os.Getenv("NAMESPACE"))),
585-
// otelroundtripper.WithAttributes(initializers.InitializeOtelResources(container.Version, container.Namespace).Attributes()...),
586-
// )
587-
//}
583+
// HTTPRoundTripper creates an open telemetry http.RoundTripper
584+
func (container *Container) HTTPRoundTripper(name string) http.RoundTripper {
585+
container.logger.Debug(fmt.Sprintf("Debug: initializing %s %T", name, http.DefaultTransport))
586+
return otelroundtripper.New(
587+
otelroundtripper.WithName(name),
588+
otelroundtripper.WithParent(container.RetryHTTPRoundTripper()),
589+
otelroundtripper.WithMeter(global.Meter(container.projectID)),
590+
otelroundtripper.WithAttributes(container.OtelResources(container.version, container.projectID).Attributes()...),
591+
)
592+
}
593+
594+
// OtelResources generates default open telemetry resources
595+
func (container *Container) OtelResources(version string, namespace string) *resource.Resource {
596+
return resource.NewWithAttributes(
597+
semconv.SchemaURL,
598+
semconv.ServiceNameKey.String(namespace),
599+
semconv.ServiceVersionKey.String(version),
600+
semconv.ServiceInstanceIDKey.String(hostName()),
601+
semconv.DeploymentEnvironmentKey.String(os.Getenv("ENV")),
602+
)
603+
}
588604

589605
// RetryHTTPRoundTripper creates a retryable http.RoundTripper
590606
func (container *Container) RetryHTTPRoundTripper() http.RoundTripper {
@@ -961,11 +977,11 @@ func (container *Container) UserRepository() repositories.UserRepository {
961977
}
962978

963979
// InitializeTraceProvider initializes the open telemetry trace provider
964-
func (container *Container) InitializeTraceProvider(version string, namespace string) func() {
980+
func (container *Container) InitializeTraceProvider() func() {
965981
if isLocal() {
966-
return container.initializeUptraceProvider(version, namespace)
982+
return container.initializeUptraceProvider(container.version, container.projectID)
967983
}
968-
return container.initializeGoogleTraceProvider(version, namespace)
984+
return container.initializeGoogleTraceProvider(container.version, container.projectID)
969985
}
970986

971987
func (container *Container) initializeGoogleTraceProvider(version string, namespace string) func() {
@@ -979,7 +995,7 @@ func (container *Container) initializeGoogleTraceProvider(version string, namesp
979995
tp := trace.NewTracerProvider(
980996
trace.WithBatcher(exporter),
981997
trace.WithSampler(trace.AlwaysSample()),
982-
trace.WithResource(container.InitializeOtelResources(version, namespace)),
998+
trace.WithResource(container.OtelResources(version, namespace)),
983999
)
9841000

9851001
otel.SetTracerProvider(tp)
@@ -996,6 +1012,7 @@ func (container *Container) initializeUptraceProvider(version string, namespace
9961012
uptrace.WithDSN(os.Getenv("UPTRACE_DSN")),
9971013
uptrace.WithServiceName(namespace),
9981014
uptrace.WithServiceVersion(version),
1015+
uptrace.WithDeploymentEnvironment(os.Getenv("ENV")),
9991016
)
10001017

10011018
// Send buffered spans and free resources.
@@ -1007,18 +1024,6 @@ func (container *Container) initializeUptraceProvider(version string, namespace
10071024
}
10081025
}
10091026

1010-
// InitializeOtelResources initializes open telemetry resources
1011-
func (container *Container) InitializeOtelResources(version string, namespace string) *resource.Resource {
1012-
return resource.NewWithAttributes(
1013-
semconv.SchemaURL,
1014-
semconv.ServiceNameKey.String(namespace),
1015-
semconv.ServiceNamespaceKey.String(namespace),
1016-
semconv.ServiceVersionKey.String(version),
1017-
semconv.ServiceInstanceIDKey.String(hostName()),
1018-
attribute.String("service.environment", os.Getenv("ENV")),
1019-
)
1020-
}
1021-
10221027
func logger(skipFrameCount int) telemetry.Logger {
10231028
fields := map[string]string{
10241029
"pid": strconv.Itoa(os.Getpid()),

0 commit comments

Comments
 (0)