From 14095fb10bbea6ed31f2bd4e3d63240c7168f367 Mon Sep 17 00:00:00 2001 From: Qian Deng Date: Fri, 27 Aug 2021 15:54:20 +0000 Subject: [PATCH] Add trace to registryctl * Add trace init to main * Add trace for http server * Add trace for gc * Add env template trace Signed-off-by: Qian Deng --- .../prepare/templates/registryctl/env.jinja | 19 +++++++++++++++++ make/photon/prepare/utils/registry_ctl.py | 2 +- src/registryctl/client/client.go | 1 + src/registryctl/handlers/handler.go | 7 ++++++- src/registryctl/main.go | 21 ++++++++++++++----- 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/make/photon/prepare/templates/registryctl/env.jinja b/make/photon/prepare/templates/registryctl/env.jinja index 2bae9a2b1..2c6722280 100644 --- a/make/photon/prepare/templates/registryctl/env.jinja +++ b/make/photon/prepare/templates/registryctl/env.jinja @@ -9,3 +9,22 @@ INTERNAL_TLS_CERT_PATH=/etc/harbor/ssl/registryctl.crt {% if internal_tls.verify_client_cert %} INTERNAL_VERIFY_CLIENT_CERT=true {% endif %} +{% if trace.enabled %} +TRACE_ENABLED=true +TRACE_SERVICE_NAME=harbor-registryctl +TRACE_SAMPLE_RATE={{ trace.sample_rate }} +{% if trace.jaeger is defined %} +TRACE_JAEGER_ENDPOINT={{ trace.jaeger.endpoint if trace.jaeger.endpoint else '' }} +TRACE_JAEGER_USERNAME={{ trace.jaeger.username if trace.jaeger.username else '' }} +TRACE_JAEGER_PASSWORD={{ trace.jaeger.password if trace.jaeger.password else '' }} +TRACE_JAEGER_AGENT_HOSTNAME={{ trace.jaeger.agent_host if trace.jaeger.agent_host else '' }} +TRACE_JAEGER_AGENT_PORT={{ trace.jaeger.agent_port if trace.jaeger.agent_port else '' }} +{% endif %} +{%if trace.otel is defined %} +TRACE_OTEL_ENDPOINT={{ trace.otel.endpoint if trace.otel.endpoint else '' }} +TRACE_OTEL_URL_PATH={{ trace.otel.url_path if trace.otel.url_path else '' }} +TRACE_OTEL_COMPRESSION={{ trace.otel.compression if trace.otel.compression else '' }} +TRACE_OTEL_TIMEOUT={{ trace.otel.timeout }} +TRACE_OTEL_INSECURE={{ trace.otel.insecure if trace.otel.insecure else '' }} +{% endif %} +{% endif %} diff --git a/make/photon/prepare/utils/registry_ctl.py b/make/photon/prepare/utils/registry_ctl.py index b7b92a402..7f9cbd24a 100644 --- a/make/photon/prepare/utils/registry_ctl.py +++ b/make/photon/prepare/utils/registry_ctl.py @@ -1,4 +1,4 @@ -import os, shutil +import os from g import config_dir, templates_dir, DEFAULT_GID, DEFAULT_UID from utils.misc import prepare_dir diff --git a/src/registryctl/client/client.go b/src/registryctl/client/client.go index 7b0164aa6..bd19e5eb2 100644 --- a/src/registryctl/client/client.go +++ b/src/registryctl/client/client.go @@ -23,6 +23,7 @@ import ( common_http "github.com/goharbor/harbor/src/common/http" "github.com/goharbor/harbor/src/common/http/modifier/auth" "github.com/goharbor/harbor/src/common/utils" + "github.com/goharbor/harbor/src/lib/errors" ) // const definition diff --git a/src/registryctl/handlers/handler.go b/src/registryctl/handlers/handler.go index 84564aa5d..a21abfb21 100644 --- a/src/registryctl/handlers/handler.go +++ b/src/registryctl/handlers/handler.go @@ -18,10 +18,12 @@ import ( "net/http" "os" + gorilla_handlers "github.com/gorilla/handlers" + "github.com/goharbor/harbor/src/lib/log" + tracelib "github.com/goharbor/harbor/src/lib/trace" "github.com/goharbor/harbor/src/registryctl/auth" "github.com/goharbor/harbor/src/registryctl/config" - gorilla_handlers "github.com/gorilla/handlers" ) // NewHandlerChain returns a gorilla router which is wrapped by authenticate handler @@ -36,6 +38,9 @@ func NewHandlerChain(conf config.Configuration) http.Handler { } h = newAuthHandler(auth.NewSecretHandler(secrets), h, insecureAPIs) h = gorilla_handlers.LoggingHandler(os.Stdout, h) + if tracelib.Enabled() { + h = tracelib.NewHandler(h, "serve-http") + } return h } diff --git a/src/registryctl/main.go b/src/registryctl/main.go index b7b567ac5..735c129fc 100644 --- a/src/registryctl/main.go +++ b/src/registryctl/main.go @@ -15,15 +15,11 @@ package main import ( + "context" "crypto/tls" "flag" "net/http" - common_http "github.com/goharbor/harbor/src/common/http" - "github.com/goharbor/harbor/src/lib/log" - "github.com/goharbor/harbor/src/registryctl/config" - "github.com/goharbor/harbor/src/registryctl/handlers" - _ "github.com/docker/distribution/registry/storage/driver/azure" _ "github.com/docker/distribution/registry/storage/driver/filesystem" _ "github.com/docker/distribution/registry/storage/driver/gcs" @@ -33,6 +29,12 @@ import ( _ "github.com/docker/distribution/registry/storage/driver/oss" _ "github.com/docker/distribution/registry/storage/driver/s3-aws" _ "github.com/docker/distribution/registry/storage/driver/swift" + + common_http "github.com/goharbor/harbor/src/common/http" + "github.com/goharbor/harbor/src/lib/log" + tracelib "github.com/goharbor/harbor/src/lib/trace" + "github.com/goharbor/harbor/src/registryctl/config" + "github.com/goharbor/harbor/src/registryctl/handlers" ) // RegistryCtl for registry controller @@ -78,6 +80,15 @@ func main() { log.Fatalf("Failed to load configurations with error: %s\n", err) } + if tracelib.Enabled() { + tp := tracelib.InitGlobalTracer(context.Background()) + defer func() { + if err := tp.Shutdown(context.Background()); err != nil { + log.Errorf("Error shutting down tracer provider: %v", err) + } + }() + } + regCtl := &RegistryCtl{ ServerConf: *config.DefaultConfig, Handler: handlers.NewHandlerChain(*config.DefaultConfig),