From 69b6c3e8c4b7b1f51ad6515b5b140371bd83148e Mon Sep 17 00:00:00 2001 From: SammyOina Date: Thu, 6 Jul 2023 09:58:41 +0300 Subject: [PATCH 1/6] add traces to bootstrap endpoints Signed-off-by: SammyOina --- bootstrap/api/transport.go | 21 +++++++++++---------- cmd/bootstrap/main.go | 12 ++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/bootstrap/api/transport.go b/bootstrap/api/transport.go index 66c52b0668..5fa63aaf57 100644 --- a/bootstrap/api/transport.go +++ b/bootstrap/api/transport.go @@ -18,6 +18,7 @@ import ( mflog "github.com/mainflux/mainflux/logger" "github.com/mainflux/mainflux/pkg/errors" "github.com/prometheus/client_golang/prometheus/promhttp" + "go.opentelemetry.io/contrib/instrumentation/github.com/go-kit/kit/otelkit" ) const ( @@ -41,61 +42,61 @@ func MakeHandler(svc bootstrap.Service, reader bootstrap.ConfigReader, logger mf r := bone.New() r.Post("/things/configs", kithttp.NewServer( - addEndpoint(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("add"), otelkit.WithTracerProvider(tp))(addEndpoint(svc)), decodeAddRequest, encodeResponse, opts...)) r.Get("/things/configs/:configID", kithttp.NewServer( - viewEndpoint(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("view"), otelkit.WithTracerProvider(tp))(viewEndpoint(svc)), decodeEntityRequest, encodeResponse, opts...)) r.Put("/things/configs/:configID", kithttp.NewServer( - updateEndpoint(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("update"), otelkit.WithTracerProvider(tp))(updateEndpoint(svc)), decodeUpdateRequest, encodeResponse, opts...)) r.Patch("/things/configs/certs/:certID", kithttp.NewServer( - updateCertEndpoint(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("update_cert"), otelkit.WithTracerProvider(tp))(updateCertEndpoint(svc)), decodeUpdateCertRequest, encodeResponse, opts...)) r.Put("/things/configs/connections/:connID", kithttp.NewServer( - updateConnEndpoint(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("update_connections"), otelkit.WithTracerProvider(tp))(updateConnEndpoint(svc)), decodeUpdateConnRequest, encodeResponse, opts...)) r.Get("/things/configs", kithttp.NewServer( - listEndpoint(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("list"), otelkit.WithTracerProvider(tp))(listEndpoint(svc)), decodeListRequest, encodeResponse, opts...)) r.Get("/things/bootstrap/:externalID", kithttp.NewServer( - bootstrapEndpoint(svc, reader, false), + otelkit.EndpointMiddleware(otelkit.WithOperation("bootstrap"), otelkit.WithTracerProvider(tp))(bootstrapEndpoint(svc, reader, false)), decodeBootstrapRequest, encodeResponse, opts...)) r.Get("/things/bootstrap/secure/:externalID", kithttp.NewServer( - bootstrapEndpoint(svc, reader, true), + otelkit.EndpointMiddleware(otelkit.WithOperation("bootstrap_secure"), otelkit.WithTracerProvider(tp))(bootstrapEndpoint(svc, reader, true)), decodeBootstrapRequest, encodeSecureRes, opts...)) r.Put("/things/state/:thingID", kithttp.NewServer( - stateEndpoint(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("state"), otelkit.WithTracerProvider(tp))(stateEndpoint(svc)), decodeStateRequest, encodeResponse, opts...)) r.Delete("/things/configs/:configID", kithttp.NewServer( - removeEndpoint(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("remove"), otelkit.WithTracerProvider(tp))(removeEndpoint(svc)), decodeEntityRequest, encodeResponse, opts...)) diff --git a/cmd/bootstrap/main.go b/cmd/bootstrap/main.go index 5607a0bf2d..2a1c4bbcdc 100644 --- a/cmd/bootstrap/main.go +++ b/cmd/bootstrap/main.go @@ -37,6 +37,7 @@ import ( "github.com/jmoiron/sqlx" "github.com/mainflux/mainflux/bootstrap" "github.com/mainflux/mainflux/bootstrap/api" + jaegerClient "github.com/mainflux/mainflux/internal/clients/jaeger" ) const ( @@ -123,6 +124,17 @@ func main() { if err := env.Parse(&httpServerConfig, env.Options{Prefix: envPrefixHttp, AltPrefix: envPrefix}); err != nil { logger.Fatal(fmt.Sprintf("failed to load %s HTTP server configuration : %s", svcName, err)) } + + tp, err := jaegerClient.NewProvider(svcName, cfg.JaegerURL) + if err != nil { + logger.Error(fmt.Sprintf("Failed to init Jaeger: %s", err)) + } + defer func() { + if err := tp.Shutdown(context.Background()); err != nil { + logger.Error(fmt.Sprintf("Error shutting down tracer provider: %v", err)) + } + }() + hs := httpserver.New(ctx, cancel, svcName, httpServerConfig, api.MakeHandler(svc, bootstrap.NewConfigReader([]byte(cfg.EncKey)), logger, instanceID), logger) if cfg.SendTelemetry { From 03c5a109f8e402822a86d6122e43325ca6bc51f6 Mon Sep 17 00:00:00 2001 From: SammyOina Date: Thu, 6 Jul 2023 11:07:42 +0300 Subject: [PATCH 2/6] add tracing to certs endpoints Signed-off-by: SammyOina --- bootstrap/api/transport.go | 20 ++++++++++---------- certs/api/transport.go | 9 +++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/bootstrap/api/transport.go b/bootstrap/api/transport.go index 5fa63aaf57..1de61e809c 100644 --- a/bootstrap/api/transport.go +++ b/bootstrap/api/transport.go @@ -42,61 +42,61 @@ func MakeHandler(svc bootstrap.Service, reader bootstrap.ConfigReader, logger mf r := bone.New() r.Post("/things/configs", kithttp.NewServer( - otelkit.EndpointMiddleware(otelkit.WithOperation("add"), otelkit.WithTracerProvider(tp))(addEndpoint(svc)), + otelkit.EndpointMiddleware(otelkit.WithOperation("add"))(addEndpoint(svc)), decodeAddRequest, encodeResponse, opts...)) r.Get("/things/configs/:configID", kithttp.NewServer( - otelkit.EndpointMiddleware(otelkit.WithOperation("view"), otelkit.WithTracerProvider(tp))(viewEndpoint(svc)), + otelkit.EndpointMiddleware(otelkit.WithOperation("view"))(viewEndpoint(svc)), decodeEntityRequest, encodeResponse, opts...)) r.Put("/things/configs/:configID", kithttp.NewServer( - otelkit.EndpointMiddleware(otelkit.WithOperation("update"), otelkit.WithTracerProvider(tp))(updateEndpoint(svc)), + otelkit.EndpointMiddleware(otelkit.WithOperation("update"))(updateEndpoint(svc)), decodeUpdateRequest, encodeResponse, opts...)) r.Patch("/things/configs/certs/:certID", kithttp.NewServer( - otelkit.EndpointMiddleware(otelkit.WithOperation("update_cert"), otelkit.WithTracerProvider(tp))(updateCertEndpoint(svc)), + otelkit.EndpointMiddleware(otelkit.WithOperation("update_cert"))(updateCertEndpoint(svc)), decodeUpdateCertRequest, encodeResponse, opts...)) r.Put("/things/configs/connections/:connID", kithttp.NewServer( - otelkit.EndpointMiddleware(otelkit.WithOperation("update_connections"), otelkit.WithTracerProvider(tp))(updateConnEndpoint(svc)), + otelkit.EndpointMiddleware(otelkit.WithOperation("update_connections"))(updateConnEndpoint(svc)), decodeUpdateConnRequest, encodeResponse, opts...)) r.Get("/things/configs", kithttp.NewServer( - otelkit.EndpointMiddleware(otelkit.WithOperation("list"), otelkit.WithTracerProvider(tp))(listEndpoint(svc)), + otelkit.EndpointMiddleware(otelkit.WithOperation("list"))(listEndpoint(svc)), decodeListRequest, encodeResponse, opts...)) r.Get("/things/bootstrap/:externalID", kithttp.NewServer( - otelkit.EndpointMiddleware(otelkit.WithOperation("bootstrap"), otelkit.WithTracerProvider(tp))(bootstrapEndpoint(svc, reader, false)), + otelkit.EndpointMiddleware(otelkit.WithOperation("bootstrap"))(bootstrapEndpoint(svc, reader, false)), decodeBootstrapRequest, encodeResponse, opts...)) r.Get("/things/bootstrap/secure/:externalID", kithttp.NewServer( - otelkit.EndpointMiddleware(otelkit.WithOperation("bootstrap_secure"), otelkit.WithTracerProvider(tp))(bootstrapEndpoint(svc, reader, true)), + otelkit.EndpointMiddleware(otelkit.WithOperation("bootstrap_secure"))(bootstrapEndpoint(svc, reader, true)), decodeBootstrapRequest, encodeSecureRes, opts...)) r.Put("/things/state/:thingID", kithttp.NewServer( - otelkit.EndpointMiddleware(otelkit.WithOperation("state"), otelkit.WithTracerProvider(tp))(stateEndpoint(svc)), + otelkit.EndpointMiddleware(otelkit.WithOperation("state"))(stateEndpoint(svc)), decodeStateRequest, encodeResponse, opts...)) r.Delete("/things/configs/:configID", kithttp.NewServer( - otelkit.EndpointMiddleware(otelkit.WithOperation("remove"), otelkit.WithTracerProvider(tp))(removeEndpoint(svc)), + otelkit.EndpointMiddleware(otelkit.WithOperation("remove"))(removeEndpoint(svc)), decodeEntityRequest, encodeResponse, opts...)) diff --git a/certs/api/transport.go b/certs/api/transport.go index 79deecb6f5..02f2d03d48 100644 --- a/certs/api/transport.go +++ b/certs/api/transport.go @@ -16,6 +16,7 @@ import ( "github.com/mainflux/mainflux/logger" "github.com/mainflux/mainflux/pkg/errors" "github.com/prometheus/client_golang/prometheus/promhttp" + "go.opentelemetry.io/contrib/instrumentation/github.com/go-kit/kit/otelkit" ) const ( @@ -35,28 +36,28 @@ func MakeHandler(svc certs.Service, logger logger.Logger, instanceID string) htt r := bone.New() r.Post("/certs", kithttp.NewServer( - issueCert(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("issue"))(issueCert(svc)), decodeCerts, encodeResponse, opts..., )) r.Get("/certs/:certID", kithttp.NewServer( - viewCert(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("view"))(viewCert(svc)), decodeViewCert, encodeResponse, opts..., )) r.Delete("/certs/:certID", kithttp.NewServer( - revokeCert(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("revoke"))(revokeCert(svc)), decodeRevokeCerts, encodeResponse, opts..., )) r.Get("/serials/:thingID", kithttp.NewServer( - listSerials(svc), + otelkit.EndpointMiddleware(otelkit.WithOperation("list_serials"))(listSerials(svc)), decodeListCerts, encodeResponse, opts..., From 9938b9adfc41425b2191dab4696f5bacd502be5a Mon Sep 17 00:00:00 2001 From: SammyOina Date: Thu, 6 Jul 2023 11:11:23 +0300 Subject: [PATCH 3/6] remove tracing provider Signed-off-by: SammyOina --- cmd/bootstrap/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/bootstrap/main.go b/cmd/bootstrap/main.go index 2a1c4bbcdc..5a54425cd8 100644 --- a/cmd/bootstrap/main.go +++ b/cmd/bootstrap/main.go @@ -37,7 +37,6 @@ import ( "github.com/jmoiron/sqlx" "github.com/mainflux/mainflux/bootstrap" "github.com/mainflux/mainflux/bootstrap/api" - jaegerClient "github.com/mainflux/mainflux/internal/clients/jaeger" ) const ( From c5168bd9ba23b573a289a0c9e6640e47012cb019 Mon Sep 17 00:00:00 2001 From: SammyOina Date: Thu, 6 Jul 2023 11:54:48 +0300 Subject: [PATCH 4/6] add tracing provider Signed-off-by: SammyOina --- cmd/certs/main.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cmd/certs/main.go b/cmd/certs/main.go index 511adb0b81..dc31365ae4 100644 --- a/cmd/certs/main.go +++ b/cmd/certs/main.go @@ -18,6 +18,7 @@ import ( vault "github.com/mainflux/mainflux/certs/pki" certsPg "github.com/mainflux/mainflux/certs/postgres" "github.com/mainflux/mainflux/internal" + jaegerClient "github.com/mainflux/mainflux/internal/clients/jaeger" "github.com/mainflux/mainflux/internal/env" "github.com/mainflux/mainflux/internal/server" httpserver "github.com/mainflux/mainflux/internal/server/http" @@ -112,6 +113,15 @@ func main() { if err := env.Parse(&httpServerConfig, env.Options{Prefix: envPrefixHttp, AltPrefix: envPrefix}); err != nil { logger.Fatal(fmt.Sprintf("failed to load %s HTTP server configuration : %s", svcName, err)) } + tp, err := jaegerClient.NewProvider(svcName, cfg.JaegerURL) + if err != nil { + logger.Fatal(fmt.Sprintf("failed to init Jaeger: %s", err)) + } + defer func() { + if err := tp.Shutdown(context.Background()); err != nil { + logger.Error(fmt.Sprintf("error shutting down tracer provider: %v", err)) + } + }() hs := httpserver.New(ctx, cancel, svcName, httpServerConfig, api.MakeHandler(svc, logger, instanceID), logger) if cfg.SendTelemetry { From 80559b61988639efb51ced8cf6f4b769e0c2da04 Mon Sep 17 00:00:00 2001 From: SammyOina Date: Tue, 11 Jul 2023 00:27:26 +0300 Subject: [PATCH 5/6] add tracing provider Signed-off-by: SammyOina --- cmd/bootstrap/main.go | 10 ---------- cmd/certs/main.go | 3 ++- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/cmd/bootstrap/main.go b/cmd/bootstrap/main.go index 5a54425cd8..942c53343a 100644 --- a/cmd/bootstrap/main.go +++ b/cmd/bootstrap/main.go @@ -124,16 +124,6 @@ func main() { logger.Fatal(fmt.Sprintf("failed to load %s HTTP server configuration : %s", svcName, err)) } - tp, err := jaegerClient.NewProvider(svcName, cfg.JaegerURL) - if err != nil { - logger.Error(fmt.Sprintf("Failed to init Jaeger: %s", err)) - } - defer func() { - if err := tp.Shutdown(context.Background()); err != nil { - logger.Error(fmt.Sprintf("Error shutting down tracer provider: %v", err)) - } - }() - hs := httpserver.New(ctx, cancel, svcName, httpServerConfig, api.MakeHandler(svc, bootstrap.NewConfigReader([]byte(cfg.EncKey)), logger, instanceID), logger) if cfg.SendTelemetry { diff --git a/cmd/certs/main.go b/cmd/certs/main.go index dc31365ae4..f228759f0a 100644 --- a/cmd/certs/main.go +++ b/cmd/certs/main.go @@ -47,6 +47,7 @@ type config struct { ThingsURL string `env:"MF_THINGS_URL" envDefault:"http://things:9000"` SendTelemetry bool `env:"MF_SEND_TELEMETRY" envDefault:"true"` InstanceID string `env:"MF_CERTS_INSTANCE_ID" envDefault:""` + JaegerURL string `env:"MF_JAEGER_URL" envDefault:"http://jaeger:14268/api/traces"` // Sign and issue certificates without 3rd party PKI SignCAPath string `env:"MF_CERTS_SIGN_CA_PATH" envDefault:"ca.crt"` @@ -113,7 +114,7 @@ func main() { if err := env.Parse(&httpServerConfig, env.Options{Prefix: envPrefixHttp, AltPrefix: envPrefix}); err != nil { logger.Fatal(fmt.Sprintf("failed to load %s HTTP server configuration : %s", svcName, err)) } - tp, err := jaegerClient.NewProvider(svcName, cfg.JaegerURL) + tp, err := jaegerClient.NewProvider(svcName, cfg.JaegerURL, instanceID) if err != nil { logger.Fatal(fmt.Sprintf("failed to init Jaeger: %s", err)) } From f081f2d9159a32ded0774440e72528ab7eca4f1a Mon Sep 17 00:00:00 2001 From: Sammy Kerata Oina <44265300+SammyOina@users.noreply.github.com> Date: Tue, 11 Jul 2023 10:50:49 +0300 Subject: [PATCH 6/6] Update cmd/certs/main.go Co-authored-by: b1ackd0t Signed-off-by: SammyOina --- cmd/certs/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/certs/main.go b/cmd/certs/main.go index f228759f0a..4466d991f8 100644 --- a/cmd/certs/main.go +++ b/cmd/certs/main.go @@ -119,7 +119,7 @@ func main() { logger.Fatal(fmt.Sprintf("failed to init Jaeger: %s", err)) } defer func() { - if err := tp.Shutdown(context.Background()); err != nil { + if err := tp.Shutdown(ctx); err != nil { logger.Error(fmt.Sprintf("error shutting down tracer provider: %v", err)) } }()