Skip to content

opentel-optional #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions agent/container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@ import (
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)

tp, err := opentelemetry.InitTracer()
//opentelemetry
opentelconfig, err := opentelemetry.GetConfigurations()
if err != nil {
log.Fatal(err)
log.Println("Unable to read open telemetry configurations")
}
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
if opentelconfig.IsEnabled {
tp, err := opentelemetry.InitTracer()
if err != nil {
log.Fatal(err)
}
}()

defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
} else {
log.Println("OpenTelemetry is disabled. Tracing will not be enabled.")
}

app := application.New()
go app.GithubContainerWatch()
go app.Start()
Expand Down
8 changes: 5 additions & 3 deletions agent/container/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ func New() *Application {

r := gin.Default()

config, err := opentelemetry.GetConfigurations()
//opentelemetry
opentelconfig, err := opentelemetry.GetConfigurations()
if err != nil {
log.Println("Unable to read open telemetry configurations")
}
if opentelconfig.IsEnabled {
r.Use(otelgin.Middleware(opentelconfig.ServiceName))
}

r.Use(otelgin.Middleware(config.ServiceName))

apiServer.BindRequest(r)

httpServer := &http.Server{
Expand Down
13 changes: 1 addition & 12 deletions agent/container/pkg/application/handlers.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package application

import (
"context"
"io"
"log"

"net/http"

"github.com/intelops/kubviz/pkg/opentelemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)

//githubHandler handles the github webhooks post requests.
// githubHandler handles the github webhooks post requests.
func (app *Application) localRegistryHandler(w http.ResponseWriter, r *http.Request) {

ctx:=context.Background()
tracer := otel.Tracer("container-gitlab")
_, span := tracer.Start(opentelemetry.BuildContext(ctx), "localRegistryHandler")
span.SetAttributes(attribute.String("http.method", "POST"))
defer span.End()

event, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Event body read failed: %v", err)
Expand Down
10 changes: 0 additions & 10 deletions agent/container/pkg/clients/nats_client.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package clients

import (
"context"
"fmt"
"log"
"time"

"github.com/intelops/kubviz/agent/container/pkg/config"
"github.com/intelops/kubviz/pkg/mtlsnats"
"github.com/intelops/kubviz/pkg/opentelemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"

"github.com/nats-io/nats.go"
)
Expand Down Expand Up @@ -143,12 +139,6 @@ func (n *NATSContext) Close() {
// An error is returned if the publishing process fails, such as if the connection is lost or if there are issues with the JetStream.
func (n *NATSContext) Publish(event []byte, repo string) error {

ctx := context.Background()
tracer := otel.Tracer("container-nats-client")
_, span := tracer.Start(opentelemetry.BuildContext(ctx), "ContainerPublish")
span.SetAttributes(attribute.String("repo-name", repo))
defer span.End()

msg := nats.NewMsg(eventSubject)
msg.Data = event
msg.Header.Set("REPO_NAME", repo)
Expand Down
8 changes: 5 additions & 3 deletions agent/container/pkg/handler/api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ func NewAPIHandler(conn *clients.NATSContext) (*APIHandler, error) {

func (ah *APIHandler) BindRequest(r *gin.Engine) {

config, err := opentelemetry.GetConfigurations()
//opentelemetry
opentelconfig, err := opentelemetry.GetConfigurations()
if err != nil {
log.Println("Unable to read open telemetry configurations")
}
if opentelconfig.IsEnabled {
r.Use(otelgin.Middleware(opentelconfig.ServiceName))
}

r.Use(otelgin.Middleware(config.ServiceName))

apiGroup := r.Group("/")
{
apiGroup.GET("/api-docs", ah.GetApiDocs)
Expand Down
18 changes: 13 additions & 5 deletions agent/container/pkg/handler/azure_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/intelops/kubviz/model"
"github.com/intelops/kubviz/pkg/opentelemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)
Expand All @@ -22,11 +23,18 @@ var ErrInvalidPayload = errors.New("invalid or malformed Azure Container Registr
// If the payload is invalid or the publishing process fails, an error response is returned.
func (ah *APIHandler) PostEventAzureContainer(c *gin.Context) {

tracer := otel.Tracer("azure-container")
_, span := tracer.Start(c.Request.Context(), "PostEventAzureContainer")
span.SetAttributes(attribute.String("http.method", "POST"))
defer span.End()

//opentelemetry
opentelconfig, err := opentelemetry.GetConfigurations()
if err != nil {
log.Println("Unable to read open telemetry configurations")
}
if opentelconfig.IsEnabled {
tracer := otel.Tracer("azure-container")
_, span := tracer.Start(c.Request.Context(), "PostEventAzureContainer")
span.SetAttributes(attribute.String("http.method", "POST"))
defer span.End()
}

defer func() {
_, _ = io.Copy(io.Discard, c.Request.Body)
_ = c.Request.Body.Close()
Expand Down
17 changes: 12 additions & 5 deletions agent/container/pkg/handler/docker_event_dockerhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/intelops/kubviz/pkg/opentelemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)
Expand All @@ -18,12 +19,18 @@ var (
)

func (ah *APIHandler) PostEventDockerHub(c *gin.Context) {
//opentelemetry
opentelconfig, err := opentelemetry.GetConfigurations()
if err != nil {
log.Println("Unable to read open telemetry configurations")
}
if opentelconfig.IsEnabled {
tracer := otel.Tracer("dockerhub-container")
_, span := tracer.Start(c.Request.Context(), "PostEventDockerHub")
span.SetAttributes(attribute.String("http.method", "POST"))
defer span.End()
}

tracer := otel.Tracer("dockerhub-container")
_, span := tracer.Start(c.Request.Context(), "PostEventDockerHub")
span.SetAttributes(attribute.String("http.method", "POST"))
defer span.End()

defer func() {
_, _ = io.Copy(io.Discard, c.Request.Body)
_ = c.Request.Body.Close()
Expand Down
18 changes: 13 additions & 5 deletions agent/container/pkg/handler/jfrog_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/intelops/kubviz/model"
"github.com/intelops/kubviz/pkg/opentelemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)
Expand All @@ -17,11 +18,18 @@ var ErrInvalidPayloads = errors.New("invalid or malformed jfrog Container Regist

func (ah *APIHandler) PostEventJfrogContainer(c *gin.Context) {

tracer := otel.Tracer("jfrog-container")
_, span := tracer.Start(c.Request.Context(), "PostEventJfrogContainer")
span.SetAttributes(attribute.String("http.method", "POST"))
defer span.End()

//opentelemetry
opentelconfig, err := opentelemetry.GetConfigurations()
if err != nil {
log.Println("Unable to read open telemetry configurations")
}
if opentelconfig.IsEnabled {
tracer := otel.Tracer("jfrog-container")
_, span := tracer.Start(c.Request.Context(), "PostEventJfrogContainer")
span.SetAttributes(attribute.String("http.method", "POST"))
defer span.End()
}

defer func() {
_, _ = io.Copy(io.Discard, c.Request.Body)
_ = c.Request.Body.Close()
Expand Down
20 changes: 14 additions & 6 deletions agent/container/pkg/handler/quay_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ import (

"github.com/gin-gonic/gin"
"github.com/intelops/kubviz/model"
"github.com/intelops/kubviz/pkg/opentelemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)

func (ah *APIHandler) PostEventQuayContainer(c *gin.Context) {

tracer := otel.Tracer("quay-container")
_, span := tracer.Start(c.Request.Context(), "PostEventQuayContainer")
span.SetAttributes(attribute.String("http.method", "POST"))
defer span.End()

//opentelemetry
opentelconfig, err := opentelemetry.GetConfigurations()
if err != nil {
log.Println("Unable to read open telemetry configurations")
}
if opentelconfig.IsEnabled {
tracer := otel.Tracer("quay-container")
_, span := tracer.Start(c.Request.Context(), "PostEventQuayContainer")
span.SetAttributes(attribute.String("http.method", "POST"))
defer span.End()
}

defer func() {
_, _ = io.Copy(io.Discard, c.Request.Body)
_ = c.Request.Body.Close()
Expand All @@ -45,4 +53,4 @@ func (ah *APIHandler) PostEventQuayContainer(c *gin.Context) {
return
}
c.Status(http.StatusOK)
}
}
21 changes: 15 additions & 6 deletions agent/git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ func main() {
log.Fatalf("Could not parse env Config: %v", err)
}

tp, err := opentelemetry.InitTracer()
//opentelemetry
opentelconfig, err := opentelemetry.GetConfigurations()
if err != nil {
log.Fatal(err)
log.Println("Unable to read open telemetry configurations")
}
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
if opentelconfig.IsEnabled {
tp, err := opentelemetry.InitTracer()
if err != nil {
log.Fatal(err)
}
}()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
} else {
log.Println("OpenTelemetry is disabled. Tracing will not be enabled.")
}

// Connect to NATS
natsContext, err := clients.NewNATSContext(cfg)
Expand Down
8 changes: 5 additions & 3 deletions agent/git/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ func New(conf *config.Config, conn *clients.NATSContext) *Application {
func (app *Application) Routes() *gin.Engine {
router := gin.New()

config, err := opentelemetry.GetConfigurations()
//opentelemetry
opentelconfig, err := opentelemetry.GetConfigurations()
if err != nil {
log.Println("Unable to read open telemetry configurations")
}
if opentelconfig.IsEnabled {
router.Use(otelgin.Middleware(opentelconfig.ServiceName))
}

router.Use(otelgin.Middleware(config.ServiceName))

api.RegisterHandlers(router, app)
return router
}
Expand Down
Loading