Skip to content

Commit

Permalink
adding support for trace in response. adjusted limits again for clean…
Browse files Browse the repository at this point in the history
…er startup, support for db pgcli problems
  • Loading branch information
ardan-bkennedy committed Jul 29, 2023
1 parent 898d4ab commit afa8280
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 22 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Askar Sagyndyk <[email protected]>
Bob Cao <[email protected]>
Burhan Khalid <[email protected]>
Ciprian Cimpan <[email protected]>
Cole Lichi <[email protected]>
David Paraschivescu <[email protected]>
Denis Nefedov <[email protected]>
Dmytro Tananayskiy <[email protected]>
Expand Down
54 changes: 39 additions & 15 deletions foundation/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package web

import (
"context"
"encoding/hex"
"errors"
"fmt"
"net/http"
"os"
"syscall"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/dimfeld/httptreemux/v5"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -45,7 +46,7 @@ func NewApp(shutdown chan os.Signal, tracer trace.Tracer, mw ...Middleware) *App

return &App{
mux: mux,
otmux: otelhttp.NewHandler(mux, "request", otelhttp.WithPropagators(propagation.TraceContext{})),
otmux: otelhttp.NewHandler(mux, "request"),
shutdown: shutdown,
mw: mw,
tracer: tracer,
Expand Down Expand Up @@ -85,19 +86,8 @@ func (a *App) Handle(method string, group string, path string, handler Handler,
// to the application server mux.
func (a *App) handle(method string, group string, path string, handler Handler) {
h := func(w http.ResponseWriter, r *http.Request) {

// There are times when the handler is called without a tracer, such
// as with tests. We need a span for the trace id.
ctx := r.Context()
span := trace.SpanFromContext(ctx)

// If a tracer exists, then replace the span for the one currently
// found in the context. This may have come from over the wire.
if a.tracer != nil {
ctx, span = a.tracer.Start(ctx, "pkg.web.handle")
span.SetAttributes(attribute.String("endpoint", r.RequestURI))
defer span.End()
}
ctx, span := a.startSpan(w, r)
defer span.End()

v := Values{
TraceID: span.SpanContext().TraceID().String(),
Expand All @@ -122,6 +112,40 @@ func (a *App) handle(method string, group string, path string, handler Handler)
a.mux.Handle(method, finalPath, h)
}

// startSpan initializes the request by adding a span and writing otel
// related information into the response writer for the response.
func (a *App) startSpan(w http.ResponseWriter, r *http.Request) (context.Context, trace.Span) {
ctx := r.Context()

// There are times when the handler is called without a tracer, such
// as with tests. We need a span for the trace id.
span := trace.SpanFromContext(ctx)

// If a tracer exists, then replace the span for the one currently
// found in the context. This may have come from over the wire.
if a.tracer != nil {
ctx, span = a.tracer.Start(ctx, "pkg.web.handle")
span.SetAttributes(attribute.String("endpoint", r.RequestURI))
}

// Add trace context in traceparent form (https://www.w3.org/TR/trace-context/#traceparent-header)
// as Server-Timing header (https://www.w3.org/TR/server-timing/) to the HTTP response.
if spanCtx := trace.SpanContextFromContext(ctx); spanCtx.IsValid() {
traceID := spanCtx.TraceID()
hexTraceID := hex.EncodeToString(traceID[:])

spanID := spanCtx.SpanID()
hexSpanID := hex.EncodeToString(spanID[:])

traceParent := fmt.Sprintf(`traceparent;desc="00-%s-%s-01"`, hexTraceID, hexSpanID)

w.Header().Add("Access-Control-Expose-Headers", "Server-Timing")
w.Header().Add("Server-Timing", traceParent)
}

return ctx, span
}

// validateShutdown validates the error for special conditions that do not
// warrant an actual shutdown by the system.
func validateShutdown(err error) bool {
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ dev-apply:
kubectl wait pods --namespace=$(NAMESPACE) --selector app=promtail --timeout=120s --for=condition=Ready

kustomize build zarf/k8s/dev/sales | kubectl apply -f -
kubectl wait pods --namespace=$(NAMESPACE) --selector app=$(APP) --for=condition=Ready
kubectl wait pods --namespace=$(NAMESPACE) --selector app=$(APP) --timeout=120s --for=condition=Ready

dev-restart:
kubectl rollout restart deployment $(APP) --namespace=$(NAMESPACE)
Expand Down
31 changes: 31 additions & 0 deletions zarf/k8s/dev/database/dev-database.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,29 @@ spec:
- name: data
persistentVolumeClaim:
claimName: database-data
- name: config
configMap:
name: pghbaconf
#defaultMode: 0400
items:
- key: "pg_hba.conf"
path: "pg_hba.conf"
containers:
- name: postgres
image: postgres:15.3
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
- name: config
readOnly: false
mountPath: "/etc/pg_hba.conf"
subPath: "pg_hba.conf"
resources:
requests:
cpu: "500m"
limits:
cpu: "500m"
args: ['-c', 'hba_file=/etc/pg_hba.conf']
env:
- name: POSTGRES_PASSWORD
value: postgres
Expand Down Expand Up @@ -88,3 +100,22 @@ spec:
- name: postgres
port: 5432
targetPort: postgres
---
apiVersion: v1
kind: ConfigMap
metadata:
name: pghbaconf
namespace: sales-system
data:
# file-like keys
pg_hba.conf: |
local all all trust
# IPv4 local connections:
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 0.0.0.0/0 trust
host replication all ::1/128 trust
4 changes: 2 additions & 2 deletions zarf/k8s/dev/loki/dev-loki.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ spec:
- -config.expand-env
resources:
requests:
cpu: 150m
cpu: 100m
limits:
cpu: 150m
cpu: 100m
volumeMounts:
- name: config-volume
mountPath: /loki-conf
Expand Down
4 changes: 2 additions & 2 deletions zarf/k8s/dev/promtail/dev-promtail.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ spec:
protocol: TCP
resources:
requests:
cpu: 150m
cpu: 100m
limits:
cpu: 150m
cpu: 100m
volumeMounts:
- name: config
mountPath: /etc/promtail
Expand Down
4 changes: 2 additions & 2 deletions zarf/k8s/dev/vault/dev-vault.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ spec:
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: "150m"
cpu: "250m"
limits:
cpu: "150m"
cpu: "250m"
command:
- "/bin/sh"
- "-ec"
Expand Down

0 comments on commit afa8280

Please sign in to comment.