Skip to content

Commit

Permalink
Merge pull request #305 from intelops/opentelemetry
Browse files Browse the repository at this point in the history
opentelemetry
  • Loading branch information
vijeyash1 authored Jan 16, 2024
2 parents ab5bcdf + 460c85e commit b94c692
Show file tree
Hide file tree
Showing 32 changed files with 636 additions and 43 deletions.
13 changes: 13 additions & 0 deletions agent/container/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
package main

import (
"context"
"log"
"os"
"os/signal"
"syscall"

"github.com/intelops/kubviz/agent/container/pkg/application"
"github.com/intelops/kubviz/pkg/opentelemetry"
)

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)

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)
}
}()

app := application.New()
go app.GithubContainerWatch()
go app.Start()
Expand Down
10 changes: 10 additions & 0 deletions agent/container/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/intelops/kubviz/agent/container/pkg/clients"
"github.com/intelops/kubviz/agent/container/pkg/config"
"github.com/intelops/kubviz/agent/container/pkg/handler"
"github.com/intelops/kubviz/pkg/opentelemetry"
"github.com/kelseyhightower/envconfig"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)

type Application struct {
Expand Down Expand Up @@ -43,6 +45,14 @@ func New() *Application {
}

r := gin.Default()

config, err := opentelemetry.GetConfigurations()
if err != nil {
log.Println("Unable to read open telemetry configurations")
}

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

apiServer.BindRequest(r)

httpServer := &http.Server{
Expand Down
12 changes: 12 additions & 0 deletions agent/container/pkg/application/handlers.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
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.
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
11 changes: 11 additions & 0 deletions agent/container/pkg/clients/nats_client.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package clients

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

"github.com/intelops/kubviz/agent/container/pkg/config"
"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 @@ -112,6 +116,13 @@ func (n *NATSContext) Close() {
// The repository information in the header can be used by subscribers to filter or route the event based on its origin or destination.
// 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
11 changes: 11 additions & 0 deletions agent/container/pkg/handler/api_handler.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package handler

import (
"log"
"net/http"

"github.com/gin-gonic/gin"
"github.com/intelops/kubviz/agent/container/api"
"github.com/intelops/kubviz/agent/container/pkg/clients"
"github.com/intelops/kubviz/pkg/opentelemetry"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)

type APIHandler struct {
Expand All @@ -29,6 +32,14 @@ func NewAPIHandler(conn *clients.NATSContext) (*APIHandler, error) {
}

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

config, err := opentelemetry.GetConfigurations()
if err != nil {
log.Println("Unable to read open telemetry configurations")
}

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

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

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

var ErrInvalidPayload = errors.New("invalid or malformed Azure Container Registry webhook payload")
Expand All @@ -19,6 +21,12 @@ var ErrInvalidPayload = errors.New("invalid or malformed Azure Container Registr
// application to subscribe to these events and respond to changes in the container registry.
// 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()

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

"github.com/gin-gonic/gin"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)

// parse errors
Expand All @@ -16,6 +18,12 @@ var (
)

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

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
8 changes: 8 additions & 0 deletions agent/container/pkg/handler/jfrog_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ import (

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

var ErrInvalidPayloads = errors.New("invalid or malformed jfrog Container Registry webhook payload")

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()

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

"github.com/gin-gonic/gin"
"github.com/intelops/kubviz/model"
"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()

defer func() {
_, _ = io.Copy(io.Discard, c.Request.Body)
_ = c.Request.Body.Close()
Expand Down
12 changes: 12 additions & 0 deletions agent/git/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"log"
"os"
"os/signal"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/intelops/kubviz/agent/git/pkg/application"
"github.com/intelops/kubviz/agent/git/pkg/clients"
"github.com/intelops/kubviz/agent/git/pkg/config"
"github.com/intelops/kubviz/pkg/opentelemetry"

"github.com/kelseyhightower/envconfig"
)
Expand All @@ -20,6 +22,16 @@ func main() {
log.Fatalf("Could not parse env Config: %v", err)
}

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)
}
}()

// Connect to NATS
natsContext, err := clients.NewNATSContext(cfg)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions agent/git/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/intelops/kubviz/agent/git/api"
"github.com/intelops/kubviz/agent/git/pkg/clients"
"github.com/intelops/kubviz/agent/git/pkg/config"
"github.com/intelops/kubviz/pkg/opentelemetry"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"

"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -41,6 +43,14 @@ func New(conf *config.Config, conn *clients.NATSContext) *Application {

func (app *Application) Routes() *gin.Engine {
router := gin.New()

config, err := opentelemetry.GetConfigurations()
if err != nil {
log.Println("Unable to read open telemetry configurations")
}

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

api.RegisterHandlers(router, app)
return router
}
Expand Down
32 changes: 32 additions & 0 deletions agent/git/pkg/application/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ import (
"github.com/intelops/kubviz/agent/git/api"
"github.com/intelops/kubviz/gitmodels/azuremodel"
"github.com/intelops/kubviz/model"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)

func (app *Application) PostGitea(c *gin.Context) {
log.Println("gitea handler called...")

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

defer log.Println("gitea handler exited...")

event := c.Request.Header.Get(string(model.GiteaHeader))
Expand All @@ -30,6 +38,12 @@ func (app *Application) PostGitea(c *gin.Context) {

func (app *Application) PostAzure(c *gin.Context) {
log.Println("azure handler called...")

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

defer log.Println("azure handler exited...")

jsonData, err := c.GetRawData()
Expand Down Expand Up @@ -58,6 +72,12 @@ func (app *Application) PostAzure(c *gin.Context) {
// githubHandler handles the github webhooks post requests.
func (app *Application) PostGithub(c *gin.Context) {
log.Println("github handler called...")

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

defer log.Println("github handler exited...")

event := c.Request.Header.Get(string(model.GithubHeader))
Expand All @@ -79,6 +99,12 @@ func (app *Application) PostGithub(c *gin.Context) {
// gitlabHandler handles the github webhooks post requests.
func (app *Application) PostGitlab(c *gin.Context) {
log.Println("gitlab handler called...")

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

defer log.Println("gitlab handler exited...")

event := c.Request.Header.Get(string(model.GitlabHeader))
Expand All @@ -100,6 +126,12 @@ func (app *Application) PostGitlab(c *gin.Context) {
// bitBucketHandler handles the github webhooks post requests.
func (app *Application) PostBitbucket(c *gin.Context) {
log.Println("bitbucket handler called...")

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

defer log.Println("bitbucket handler exited...")

event := c.Request.Header.Get(string(model.BitBucketHeader))
Expand Down
11 changes: 11 additions & 0 deletions agent/git/pkg/clients/nats_client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package clients

import (
"context"
"fmt"

"github.com/intelops/kubviz/agent/git/pkg/config"
"github.com/intelops/kubviz/model"
"github.com/intelops/kubviz/pkg/opentelemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"

"log"
"time"
Expand Down Expand Up @@ -91,6 +95,13 @@ func (n *NATSContext) Close() {
}

func (n *NATSContext) Publish(metric []byte, repo string, eventkey model.EventKey, eventvalue model.EventValue) error {

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

msg := nats.NewMsg(eventSubject)
msg.Data = metric
msg.Header.Set("GitProvider", repo)
Expand Down
Loading

0 comments on commit b94c692

Please sign in to comment.