Skip to content

Commit

Permalink
Moved to otel collector as standard
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Simpson committed Apr 1, 2024
1 parent ffb2aee commit f40cef7
Show file tree
Hide file tree
Showing 44 changed files with 1,019 additions and 422 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ coverprofile.out

# Development postgres data
postgres-data

# SRO config files
cmd/**/config.yaml
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ PROTO_FILES = $(shell find $(PROTO_DIR) -name '*.proto')

MOCK_INTERFACES = $(shell egrep -rl --include="*.go" "type (\w*) interface {" $(ROOT_DIR)/pkg | sed "s/.go$$//")

CMDS = $(shell find $(ROOT_DIR)/cmd -maxdepth 1 -mindepth 1 -type d | sed "s/^.*\///")
ALL_CMDS = $(shell find $(ROOT_DIR)/cmd -maxdepth 1 -mindepth 1 -type d | sed "s/^.*\///")
CMDS = $(filter-out stressy, $(ALL_CMDS))

# _____ _
# |_ _| | |
Expand Down Expand Up @@ -83,9 +84,11 @@ mocks: clean-mocks
clean-mocks:
rm -rf $(ROOT_DIR)/pkg/mocks

build: $(addprefix build-, $(CMDS))
build: $(addprefix build-, $(CMDS)) buildclis
build-%:
go build -ldflags="-X 'github.com/ShatteredRealms/go-backend/pkg/config/default.Version=$(BASE_VERSION)'" -o $(ROOT_DIR)/bin/$* $(ROOT_DIR)/cmd/$*
buildclis:
go build -o $(ROOT_DIR)/bin/stressy $(ROOT_DIR)/cmd/stressy

run: $(addprefix run-, $(CMDS))
run-%:
Expand Down
4 changes: 3 additions & 1 deletion build/character.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
FROM golang:1.22 AS build
WORKDIR /src
ENV CGO_ENABLED=0
COPY ./ ./
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
ARG APP_VERSION=v0.0.1
RUN go build \
-ldflags="-X 'github.com/ShatteredRealms/go-backend/pkg/config/default.Version=${APP_VERSION}'" \
Expand Down
4 changes: 3 additions & 1 deletion build/chat.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
FROM golang:1.22 AS build
WORKDIR /src
ENV CGO_ENABLED=0
COPY ./ ./
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
ARG APP_VERSION=v0.0.1
RUN go build \
-ldflags="-X 'github.com/ShatteredRealms/go-backend/pkg/config/default.Version=${APP_VERSION}'" \
Expand Down
4 changes: 3 additions & 1 deletion build/gamebackend.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
FROM golang:1.22 AS build
WORKDIR /src
ENV CGO_ENABLED=0
COPY ./ ./
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
ARG APP_VERSION=v0.0.1
RUN go build \
-ldflags="-X 'github.com/ShatteredRealms/go-backend/pkg/config/default.Version=${APP_VERSION}'" \
Expand Down
13 changes: 9 additions & 4 deletions cmd/character/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
"github.com/ShatteredRealms/go-backend/pkg/service"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)

var (
ServiceName = "characters"
ServiceName = "character"
)

type CharactersServerContext struct {
Expand All @@ -30,18 +31,22 @@ func NewServerContext(ctx context.Context, conf *config.GlobalConfig) *Character
server := &CharactersServerContext{
GlobalConfig: conf,
Tracer: otel.Tracer("CharactersService"),
KeycloakClient: gocloak.NewClient(conf.GameBackend.Keycloak.BaseURL),
KeycloakClient: gocloak.NewClient(conf.Keycloak.BaseURL),
}

postgres, err := repository.ConnectDB(server.GlobalConfig.Character.Postgres)
helpers.Check(ctx, err, "connecting to postgres database")

characterRepo := repository.NewCharacterRepository(postgres)
characterRepo, err := repository.NewCharacterRepository(postgres)
helpers.Check(ctx, err, "character repo")
characterService, err := service.NewCharacterService(ctx, characterRepo)
helpers.Check(ctx, err, "character serivce")
server.CharacterService = characterService

mongoDb, err := mongo.Connect(ctx, options.Client().ApplyURI(server.GlobalConfig.Character.Mongo.Master.MongoDSN()))
opts := options.Client()
opts.Monitor = otelmongo.NewMonitor()
opts.ApplyURI(server.GlobalConfig.Character.Mongo.Master.MongoDSN())
mongoDb, err := mongo.Connect(ctx, opts)
helpers.Check(ctx, err, "connecting to mongo database")
invRepo := repository.NewInventoryRepository(mongoDb.Database(server.GlobalConfig.Character.Mongo.Master.Name))
server.InventoryService = service.NewInventoryService(invRepo)
Expand Down
42 changes: 31 additions & 11 deletions cmd/character/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package main

import (
"context"
"errors"
"os"
"os/signal"

"github.com/ShatteredRealms/go-backend/pkg/helpers"
"github.com/ShatteredRealms/go-backend/pkg/log"
"github.com/ShatteredRealms/go-backend/pkg/pb"
"github.com/ShatteredRealms/go-backend/pkg/srv"
"github.com/uptrace/uptrace-go/uptrace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/ShatteredRealms/go-backend/cmd/character/app"
character "github.com/ShatteredRealms/go-backend/cmd/character/app"
"github.com/ShatteredRealms/go-backend/pkg/config"
)

Expand All @@ -24,20 +27,28 @@ func init() {
}

func main() {
ctx := context.Background()
uptrace.ConfigureOpentelemetry(
uptrace.WithDSN(conf.Uptrace.DSN),
uptrace.WithServiceName(character.ServiceName),
uptrace.WithServiceVersion(conf.Version),
)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

otelShutdown, err := helpers.SetupOTelSDK(ctx, character.ServiceName, config.Version, conf.OpenTelemetry.Addr)
defer func() {
err = errors.Join(err, otelShutdown(context.Background()))
if err != nil {
log.Logger.Infof("Error shutting down: %v", err)
}
}()

if err != nil {
log.Logger.Fatal(err)
}

server := character.NewServerContext(ctx, conf)
grpcServer, gwmux := helpers.InitServerDefaults()
address := server.GlobalConfig.Character.Local.Address()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}

pb.RegisterHealthServiceServer(grpcServer, srv.NewHealthServiceServer())
err := pb.RegisterHealthServiceHandlerFromEndpoint(ctx, gwmux, address, opts)
err = pb.RegisterHealthServiceHandlerFromEndpoint(ctx, gwmux, address, opts)
helpers.Check(ctx, err, "register health service handler endpoint")

css, err := srv.NewCharacterServiceServer(ctx, server)
Expand All @@ -46,9 +57,18 @@ func main() {
err = pb.RegisterCharacterServiceHandlerFromEndpoint(ctx, gwmux, address, opts)
helpers.Check(ctx, err, "registering characters service handler endpoint")

helpers.StartServer(ctx, grpcServer, gwmux, server.GlobalConfig.Character.Local.Address())
srvErr := make(chan error, 1)
go func() {
srvErr <- helpers.StartServer(ctx, grpcServer, gwmux, server.GlobalConfig.Character.Local.Address())
}()

for {
select {
case err = <-srvErr:
log.Logger.Fatalf("listen server: %v", err)

case <-ctx.Done():
log.Logger.Info("Server canceled by user input.")
stop()
}

}
3 changes: 2 additions & 1 deletion cmd/chat/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chat

import (
"context"

"github.com/Nerzal/gocloak/v13"
"github.com/ShatteredRealms/go-backend/pkg/config"
"github.com/ShatteredRealms/go-backend/pkg/helpers"
Expand All @@ -28,7 +29,7 @@ func NewServerContext(ctx context.Context, conf *config.GlobalConfig) *ChatServe
server := &ChatServerContext{
GlobalConfig: conf,
Tracer: otel.Tracer("ChatService"),
KeycloakClient: gocloak.NewClient(conf.GameBackend.Keycloak.BaseURL),
KeycloakClient: gocloak.NewClient(conf.Keycloak.BaseURL),
}

db, err := repository.ConnectDB(conf.Chat.Postgres)
Expand Down
41 changes: 31 additions & 10 deletions cmd/chat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package main

import (
"context"
"errors"
"os"
"os/signal"

character "github.com/ShatteredRealms/go-backend/cmd/character/app"
chat "github.com/ShatteredRealms/go-backend/cmd/chat/app"
"github.com/ShatteredRealms/go-backend/pkg/helpers"
"github.com/ShatteredRealms/go-backend/pkg/log"
"github.com/ShatteredRealms/go-backend/pkg/pb"
"github.com/ShatteredRealms/go-backend/pkg/srv"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/ShatteredRealms/go-backend/pkg/config"
"github.com/uptrace/uptrace-go/uptrace"
)

var (
Expand All @@ -23,20 +28,28 @@ func init() {
}

func main() {
ctx := context.Background()
uptrace.ConfigureOpentelemetry(
uptrace.WithDSN(conf.Uptrace.DSN),
uptrace.WithServiceName(chat.ServiceName),
uptrace.WithServiceVersion(conf.Version),
)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

otelShutdown, err := helpers.SetupOTelSDK(ctx, character.ServiceName, config.Version, conf.OpenTelemetry.Addr)
defer func() {
err = errors.Join(err, otelShutdown(context.Background()))
if err != nil {
log.Logger.Infof("Error shutting down: %v", err)
}
}()

if err != nil {
log.Logger.Fatal(err)
}

server := chat.NewServerContext(ctx, conf)
grpcServer, gwmux := helpers.InitServerDefaults()
address := server.GlobalConfig.Chat.Local.Address()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}

pb.RegisterHealthServiceServer(grpcServer, srv.NewHealthServiceServer())
err := pb.RegisterHealthServiceHandlerFromEndpoint(ctx, gwmux, address, opts)
err = pb.RegisterHealthServiceHandlerFromEndpoint(ctx, gwmux, address, opts)
helpers.Check(ctx, err, "register health service handler endpoint")

srvService, err := srv.NewChatServiceServer(ctx, server)
Expand All @@ -45,9 +58,17 @@ func main() {
err = pb.RegisterChatServiceHandlerFromEndpoint(ctx, gwmux, address, opts)
helpers.Check(ctx, err, "register chat service handler endpoint")

helpers.StartServer(ctx, grpcServer, gwmux, server.GlobalConfig.Chat.Local.Address())
srvErr := make(chan error, 1)
go func() {
srvErr <- helpers.StartServer(ctx, grpcServer, gwmux, server.GlobalConfig.Chat.Local.Address())
}()

for {
select {
case err = <-srvErr:
log.Logger.Fatalf("listen server: %v", err)

case <-ctx.Done():
log.Logger.Info("Server canceled by user input.")
stop()
}
}
2 changes: 1 addition & 1 deletion cmd/gamebackend/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewServerContext(ctx context.Context, conf *config.GlobalConfig) *GameBacke
server := &GameBackendServerContext{
GlobalConfig: conf,
Tracer: otel.Tracer("GameBackendService"),
KeycloakClient: gocloak.NewClient(conf.GameBackend.Keycloak.BaseURL),
KeycloakClient: gocloak.NewClient(conf.Keycloak.BaseURL),
}

charactersService, err := helpers.GrpcClientWithOtel(conf.Character.Remote.Address())
Expand Down
40 changes: 30 additions & 10 deletions cmd/gamebackend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package main

import (
"context"
"errors"
"os"
"os/signal"

character "github.com/ShatteredRealms/go-backend/cmd/character/app"
gamebackend "github.com/ShatteredRealms/go-backend/cmd/gamebackend/app"
"github.com/ShatteredRealms/go-backend/pkg/helpers"
"github.com/ShatteredRealms/go-backend/pkg/log"
"github.com/ShatteredRealms/go-backend/pkg/pb"
"github.com/ShatteredRealms/go-backend/pkg/srv"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/ShatteredRealms/go-backend/pkg/config"
"github.com/uptrace/uptrace-go/uptrace"
)

var (
Expand All @@ -23,20 +28,27 @@ func init() {
}

func main() {
ctx := context.Background()
uptrace.ConfigureOpentelemetry(
uptrace.WithDSN(conf.Uptrace.DSN),
uptrace.WithServiceName(gamebackend.ServiceName),
uptrace.WithServiceVersion(conf.Version),
)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

otelShutdown, err := helpers.SetupOTelSDK(ctx, character.ServiceName, config.Version, conf.OpenTelemetry.Addr)
defer func() {
err = errors.Join(err, otelShutdown(context.Background()))
if err != nil {
log.Logger.Infof("Error shutting down: %v", err)
}
}()

if err != nil {
log.Logger.Fatal(err)
}
server := gamebackend.NewServerContext(ctx, conf)
grpcServer, gwmux := helpers.InitServerDefaults()
address := server.GlobalConfig.GameBackend.Local.Address()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}

pb.RegisterHealthServiceServer(grpcServer, srv.NewHealthServiceServer())
err := pb.RegisterHealthServiceHandlerFromEndpoint(ctx, gwmux, address, opts)
err = pb.RegisterHealthServiceHandlerFromEndpoint(ctx, gwmux, address, opts)
helpers.Check(ctx, err, "register health service handler endpoint")

connServer, err := srv.NewConnectionServiceServer(ctx, server)
Expand All @@ -51,9 +63,17 @@ func main() {
err = pb.RegisterServerManagerServiceHandlerFromEndpoint(ctx, gwmux, address, opts)
helpers.Check(ctx, err, "register server manager service handler endpoint")

helpers.StartServer(ctx, grpcServer, gwmux, server.GlobalConfig.GameBackend.Local.Address())
srvErr := make(chan error, 1)
go func() {
srvErr <- helpers.StartServer(ctx, grpcServer, gwmux, server.GlobalConfig.GameBackend.Local.Address())
}()

for {
select {
case err = <-srvErr:
log.Logger.Fatalf("listen server: %v", err)

case <-ctx.Done():
log.Logger.Info("Server canceled by user input.")
stop()
}
}
Loading

0 comments on commit f40cef7

Please sign in to comment.