Compare commits
2 Commits
dev
...
documentor
| Author | SHA1 | Date | |
|---|---|---|---|
| 23ddde58da | |||
| 091651af99 |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,5 +0,0 @@
|
|||||||
bin/
|
|
||||||
*.exe
|
|
||||||
*.test
|
|
||||||
.DS_Store
|
|
||||||
vendor/
|
|
||||||
20
Dockerfile
20
Dockerfile
@@ -1,20 +0,0 @@
|
|||||||
# ---- Build stage ----
|
|
||||||
FROM golang:1.22-alpine AS build
|
|
||||||
WORKDIR /src
|
|
||||||
|
|
||||||
COPY go.mod go.sum* ./
|
|
||||||
RUN go mod tidy && go mod download
|
|
||||||
|
|
||||||
COPY . .
|
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /app .
|
|
||||||
|
|
||||||
# ---- Runtime stage (distroless) ----
|
|
||||||
FROM gcr.io/distroless/static-debian12:nonroot
|
|
||||||
COPY --from=build /app /app
|
|
||||||
|
|
||||||
EXPOSE 8080
|
|
||||||
|
|
||||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
|
||||||
CMD ["/app", "-health-check"]
|
|
||||||
|
|
||||||
ENTRYPOINT ["/app"]
|
|
||||||
@@ -2,7 +2,8 @@ apiVersion: backstage.io/v1alpha1
|
|||||||
kind: Component
|
kind: Component
|
||||||
metadata:
|
metadata:
|
||||||
name: gate-svc-1
|
name: gate-svc-1
|
||||||
description: Task 14 gate canary - create-microservice scaffolded onto crucible-apps-dev-aks
|
description: Task 14 gate canary - create-microservice scaffolded onto
|
||||||
|
crucible-apps-dev-aks
|
||||||
annotations:
|
annotations:
|
||||||
humanitec.com/orgId: crucible
|
humanitec.com/orgId: crucible
|
||||||
humanitec.com/projectId: apps-cluster
|
humanitec.com/projectId: apps-cluster
|
||||||
@@ -18,19 +19,16 @@ metadata:
|
|||||||
prometheus.io/path: "/metrics"
|
prometheus.io/path: "/metrics"
|
||||||
grafana/grafana-instance: default
|
grafana/grafana-instance: default
|
||||||
grafana/alert-label-selector: "app=gate-svc-1"
|
grafana/alert-label-selector: "app=gate-svc-1"
|
||||||
|
|
||||||
grafana/dashboard-selector: "uid == 'otel-app-observability-v2'"
|
grafana/dashboard-selector: "uid == 'otel-app-observability-v2'"
|
||||||
grafana.com/alert-label-selector: "app=gate-svc-1"
|
grafana.com/alert-label-selector: "app=gate-svc-1"
|
||||||
grafana.com/dashboard-url: "https://grafana.kyndemo.live/d/otel-app-observability-v2/opentelemetry-application-observability?orgId=1&var-app=gate-svc-1"
|
grafana.com/dashboard-url: "https://grafana.kyndemo.live/d/otel-app-observability-v2/opentelemetry-application-observability?orgId=1&var-app=gate-svc-1"
|
||||||
chaos-mesh/enabled: "true"
|
chaos-mesh/enabled: "true"
|
||||||
|
|
||||||
|
|
||||||
k6/enabled: "true"
|
k6/enabled: "true"
|
||||||
k6/test-configmap: "k6-test-gate-svc-1"
|
k6/test-configmap: "k6-test-gate-svc-1"
|
||||||
k6/test-namespace: "dev"
|
k6/test-namespace: "dev"
|
||||||
k6/target-service: "gate-svc-1"
|
k6/target-service: "gate-svc-1"
|
||||||
k6/target-port: "8080"
|
k6/target-port: "8080"
|
||||||
|
docs.kyndemo.live/readme: catalog/component/default/gate-svc-1/README.md
|
||||||
tags:
|
tags:
|
||||||
- microservice
|
- microservice
|
||||||
- golden-path
|
- golden-path
|
||||||
@@ -44,11 +42,13 @@ metadata:
|
|||||||
- k6
|
- k6
|
||||||
|
|
||||||
links:
|
links:
|
||||||
- url: https://console.dev.orchestrator.crucible.kyndemo.live/orgs/crucible/projects/apps-cluster
|
- url:
|
||||||
|
https://console.dev.orchestrator.crucible.kyndemo.live/orgs/crucible/projects/apps-cluster
|
||||||
title: Humanitec Console
|
title: Humanitec Console
|
||||||
icon: dashboard
|
icon: dashboard
|
||||||
|
|
||||||
- url: https://grafana.kyndemo.live/d/otel-app-observability-v2/opentelemetry-application-observability?orgId=1&var-app=gate-svc-1
|
- url:
|
||||||
|
https://grafana.kyndemo.live/d/otel-app-observability-v2/opentelemetry-application-observability?orgId=1&var-app=gate-svc-1
|
||||||
title: Grafana Dashboard
|
title: Grafana Dashboard
|
||||||
icon: dashboard
|
icon: dashboard
|
||||||
|
|
||||||
@@ -70,6 +70,7 @@ metadata:
|
|||||||
description: REST API for gate-svc-1
|
description: REST API for gate-svc-1
|
||||||
annotations:
|
annotations:
|
||||||
backstage.io/techdocs-ref: dir:.
|
backstage.io/techdocs-ref: dir:.
|
||||||
|
docs.kyndemo.live/readme: catalog/api/default/gate-svc-1-api/README.md
|
||||||
spec:
|
spec:
|
||||||
type: openapi
|
type: openapi
|
||||||
lifecycle: experimental
|
lifecycle: experimental
|
||||||
|
|||||||
13
go.mod
13
go.mod
@@ -1,13 +0,0 @@
|
|||||||
module github.com/kyndryl-platform/gate-svc-1
|
|
||||||
|
|
||||||
go 1.22
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/prometheus/client_golang v1.19.1
|
|
||||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0
|
|
||||||
go.opentelemetry.io/otel v1.27.0
|
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.27.0
|
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.27.0
|
|
||||||
go.opentelemetry.io/otel/sdk v1.27.0
|
|
||||||
go.opentelemetry.io/otel/sdk/metric v1.27.0
|
|
||||||
)
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: k6-test-gate-svc-1
|
|
||||||
namespace: dev
|
|
||||||
labels:
|
|
||||||
app: gate-svc-1
|
|
||||||
app.kubernetes.io/managed-by: backstage
|
|
||||||
app.kubernetes.io/component: load-testing
|
|
||||||
data:
|
|
||||||
K6_OUT: opentelemetry
|
|
||||||
K6_OTEL_GRPC_EXPORTER_INSECURE: 'true'
|
|
||||||
K6_OTEL_GRPC_EXPORTER_ENDPOINT: otel-collector.monitoring.svc.cluster.local:4317
|
|
||||||
K6_OTEL_METRIC_PREFIX: k6_
|
|
||||||
K6_OTEL_FLUSH_INTERVAL: '1000'
|
|
||||||
K6_OTEL_EXPORT_INTERVAL: '5000'
|
|
||||||
K6_OTEL_SERVICE_NAME: k6-gate-svc-1
|
|
||||||
load-test.js: "import http from 'k6/http';\nimport { check, sleep, group } from\
|
|
||||||
\ 'k6';\n\nconst vus = parseInt(__ENV.TEST_VUS || '10');\nconst duration = __ENV.TEST_DURATION\
|
|
||||||
\ || '30s';\nconst targetUrl = __ENV.TARGET_URL || 'http://gate-svc-1.dev.svc.cluster.local:8080';\n\
|
|
||||||
\nexport const options = {\n scenarios: {\n load_test: {\n executor:\
|
|
||||||
\ 'ramping-vus',\n startVUs: 0,\n stages: [\n { duration: '10s',\
|
|
||||||
\ target: vus },\n { duration: duration, target: vus },\n { duration:\
|
|
||||||
\ '5s', target: 0 },\n ],\n },\n },\n thresholds: {\n http_req_duration:\
|
|
||||||
\ ['p(95)<500'],\n http_req_failed: ['rate<0.01'],\n },\n};\n\nhttp.setResponseCallback(http.expectedStatuses({\
|
|
||||||
\ min: 200, max: 399 }));\n\nexport default function () {\n group('Health Check',\
|
|
||||||
\ () => {\n const res = http.get(`${targetUrl}/health`);\n check(res, {\n\
|
|
||||||
\ 'status is 200': (r) => r.status === 200,\n 'response time < 500ms':\
|
|
||||||
\ (r) => r.timings.duration < 500,\n 'body contains status UP': (r) => r.json().status\
|
|
||||||
\ === 'UP',\n });\n });\n\n sleep(0.5);\n\n group('Items API', () => {\n\
|
|
||||||
\ // List all items\n let res = http.get(`${targetUrl}/api/items`);\n \
|
|
||||||
\ check(res, {\n 'status is 200': (r) => r.status === 200,\n 'response\
|
|
||||||
\ time < 500ms': (r) => r.timings.duration < 500,\n 'body is an array': (r)\
|
|
||||||
\ => Array.isArray(r.json()),\n });\n\n // Create a new item\n const\
|
|
||||||
\ newItem = { name: 'Test Item', description: 'This is a test item' };\n res\
|
|
||||||
\ = http.post(`${targetUrl}/api/items`, JSON.stringify(newItem), {\n headers:\
|
|
||||||
\ { 'Content-Type': 'application/json' },\n });\n check(res, {\n 'status\
|
|
||||||
\ is 201': (r) => r.status === 201,\n 'response time < 500ms': (r) => r.timings.duration\
|
|
||||||
\ < 500,\n 'body contains id': (r) => r.json().id !== undefined,\n });\n\
|
|
||||||
\n const itemId = res.json().id;\n\n // Retrieve the created item\n res\
|
|
||||||
\ = http.get(`${targetUrl}/api/items/${itemId}`);\n check(res, {\n 'status\
|
|
||||||
\ is 200': (r) => r.status === 200,\n 'response time < 500ms': (r) => r.timings.duration\
|
|
||||||
\ < 500,\n 'body contains correct id': (r) => r.json().id === itemId,\n \
|
|
||||||
\ });\n\n // Update the item\n const updatedItem = { name: 'Updated Item',\
|
|
||||||
\ description: 'Updated description' };\n res = http.put(`${targetUrl}/api/items/${itemId}`,\
|
|
||||||
\ JSON.stringify(updatedItem), {\n headers: { 'Content-Type': 'application/json'\
|
|
||||||
\ },\n });\n check(res, {\n 'status is 200': (r) => r.status === 200,\n\
|
|
||||||
\ 'response time < 500ms': (r) => r.timings.duration < 500,\n 'body\
|
|
||||||
\ contains updated name': (r) => r.json().name === 'Updated Item',\n });\n\n\
|
|
||||||
\ // Delete the item\n res = http.del(`${targetUrl}/api/items/${itemId}`);\n\
|
|
||||||
\ check(res, {\n 'status is 200': (r) => r.status === 200,\n 'response\
|
|
||||||
\ time < 500ms': (r) => r.timings.duration < 500,\n 'body contains deleted\
|
|
||||||
\ id': (r) => r.json().deleted === itemId,\n });\n });\n\n sleep(0.5);\n}"
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
import http from 'k6/http';
|
|
||||||
import { check, sleep, group } from 'k6';
|
|
||||||
|
|
||||||
const vus = parseInt(__ENV.TEST_VUS || '10');
|
|
||||||
const duration = __ENV.TEST_DURATION || '30s';
|
|
||||||
const targetUrl = __ENV.TARGET_URL || 'http://gate-svc-1.dev.svc.cluster.local:8080';
|
|
||||||
|
|
||||||
export const options = {
|
|
||||||
scenarios: {
|
|
||||||
load_test: {
|
|
||||||
executor: 'ramping-vus',
|
|
||||||
startVUs: 0,
|
|
||||||
stages: [
|
|
||||||
{ duration: '10s', target: vus },
|
|
||||||
{ duration: duration, target: vus },
|
|
||||||
{ duration: '5s', target: 0 },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
thresholds: {
|
|
||||||
http_req_duration: ['p(95)<500'],
|
|
||||||
http_req_failed: ['rate<0.01'],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
http.setResponseCallback(http.expectedStatuses({ min: 200, max: 399 }));
|
|
||||||
|
|
||||||
export default function () {
|
|
||||||
group('Health Check', () => {
|
|
||||||
const res = http.get(`${targetUrl}/health`);
|
|
||||||
check(res, {
|
|
||||||
'status is 200': (r) => r.status === 200,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body contains status UP': (r) => r.json().status === 'UP',
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
sleep(0.5);
|
|
||||||
|
|
||||||
group('Items API', () => {
|
|
||||||
// List all items
|
|
||||||
let res = http.get(`${targetUrl}/api/items`);
|
|
||||||
check(res, {
|
|
||||||
'status is 200': (r) => r.status === 200,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body is an array': (r) => Array.isArray(r.json()),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create a new item
|
|
||||||
const newItem = { name: 'Test Item', description: 'This is a test item' };
|
|
||||||
res = http.post(`${targetUrl}/api/items`, JSON.stringify(newItem), {
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
});
|
|
||||||
check(res, {
|
|
||||||
'status is 201': (r) => r.status === 201,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body contains id': (r) => r.json().id !== undefined,
|
|
||||||
});
|
|
||||||
|
|
||||||
const itemId = res.json().id;
|
|
||||||
|
|
||||||
// Retrieve the created item
|
|
||||||
res = http.get(`${targetUrl}/api/items/${itemId}`);
|
|
||||||
check(res, {
|
|
||||||
'status is 200': (r) => r.status === 200,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body contains correct id': (r) => r.json().id === itemId,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Update the item
|
|
||||||
const updatedItem = { name: 'Updated Item', description: 'Updated description' };
|
|
||||||
res = http.put(`${targetUrl}/api/items/${itemId}`, JSON.stringify(updatedItem), {
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
});
|
|
||||||
check(res, {
|
|
||||||
'status is 200': (r) => r.status === 200,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body contains updated name': (r) => r.json().name === 'Updated Item',
|
|
||||||
});
|
|
||||||
|
|
||||||
// Delete the item
|
|
||||||
res = http.del(`${targetUrl}/api/items/${itemId}`);
|
|
||||||
check(res, {
|
|
||||||
'status is 200': (r) => r.status === 200,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body contains deleted id': (r) => r.json().deleted === itemId,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
sleep(0.5);
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
apiVersion: k6.io/v1alpha1
|
|
||||||
kind: TestRun
|
|
||||||
metadata:
|
|
||||||
name: k6-gate-svc-1
|
|
||||||
namespace: dev
|
|
||||||
labels:
|
|
||||||
app: gate-svc-1
|
|
||||||
backstage.io/component: gate-svc-1
|
|
||||||
app.kubernetes.io/managed-by: backstage
|
|
||||||
app.kubernetes.io/component: load-testing
|
|
||||||
spec:
|
|
||||||
parallelism: 1
|
|
||||||
script:
|
|
||||||
configMap:
|
|
||||||
name: k6-test-gate-svc-1
|
|
||||||
file: load-test.js
|
|
||||||
runner:
|
|
||||||
image: grafana/k6:latest
|
|
||||||
envFrom:
|
|
||||||
- configMapRef:
|
|
||||||
name: k6-test-gate-svc-1
|
|
||||||
env:
|
|
||||||
- name: K6_OTEL_SERVICE_NAME
|
|
||||||
value: k6-gate-svc-1
|
|
||||||
215
main.go
215
main.go
@@ -1,215 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
|
||||||
"go.opentelemetry.io/otel"
|
|
||||||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
|
|
||||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
|
||||||
"go.opentelemetry.io/otel/propagation"
|
|
||||||
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
|
|
||||||
"go.opentelemetry.io/otel/sdk/resource"
|
|
||||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
||||||
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Domain model
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
type Item struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ItemRequest struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// In-memory store
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
var (
|
|
||||||
store sync.Map
|
|
||||||
counter atomic.Int64
|
|
||||||
)
|
|
||||||
|
|
||||||
func nextID() int {
|
|
||||||
return int(counter.Add(1))
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Helpers
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
func writeJSON(w http.ResponseWriter, status int, v any) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(status)
|
|
||||||
_ = json.NewEncoder(w).Encode(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func itemIDFromPath(path string) (int, bool) {
|
|
||||||
parts := strings.Split(strings.TrimSuffix(path, "/"), "/")
|
|
||||||
if len(parts) < 3 {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
id, err := strconv.Atoi(parts[len(parts)-1])
|
|
||||||
return id, err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Handlers
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
func healthHandler(w http.ResponseWriter, _ *http.Request) {
|
|
||||||
writeJSON(w, http.StatusOK, map[string]string{"status": "UP"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func itemsHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// Route: /api/items or /api/items/{id}
|
|
||||||
hasSub := strings.Count(strings.TrimSuffix(r.URL.Path, "/"), "/") >= 3
|
|
||||||
|
|
||||||
if hasSub {
|
|
||||||
id, ok := itemIDFromPath(r.URL.Path)
|
|
||||||
if !ok {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid id"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch r.Method {
|
|
||||||
case http.MethodGet:
|
|
||||||
v, exists := store.Load(id)
|
|
||||||
if !exists {
|
|
||||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, http.StatusOK, v)
|
|
||||||
case http.MethodPut:
|
|
||||||
var req ItemRequest
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
existingRaw, exists := store.Load(id)
|
|
||||||
if !exists {
|
|
||||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
existing := existingRaw.(Item)
|
|
||||||
if req.Name == "" {
|
|
||||||
req.Name = existing.Name
|
|
||||||
}
|
|
||||||
if req.Description == "" {
|
|
||||||
req.Description = existing.Description
|
|
||||||
}
|
|
||||||
updated := Item{ID: id, Name: req.Name, Description: req.Description}
|
|
||||||
store.Store(id, updated)
|
|
||||||
writeJSON(w, http.StatusOK, updated)
|
|
||||||
case http.MethodDelete:
|
|
||||||
store.Delete(id)
|
|
||||||
writeJSON(w, http.StatusOK, map[string]int{"deleted": id})
|
|
||||||
default:
|
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// /api/items — list or create
|
|
||||||
switch r.Method {
|
|
||||||
case http.MethodGet:
|
|
||||||
var items []Item
|
|
||||||
store.Range(func(_, v any) bool {
|
|
||||||
items = append(items, v.(Item))
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
if items == nil {
|
|
||||||
items = []Item{}
|
|
||||||
}
|
|
||||||
writeJSON(w, http.StatusOK, items)
|
|
||||||
case http.MethodPost:
|
|
||||||
var req ItemRequest
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if req.Name == "" {
|
|
||||||
req.Name = "unnamed"
|
|
||||||
}
|
|
||||||
item := Item{ID: nextID(), Name: req.Name, Description: req.Description}
|
|
||||||
store.Store(item.ID, item)
|
|
||||||
writeJSON(w, http.StatusCreated, item)
|
|
||||||
default:
|
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// OpenTelemetry setup
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// setupOTel initialises the OTLP trace and metric pipelines from OTEL_* env
|
|
||||||
// vars (injected by score.yaml / Humanitec). Returns a no-op shutdown func
|
|
||||||
// when OTEL_EXPORTER_OTLP_ENDPOINT is not set (local dev).
|
|
||||||
func setupOTel(ctx context.Context) func() {
|
|
||||||
if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") == "" {
|
|
||||||
return func() {}
|
|
||||||
}
|
|
||||||
svcName := os.Getenv("OTEL_SERVICE_NAME")
|
|
||||||
if svcName == "" {
|
|
||||||
svcName = "unknown"
|
|
||||||
}
|
|
||||||
res, _ := resource.New(ctx,
|
|
||||||
resource.WithAttributes(semconv.ServiceName(svcName)),
|
|
||||||
resource.WithFromEnv(),
|
|
||||||
)
|
|
||||||
traceExp, _ := otlptracehttp.New(ctx)
|
|
||||||
tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(traceExp), sdktrace.WithResource(res))
|
|
||||||
otel.SetTracerProvider(tp)
|
|
||||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
|
||||||
propagation.TraceContext{}, propagation.Baggage{},
|
|
||||||
))
|
|
||||||
metricExp, _ := otlpmetrichttp.New(ctx)
|
|
||||||
mp := sdkmetric.NewMeterProvider(
|
|
||||||
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(metricExp)),
|
|
||||||
sdkmetric.WithResource(res),
|
|
||||||
)
|
|
||||||
otel.SetMeterProvider(mp)
|
|
||||||
return func() {
|
|
||||||
_ = tp.Shutdown(context.Background())
|
|
||||||
_ = mp.Shutdown(context.Background())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Main
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ctx := context.Background()
|
|
||||||
shutdownOTel := setupOTel(ctx)
|
|
||||||
defer shutdownOTel()
|
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
|
|
||||||
mux.HandleFunc("/health", healthHandler)
|
|
||||||
mux.Handle("/metrics", promhttp.Handler())
|
|
||||||
mux.HandleFunc("/api/items/", itemsHandler)
|
|
||||||
mux.HandleFunc("/api/items", itemsHandler)
|
|
||||||
|
|
||||||
addr := ":8080"
|
|
||||||
fmt.Printf("gate-svc-1 listening on %s\n", addr)
|
|
||||||
log.Fatal(http.ListenAndServe(addr, otelhttp.NewHandler(mux, "")))
|
|
||||||
}
|
|
||||||
16
main_test.go
16
main_test.go
@@ -1,16 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestHealth(t *testing.T) {
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
||||||
rec := httptest.NewRecorder()
|
|
||||||
healthHandler(rec, req)
|
|
||||||
if rec.Code != http.StatusOK {
|
|
||||||
t.Fatalf("expected 200, got %d", rec.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user