Skip to content

Commit

Permalink
add Jaeger and nginx-ingress
Browse files Browse the repository at this point in the history
  • Loading branch information
naumov-andrey committed Sep 17, 2023
1 parent 70cc056 commit 6e3b745
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 101 deletions.
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
CLUSTER_NAME = exploring-cloud-native
CLUSTER_CONFIG_PATH = cluster-config.yaml

.PHONY: cluster clean
.PHONY: cluster cluster-rebuild port-forward clean

cluster:
kind create cluster --name $(CLUSTER_NAME) --config $(CLUSTER_CONFIG_PATH)
helm upgrade --install ingress-nginx ingress-nginx --repo https://kubernetes.github.io/ingress-nginx
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.6.3/cert-manager.yaml
sleep 10s
kubectl apply -f ./cluster/observability/namespace.yaml
kubectl create -f https://github.com/jaegertracing/jaeger-operator/releases/download/v1.47.0/jaeger-operator.yaml -n observability
sleep 10s
kubectl apply -R -f ./cluster/

cluster-rebuild: clean cluster

port-forward:
kubectl port-forward service/ingress-nginx-controller 8080:80

clean:
kind delete cluster --name $(CLUSTER_NAME)
19 changes: 0 additions & 19 deletions cluster/default-ns/nginx/deployment.yaml

This file was deleted.

19 changes: 19 additions & 0 deletions cluster/default/echo-server/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: echo-server-deployment
spec:
selector:
matchLabels:
app: echo-server
replicas: 2
template:
metadata:
labels:
app: echo-server
spec:
containers:
- name: echo-server
image: naumovandrey/echo-server:v0.1.1
ports:
- containerPort: 8080
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: nginx-service
name: echo-server-svc
spec:
selector:
app: nginx
app: echo-server
ports:
- port: 80
targetPort: 80
- port: 8080
targetPort: 8080
protocol: TCP
5 changes: 5 additions & 0 deletions cluster/default/jaeger/jaeger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: jaegertracing.io/v1
kind: Jaeger
metadata:
name: jaeger

25 changes: 25 additions & 0 deletions cluster/default/nginx/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /echo
pathType: Exact
backend:
service:
name: echo-server-svc
port:
number: 8080
- path: /
pathType: Prefix
backend:
service:
name: jaeger-query
port:
number: 16686
5 changes: 5 additions & 0 deletions cluster/observability/namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
kind: Namespace
metadata:
name: observability

1 change: 0 additions & 1 deletion src/echo-server/.gitignore

This file was deleted.

18 changes: 18 additions & 0 deletions src/echo-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.19-alpine as builder

WORKDIR /build
COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -o ./echo-server ./cmd/main.go

FROM alpine:latest

WORKDIR /service
COPY --from=builder /build/echo-server ./echo-server

ENTRYPOINT ["./echo-server"]
81 changes: 13 additions & 68 deletions src/echo-server/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,22 @@ import (

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
)

const (
serviceName = "echo-server"
serviceVersion = "v0.1.0"

tracerName = "tracer"
tracesFileName = "traces.txt"
"github.com/naumov-andrey/exploring-cloud-native/echo-server/config"
"github.com/naumov-andrey/exploring-cloud-native/echo-server/tracing"
)

func main() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
log.Printf("Staring %s:%s", config.ServiceName, config.ServiceVersion)

ctx := context.Background()

f, err := os.Create(tracesFileName)
if err != nil {
log.Fatal(err)
}
defer f.Close()

exporter, err := newExporter(f)
if err != nil {
log.Fatal(err)
}

resource, err := newResource()
tracerProvider, err := tracing.NewJaegerTraceProvider()
if err != nil {
log.Fatal(err)
}

tracerProvider := trace.NewTracerProvider(
trace.WithBatcher(exporter),
trace.WithResource(resource),
)
defer func() {
if err := tracerProvider.Shutdown(ctx); err != nil {
log.Fatal(err)
Expand All @@ -65,16 +38,19 @@ func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(otelecho.Middleware(config.ServiceName))

e.POST("/", echoHandler)

errCh := make(chan error, 1)
go func() {
log.Printf("Starting server")
err := e.Start(":8080")
err := e.Start(config.Port)
errCh <- err
}()

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)

select {
case <-sigCh:
log.Printf("Starting shutdown...")
Expand All @@ -92,41 +68,10 @@ func main() {
}

func echoHandler(c echo.Context) error {
ctx := c.Request().Context()

_, span := otel.Tracer(tracerName).Start(ctx, "handler")
defer span.End()

msgBytes, err := io.ReadAll(c.Request().Body)
msg, err := io.ReadAll(c.Request().Body)
if err != nil {
errMsg := fmt.Sprintf("failed to read request body: %s", err)
span.RecordError(err)
span.SetStatus(codes.Error, errMsg)
return c.String(http.StatusInternalServerError, errMsg)
return fmt.Errorf("failed to read request body: %w", err)
}

msg := string(msgBytes)

span.SetAttributes(attribute.String("request.message", msg))

return c.String(http.StatusOK, msg)
}

func newExporter(w io.Writer) (trace.SpanExporter, error) {
return stdouttrace.New(
stdouttrace.WithWriter(w),
stdouttrace.WithPrettyPrint(),
)
}

func newResource() (*resource.Resource, error) {
return resource.Merge(
resource.Default(),
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName(serviceName),
semconv.ServiceVersion(serviceVersion),
attribute.String("environment", "demo"),
),
)
return c.String(http.StatusOK, string(msg))
}
11 changes: 11 additions & 0 deletions src/echo-server/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

const (
ServiceName = "echo-server"
ServiceVersion = "v0.1.1"

Env = "demo"
Port = ":8080"

JaegerCollectorEndpoint = "http://jaeger-collector:14268/api/traces"
)
42 changes: 34 additions & 8 deletions src/echo-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,51 @@ go 1.19

require (
github.com/labstack/echo/v4 v4.11.1
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel v1.18.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
go.opentelemetry.io/otel/sdk v1.17.0
)

require (
github.com/DataDog/appsec-internal-go v1.0.0 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.45.0-rc.1 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.48.0-devel.0.20230725154044-2549ba9058df // indirect
github.com/DataDog/datadog-go/v5 v5.1.1 // indirect
github.com/DataDog/go-libddwaf v1.4.2 // indirect
github.com/DataDog/go-tuf v1.0.1-0.5.2 // indirect
github.com/DataDog/sketches-go v1.2.1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/ebitengine/purego v0.5.0-alpha // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/outcaste-io/ristretto v0.2.1 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.7.0 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.44.0 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.17.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.opentelemetry.io/otel/trace v1.18.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go4.org/intern v0.0.0-20211027215823-ae77deb06f29 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20220617031537-928513b29760 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/DataDog/dd-trace-go.v1 v1.54.1 // indirect
inet.af/netaddr v0.0.0-20220811202034-502d2d690317 // indirect
)
Loading

0 comments on commit 6e3b745

Please sign in to comment.