From 175acc7589ee1ad2e3abc4dee35380a4a14c7863 Mon Sep 17 00:00:00 2001 From: Ian McEwen Date: Wed, 13 Apr 2022 16:17:53 -0700 Subject: [PATCH] Update to using otelutils --- go.mod | 4 +--- go.sum | 27 ++++++++++--------------- main.go | 61 +++++---------------------------------------------------- 3 files changed, 16 insertions(+), 76 deletions(-) diff --git a/go.mod b/go.mod index 40f2c39..7596222 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/DATA-DOG/go-sqlmock v1.3.0 github.com/cyverse-de/configurate v0.0.0-20171005230251-9b512d37328e github.com/cyverse-de/dbutil v1.0.1 + github.com/cyverse-de/go-mod/otelutils v0.0.2 github.com/cyverse-de/queries v1.0.1 github.com/gorilla/mux v1.8.0 github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb // indirect @@ -24,9 +25,6 @@ require ( github.com/spf13/viper v1.0.0 github.com/uptrace/opentelemetry-go-extra/otelsql v0.1.11 // indirect go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.30.0 - go.opentelemetry.io/otel v1.6.1 - go.opentelemetry.io/otel/exporters/jaeger v1.6.0 - go.opentelemetry.io/otel/sdk v1.6.0 gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect ) diff --git a/go.sum b/go.sum index 57f49d6..d50610e 100644 --- a/go.sum +++ b/go.sum @@ -7,16 +7,10 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/cyverse-de/configurate v0.0.0-20171005230251-9b512d37328e h1:h6okcwNgr8hsFfd0hFdl8usV0AtdCQca4CNfpllCqxQ= github.com/cyverse-de/configurate v0.0.0-20171005230251-9b512d37328e/go.mod h1:QMZ4G8bX5f0vKiH9+/2JqV687mN1byJ18tjZwIJIagI= -github.com/cyverse-de/dbutil v0.0.0-20160615220802-d6ccc51d67cd h1:cIUGNefDp8/c92Dk8fUbCUcnXGyt4gM71mQ5tuXfRLo= -github.com/cyverse-de/dbutil v0.0.0-20160615220802-d6ccc51d67cd/go.mod h1:ExgEsAIPqEEtubF/XyUlj8+4ReFsbyT9QChN6EsorN8= -github.com/cyverse-de/dbutil v1.0.0 h1:1bqPdGaboGdRSoFsqK3SqJcaGI8NqV7I9OcBUozRFNQ= -github.com/cyverse-de/dbutil v1.0.0/go.mod h1:DyVsTmbNNljQRUp9n35sZYfkj/Yn/UfYpEda0M+6GeU= github.com/cyverse-de/dbutil v1.0.1 h1:aCfckMIIJcPGZw9kJ5a1sJSji03/swkCsC7iwD5cX9A= github.com/cyverse-de/dbutil v1.0.1/go.mod h1:31IZYWBDxS/f4Gz3L/Nx17Q5HghARlB7VFdfjjv50M4= -github.com/cyverse-de/queries v0.0.0-20160412214440-1a09c989c99d h1:ctw5y3ycfdgwqGfX8hqsfcp2SZuzLACUO+d99KlbEdY= -github.com/cyverse-de/queries v0.0.0-20160412214440-1a09c989c99d/go.mod h1:YVclxEOFHTv0ZnVuJGiWh286GrxesamshwikcifVehA= -github.com/cyverse-de/queries v1.0.0 h1:PD882YqRpGKDnCpKMy5BQAzV3+WV9ML/IrDi0+U7rXE= -github.com/cyverse-de/queries v1.0.0/go.mod h1:AzLmCqSmI9iGqiWJi5tzsif2UHIAr3wrleDgQRhmDZk= +github.com/cyverse-de/go-mod/otelutils v0.0.2 h1:7O3jWQgf+PIIACSwtQM5SNtn6xTxxaLIUW6kjEKUpHM= +github.com/cyverse-de/go-mod/otelutils v0.0.2/go.mod h1:8P2jRGnvUsH4qtyVP0W6KiV5iSr102pLkzKFgkYJ4og= github.com/cyverse-de/queries v1.0.1 h1:xODyzr9I0SMlcMAqSLiXSHn5xqzOoAJXCwILlmOuRHE= github.com/cyverse-de/queries v1.0.1/go.mod h1:AzLmCqSmI9iGqiWJi5tzsif2UHIAr3wrleDgQRhmDZk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -50,14 +44,14 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ 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/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +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/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb h1:1OvvPvZkn/yCQ3xBcM8y4020wdkMXPHLB4+NfoGWh4U= github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/lib/pq v0.0.0-20180201184707-88edab080323 h1:Ou506ViB5uo2GloKFWIYi5hwRJn4AAOXuLVv8RMY9+4= -github.com/lib/pq v0.0.0-20180201184707-88edab080323/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk= github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/magiconair/properties v1.7.6 h1:U+1DqNen04MdEPgFiIwdOUiqZ8qPa37xgogX/sd3+54= @@ -81,6 +75,8 @@ github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs= github.com/pelletier/go-toml v1.1.0 h1:cmiOvKzEunMsAxyhXSzpL5Q1CRKpVv0KQsnAIcSEVYM= github.com/pelletier/go-toml v1.1.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.0.5-0.20180129181852-768a92a02685 h1:833faJBZ5DG4pN7wlypaNFWD9Ck7VpjhkJ1H42O/GmI= @@ -110,23 +106,20 @@ go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.30 go.opentelemetry.io/otel v1.4.0/go.mod h1:jeAqMFKy2uLIxCtKxoFj0FAL5zAPKQagc3+GtBWakzk= go.opentelemetry.io/otel v1.4.1/go.mod h1:StM6F/0fSwpd8dKWDCdRr7uRvEPYdW0hBSlbdTiUde4= go.opentelemetry.io/otel v1.5.0/go.mod h1:Jm/m+rNp/z0eqJc74H7LPwQ3G87qkU/AnnAydAjSAHk= -go.opentelemetry.io/otel v1.6.0 h1:YV6GkGe/Ag2PKsm4rjlqdSNs0w0A5ZzxeGkxhx1T+t4= go.opentelemetry.io/otel v1.6.0/go.mod h1:bfJD2DZVw0LBxghOTlgnlI0CV3hLDu9XF/QKOUXMTQQ= go.opentelemetry.io/otel v1.6.1 h1:6r1YrcTenBvYa1x491d0GGpTVBsNECmrc/K6b+zDeis= go.opentelemetry.io/otel v1.6.1/go.mod h1:blzUabWHkX6LJewxvadmzafgh/wnvBSDBdOuwkAtrWQ= -go.opentelemetry.io/otel/exporters/jaeger v1.6.0 h1:S6NKcNwBf9DJPeVIWYp2MEsgtnM2hXcMkDh27E7U5c0= -go.opentelemetry.io/otel/exporters/jaeger v1.6.0/go.mod h1:2ZcEphvH09jWp3zi/RFeAELQp1AV7R2AOP71Fjx9h2A= -go.opentelemetry.io/otel/internal/metric v0.27.0 h1:9dAVGAfFiiEq5NVB9FUJ5et+btbDQAUIJehJ+ikyryk= +go.opentelemetry.io/otel/exporters/jaeger v1.6.1 h1:7xuwXr3qUWq48Chyuq+VvomV3KjXZLd5seQwg83s/sU= +go.opentelemetry.io/otel/exporters/jaeger v1.6.1/go.mod h1:Cu6mKJ+LLTPuOBX830xM4wVKIsVpHSXa50uN7aAxraQ= go.opentelemetry.io/otel/internal/metric v0.27.0/go.mod h1:n1CVxRqKqYZtqyTh9U/onvKapPGv7y/rpyOTI+LFNzw= go.opentelemetry.io/otel/metric v0.27.0/go.mod h1:raXDJ7uP2/Jc0nVZWQjJtzoyssOYWu/+pjZqRzfvZ7g= go.opentelemetry.io/otel/metric v0.28.0 h1:o5YNh+jxACMODoAo1bI7OES0RUW4jAMae0Vgs2etWAQ= go.opentelemetry.io/otel/metric v0.28.0/go.mod h1:TrzsfQAmQaB1PDcdhBauLMk7nyyg9hm+GoQq/ekE9Iw= -go.opentelemetry.io/otel/sdk v1.6.0 h1:JoriAoiNENuxxIQApR1O0k2h1Md5QegZhbentcRJpWk= -go.opentelemetry.io/otel/sdk v1.6.0/go.mod h1:PjLRUfDsoPy0zl7yrDGSUqjj43tL7rEtFdCEiGlxXRM= +go.opentelemetry.io/otel/sdk v1.6.1 h1:ZmcNyMhcuAYIb/Nr6QhBPTMopMTbov/47wHt1gibkoY= +go.opentelemetry.io/otel/sdk v1.6.1/go.mod h1:IVYrddmFZ+eJqu2k38qD3WezFR2pymCzm8tdxyh3R4E= go.opentelemetry.io/otel/trace v1.4.0/go.mod h1:uc3eRsqDfWs9R7b92xbQbU42/eTNz4N+gLP8qJCi4aE= go.opentelemetry.io/otel/trace v1.4.1/go.mod h1:iYEVbroFCNut9QkwEczV9vMRPHNKSSwYZjulEtsmhFc= go.opentelemetry.io/otel/trace v1.5.0/go.mod h1:sq55kfhjXYr1zVSyexg0w1mpa03AYXR5eyTkB9NPPdE= -go.opentelemetry.io/otel/trace v1.6.0 h1:NDzPermp9ISkhxIaJXjBTi2O60xOSHDHP/EezjOL2wo= go.opentelemetry.io/otel/trace v1.6.0/go.mod h1:qs7BrU5cZ8dXQHBGxHMOxwME/27YH2qEp4/+tZLLwJE= go.opentelemetry.io/otel/trace v1.6.1 h1:f8c93l5tboBYZna1nWk0W9DYyMzJXDWdZcJZ0Kb400U= go.opentelemetry.io/otel/trace v1.6.1/go.mod h1:RkFRM1m0puWIq10oxImnGEduNBzxiN7TXluRBtE+5j0= diff --git a/main.go b/main.go index 55a3703..fb59ebe 100644 --- a/main.go +++ b/main.go @@ -6,20 +6,13 @@ import ( "flag" "net/http" "os" - "time" "github.com/cyverse-de/configurate" "github.com/cyverse-de/dbutil" + "github.com/cyverse-de/go-mod/otelutils" _ "github.com/lib/pq" log "github.com/sirupsen/logrus" "github.com/spf13/viper" - - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/exporters/jaeger" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/sdk/resource" - tracesdk "go.opentelemetry.io/otel/sdk/trace" - semconv "go.opentelemetry.io/otel/semconv/v1.7.0" ) const serviceName = "user-info" @@ -27,24 +20,6 @@ const serviceName = "user-info" // IplantSuffix is what is appended to a username in the database. const IplantSuffix = "@iplantcollaborative.org" -func jaegerTracerProvider(url string) (*tracesdk.TracerProvider, error) { - // Create the Jaeger exporter - exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url))) - if err != nil { - return nil, err - } - - tp := tracesdk.NewTracerProvider( - tracesdk.WithBatcher(exp), - tracesdk.WithResource(resource.NewWithAttributes( - semconv.SchemaURL, - semconv.ServiceNameKey.String(serviceName), - )), - ) - - return tp, nil -} - func main() { var ( showVersion = flag.Bool("version", false, "Print the version information") @@ -52,8 +27,6 @@ func main() { port = flag.String("port", "60000", "The port number to listen on") err error cfg *viper.Viper - - tracerProvider *tracesdk.TracerProvider ) flag.Parse() @@ -67,34 +40,10 @@ func main() { log.Fatal("--config must be set") } - otelTracesExporter := os.Getenv("OTEL_TRACES_EXPORTER") - if otelTracesExporter == "jaeger" { - jaegerEndpoint := os.Getenv("OTEL_EXPORTER_JAEGER_ENDPOINT") - if jaegerEndpoint == "" { - log.Warn("Jaeger set as OpenTelemetry trace exporter, but no Jaeger endpoint configured.") - } else { - tp, err := jaegerTracerProvider(jaegerEndpoint) - if err != nil { - log.Fatal(err) - } - tracerProvider = tp - otel.SetTracerProvider(tp) - otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) - } - } - - if tracerProvider != nil { - tracerCtx, cancel := context.WithCancel(context.Background()) - defer cancel() - - defer func(tracerContext context.Context) { - ctx, cancel := context.WithTimeout(tracerContext, time.Second*5) - defer cancel() - if err := tracerProvider.Shutdown(ctx); err != nil { - log.Fatal(err) - } - }(tracerCtx) - } + var tracerCtx, cancel = context.WithCancel(context.Background()) + defer cancel() + shutdown := otelutils.TracerProviderFromEnv(tracerCtx, serviceName, func(e error) { log.Fatal(e) }) + defer shutdown() if cfg, err = configurate.InitDefaults(*cfgPath, configurate.JobServicesDefaults); err != nil { log.Fatal(err.Error())