Skip to content

Commit

Permalink
Merge pull request #162 from numary/feature/num-357-ledger-openteleme…
Browse files Browse the repository at this point in the history
…try-config

feat: Make opentelemetry batching configurable
  • Loading branch information
gfyrag authored Mar 4, 2022
2 parents 2c3d5f7 + 087fcd1 commit 0b5a1c2
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 128 deletions.
1 change: 1 addition & 0 deletions cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewContainer(v *viper.Viper, options ...fx.Option) *fx.App {
options = append(options, opentelemetrytraces.TracesModule(opentelemetrytraces.ModuleConfig{
ServiceName: "ledger",
Version: Version,
Batch: v.GetBool(otelTracesBatchFlag),
Exporter: v.GetString(otelTracesExporterFlag),
JaegerConfig: func() *opentelemetrytraces.JaegerConfig {
if v.GetString(otelTracesExporterFlag) != opentelemetrytraces.JaegerExporter {
Expand Down
59 changes: 59 additions & 0 deletions cmd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ package cmd
import (
"context"
"github.com/numary/ledger/pkg/ledgertesting"
"github.com/numary/ledger/pkg/opentelemetry/opentelemetrytraces"
"github.com/numary/ledger/pkg/storage"
"github.com/numary/ledger/pkg/storage/sqlstorage"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/fx"
"os"
"reflect"
"runtime"
"strings"
"testing"
)

Expand Down Expand Up @@ -37,6 +43,59 @@ func TestContainers(t *testing.T) {
v.Set(otelTracesFlag, true)
v.Set(otelTracesExporterFlag, "stdout")
},
options: []fx.Option{
fx.Invoke(fx.Annotate(func(t *testing.T, exp trace.SpanExporter, options ...trace.TracerProviderOption) {
assert.Len(t, options, 2)
if os.Getenv("CI") == "true" { // runtime.FuncForPC does not returns same results locally or in the CI pipeline (probably related to inlining)
return
}
var (
foundWithResource bool
foundWithSyncer bool
)
for _, opt := range options {
if strings.Contains(runtime.FuncForPC(reflect.ValueOf(opt).Pointer()).Name(), "trace.WithSyncer") {
foundWithSyncer = true
}
if strings.Contains(runtime.FuncForPC(reflect.ValueOf(opt).Pointer()).Name(), "trace.WithResource") {
foundWithResource = true
}
}
assert.True(t, foundWithResource)
assert.True(t, foundWithSyncer)
}, fx.ParamTags(``, ``, opentelemetrytraces.TracerProviderOptionKey))),
},
},
{
name: "default-with-opentelemetry-traces-on-stdout-and-batch",
init: func(v *viper.Viper) {
v.Set(storageDriverFlag, sqlstorage.SQLite.String())
v.Set(otelTracesFlag, true)
v.Set(otelTracesExporterFlag, "stdout")
v.Set(otelTracesBatchFlag, true)
},
options: []fx.Option{
fx.Invoke(fx.Annotate(func(t *testing.T, exp trace.SpanExporter, options ...trace.TracerProviderOption) {
assert.Len(t, options, 2)
if os.Getenv("CI") == "true" { // runtime.FuncForPC does not returns same results locally or in the CI pipeline (probably related to inlining)
return
}
var (
foundWithResource bool
foundWithBatcher bool
)
for _, opt := range options {
if strings.Contains(runtime.FuncForPC(reflect.ValueOf(opt).Pointer()).Name(), "trace.WithBatch") {
foundWithBatcher = true
}
if strings.Contains(runtime.FuncForPC(reflect.ValueOf(opt).Pointer()).Name(), "trace.WithResource") {
foundWithResource = true
}
}
assert.True(t, foundWithResource)
assert.True(t, foundWithBatcher)
}, fx.ParamTags(``, ``, opentelemetrytraces.TracerProviderOptionKey))),
},
},
{
name: "default-with-opentelemetry-traces-on-otlp",
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
ledgersFlag = "ledgers"
serverHttpBasicAuthFlag = "server.http.basic_auth"
otelTracesFlag = "otel-traces"
otelTracesBatchFlag = "otel-traces-batch"
otelTracesExporterFlag = "otel-traces-exporter"
otelTracesExporterJaegerEndpointFlag = "otel-traces-exporter-jaeger-endpoint"
otelTracesExporterJaegerUserFlag = "otel-traces-exporter-jaeger-user"
Expand Down Expand Up @@ -103,6 +104,7 @@ func NewRootCommand() *cobra.Command {
root.PersistentFlags().StringSlice(ledgersFlag, []string{"quickstart"}, "Ledgers")
root.PersistentFlags().String(serverHttpBasicAuthFlag, "", "Http basic auth")
root.PersistentFlags().Bool(otelTracesFlag, false, "Enable OpenTelemetry traces support")
root.PersistentFlags().Bool(otelTracesBatchFlag, false, "Use OpenTelemetry batching")
root.PersistentFlags().String(otelTracesExporterFlag, "stdout", "OpenTelemetry traces exporter")
root.PersistentFlags().String(otelTracesExporterJaegerEndpointFlag, "", "OpenTelemetry traces Jaeger exporter endpoint")
root.PersistentFlags().String(otelTracesExporterJaegerUserFlag, "", "OpenTelemetry traces Jaeger exporter user")
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/XSAM/otelsql v0.10.0
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.7.7
github.com/google/go-cmp v0.5.6
github.com/google/go-cmp v0.5.7
github.com/huandu/go-sqlbuilder v1.13.0
github.com/jackc/pgconn v1.10.1
github.com/jackc/pgx/v4 v4.14.1
Expand All @@ -20,7 +20,7 @@ require (
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.28.0
go.opentelemetry.io/otel v1.3.0
go.opentelemetry.io/otel v1.4.1
go.opentelemetry.io/otel/exporters/jaeger v1.3.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.26.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.26.0
Expand All @@ -32,7 +32,7 @@ require (
go.opentelemetry.io/otel/metric v0.26.0
go.opentelemetry.io/otel/sdk v1.3.0
go.opentelemetry.io/otel/sdk/metric v0.26.0
go.opentelemetry.io/otel/trace v1.3.0
go.opentelemetry.io/otel/trace v1.4.1
go.uber.org/dig v1.13.0
go.uber.org/fx v1.16.0
)
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -824,6 +826,8 @@ go.opentelemetry.io/contrib/propagators/b3 v1.2.0/go.mod h1:kO8hNKCfa1YmQJ0lM7pz
go.opentelemetry.io/otel v1.2.0/go.mod h1:aT17Fk0Z1Nor9e0uisf98LrntPGMnk4frBO9+dkf69I=
go.opentelemetry.io/otel v1.3.0 h1:APxLf0eiBwLl+SOXiJJCVYzA1OOJNyAoV8C5RNRyy7Y=
go.opentelemetry.io/otel v1.3.0/go.mod h1:PWIKzi6JCp7sM0k9yZ43VX+T345uNbAkDKwHVjb2PTs=
go.opentelemetry.io/otel v1.4.1 h1:QbINgGDDcoQUoMJa2mMaWno49lja9sHwp6aoa2n3a4g=
go.opentelemetry.io/otel v1.4.1/go.mod h1:StM6F/0fSwpd8dKWDCdRr7uRvEPYdW0hBSlbdTiUde4=
go.opentelemetry.io/otel/exporters/jaeger v1.3.0 h1:HfydzioALdtcB26H5WHc4K47iTETJCdloL7VN579/L0=
go.opentelemetry.io/otel/exporters/jaeger v1.3.0/go.mod h1:KoYHi1BtkUPncGSRtCe/eh1ijsnePhSkxwzz07vU0Fc=
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.3.0 h1:R/OBkMoGgfy2fLhs2QhkCI1w4HLEQX92GCcJB6SSdNk=
Expand Down Expand Up @@ -855,6 +859,8 @@ go.opentelemetry.io/otel/sdk/metric v0.26.0/go.mod h1:2VIeK0kS1YvRLFg3J58ptZTXYp
go.opentelemetry.io/otel/trace v1.2.0/go.mod h1:N5FLswTubnxKxOJHM7XZC074qpeEdLy3CgAVsdMucK0=
go.opentelemetry.io/otel/trace v1.3.0 h1:doy8Hzb1RJ+I3yFhtDmwNc7tIyw1tNMOIsyPzp1NOGY=
go.opentelemetry.io/otel/trace v1.3.0/go.mod h1:c/VDhno8888bvQYmbYLqe41/Ldmr/KKunbvWM4/fEjk=
go.opentelemetry.io/otel/trace v1.4.1 h1:O+16qcdTrT7zxv2J6GejTPFinSwA++cYerC5iSiF8EQ=
go.opentelemetry.io/otel/trace v1.4.1/go.mod h1:iYEVbroFCNut9QkwEczV9vMRPHNKSSwYZjulEtsmhFc=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.opentelemetry.io/proto/otlp v0.11.0 h1:cLDgIBTf4lLOlztkhzAEdQsJ4Lj+i5Wc9k6Nn0K1VyU=
go.opentelemetry.io/proto/otlp v0.11.0/go.mod h1:QpEjXPrNQzrFDZgoTo49dgHR9RYRSrg3NAKnUGl9YpQ=
Expand Down
4 changes: 3 additions & 1 deletion pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/numary/ledger/pkg/ledger"
"github.com/numary/ledger/pkg/ledgertesting"
"github.com/numary/ledger/pkg/storage"
"github.com/pborman/uuid"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"go.uber.org/fx"
Expand Down Expand Up @@ -222,12 +223,13 @@ func TestCommitTransaction(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
withNewModule(t, fx.Invoke(func(api *API) {
id := uuid.New()
doRequest := func(tx core.TransactionData) *httptest.ResponseRecorder {
data, err := json.Marshal(tx)
assert.NoError(t, err)

rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/quickstart/transactions", bytes.NewBuffer(data))
req := httptest.NewRequest(http.MethodPost, "/"+id+"/transactions", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")

api.ServeHTTP(rec, req)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ledgertesting/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func PostgresServer() (*PGServer, error) {

resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "postgres",
Tag: "13-alpine",
Tag: "13.4-alpine",
Env: []string{
"POSTGRES_USER=root",
"POSTGRES_PASSWORD=root",
Expand Down
36 changes: 0 additions & 36 deletions pkg/opentelemetry/opentelemetrytraces/exporter.go

This file was deleted.

25 changes: 5 additions & 20 deletions pkg/opentelemetry/opentelemetrytraces/jaegerexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,12 @@ package opentelemetrytraces

import (
"go.opentelemetry.io/otel/exporters/jaeger"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/fx"
)

func LoadJaegerTracerProvider(resourceFactory *resourceFactory, options ...jaeger.CollectorEndpointOption) (*tracesdk.TracerProvider, error) {
r, err := resourceFactory.Make()
if err != nil {
return nil, err
}

exp, err := jaeger.New(jaeger.WithCollectorEndpoint(options...))
if err != nil {
return nil, err
}
tp := tracesdk.NewTracerProvider(
tracesdk.WithBatcher(exp),
tracesdk.WithResource(r),
)
return tp, nil
func LoadJaegerTracerExporter(options ...jaeger.CollectorEndpointOption) (*jaeger.Exporter, error) {
return jaeger.New(jaeger.WithCollectorEndpoint(options...))
}

const (
Expand All @@ -34,11 +21,9 @@ func ProvideJaegerTracerCollectorEndpoint(provider interface{}) fx.Option {
func JaegerTracerModule() fx.Option {
return fx.Options(
fx.Provide(
fx.Annotate(LoadJaegerTracerProvider, fx.ParamTags(
"",
fx.Annotate(LoadJaegerTracerExporter, fx.ParamTags(
JaegerCollectorEndpointGroupKey,
)),
), fx.As(new(trace.SpanExporter))),
),
traceSdkExportModule(),
)
}
16 changes: 0 additions & 16 deletions pkg/opentelemetry/opentelemetrytraces/noopexporter.go

This file was deleted.

22 changes: 4 additions & 18 deletions pkg/opentelemetry/opentelemetrytraces/otlpexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,12 @@ import (
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/fx"
)

func LoadOTLPTracerProvider(f *resourceFactory, client otlptrace.Client) (*tracesdk.TracerProvider, error) {
r, err := f.Make()
if err != nil {
return nil, err
}

exp, err := otlptrace.New(context.Background(), client)
if err != nil {
return nil, err
}
tp := tracesdk.NewTracerProvider(
tracesdk.WithBatcher(exp),
tracesdk.WithResource(r),
)
return tp, nil
func LoadOTLPTracerProvider(client otlptrace.Client) (*otlptrace.Exporter, error) {
return otlptrace.New(context.Background(), client)
}

func LoadOTLPTracerGRPCClient(options ...otlptracegrpc.Option) otlptrace.Client {
Expand All @@ -37,9 +24,8 @@ func LoadOTLPTracerHTTPClient(options ...otlptracehttp.Option) otlptrace.Client
func OTLPTracerModule() fx.Option {
return fx.Options(
fx.Provide(
LoadOTLPTracerProvider,
fx.Annotate(LoadOTLPTracerProvider, fx.As(new(trace.SpanExporter))),
),
traceSdkExportModule(),
)
}

Expand Down
5 changes: 0 additions & 5 deletions pkg/opentelemetry/opentelemetrytraces/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ func (f *resourceFactory) Make() (*resource.Resource, error) {
return resource.Merge(
resource.Default(),
resource.NewWithAttributes(semconv.SchemaURL, f.attributes...),
//resource.NewWithAttributes(
// semconv.SchemaURL,
// semconv.ServiceNameKey.String(serviceName),
// semconv.ServiceVersionKey.String(version),
//),
)
}

Expand Down
22 changes: 4 additions & 18 deletions pkg/opentelemetry/opentelemetrytraces/stdoutexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,21 @@ package opentelemetrytraces

import (
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/fx"
"os"
)

func LoadStdoutTracerProvider(f *resourceFactory) (*tracesdk.TracerProvider, error) {

r, err := f.Make()
if err != nil {
return nil, err
}
exp, err := stdouttrace.New(
func LoadStdoutTracerProvider() (*stdouttrace.Exporter, error) {
return stdouttrace.New(
stdouttrace.WithWriter(os.Stdout),
)
if err != nil {
return nil, err
}
tp := tracesdk.NewTracerProvider(
tracesdk.WithBatcher(exp),
tracesdk.WithResource(r),
)
return tp, nil
}

func StdoutTracerModule() fx.Option {
return fx.Options(
fx.Provide(
LoadStdoutTracerProvider,
fx.Annotate(LoadStdoutTracerProvider, fx.As(new(trace.SpanExporter))),
),
traceSdkExportModule(),
)
}
Loading

0 comments on commit 0b5a1c2

Please sign in to comment.