Skip to content

Commit

Permalink
Writing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
efumagal committed Aug 8, 2023
1 parent e0956e2 commit 1570575
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ghcr-build-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
push:
branches:
- main
paths-ignore:
- '**/README.md'

env:
REGISTRY: ghcr.io
Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
# geo-3d-otel

[![golangci-lint](https://github.com/efumagal/geo-3d-otel/actions/workflows/golangci-lint.yml/badge.svg)](https://github.com/efumagal/geo-3d-otel/actions/workflows/golangci-lint.yml)

## Introduction

A simple HTTP client to serve APIs to calculate distance between 3D points.
Implemented using [Fiber](https://gofiber.io) and instrumented using [OpenTelemetry](https://opentelemetry.io).

When pushing on main the [Publish Docker image to GHCR](.github/workflows/ghcr-build-push.yml) is triggered this performs:
- Build the Docker image and push it to ghcr.io registry
- Scan the docker image using [Snyk](https://snyk.io)
- Update [kustomization.yaml](kustomize/kustomization.yaml) with the newly generated Docker image


## Run

```shell
go run main.go
```

## Load test

Pre-requiste [K6](https://k6.io)

[Load test K6 script](k6-load/load_distance.js)

```shell
k6 run load_distance.js
```

## TO DOs

- For a real app consider structuring the Go code using DDD Hexagonal pattern
- Add unit tests and run them on PRs
- Generate OpenAPI specs

## Notes

-
21 changes: 21 additions & 0 deletions k6-load/load_distance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import http from "k6/http";
import { check, group, sleep } from "k6";

export const options = {
stages: [
{ duration: "10s", target: 20 }, // simulate ramp-up of traffic from 1 to 20 users over 10s.
{ duration: "20s", target: 100 }, // stay at 100 users for 20 seconds
{ duration: "10s", target: 0 }, // ramp-down to 0 users
],
thresholds: {
http_req_duration: ["p(99)<100"], // 99% of requests must completed below 100ms
},
};

const BASE_URL = "http://localhost:8080/distance";

export default () => {
check(http.get(BASE_URL), {
"status is 200": (r) => r.status == 200,
});
};
4 changes: 2 additions & 2 deletions k6-load/load.js → k6-load/load_high_cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export const options = {
},
};

const BASE_URL = "http://localhost:8080/cpu";
const BASE_URL = "http://localhost:8080/action/cpu-load";

export default () => {
check(http.get(BASE_URL), {
check(http.post(BASE_URL), {
"status is 200": (r) => r.status == 200,
});
};
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
var tracer trace.Tracer

func init() {
// Name the tracer after the package, or the service if you are in main
tracer = otel.Tracer("github.com/efumagal/geo-3d-otel")
}

Expand All @@ -49,6 +48,7 @@ func main() {

app := fiber.New()

// Exclude instrumentation for /health
app.Use(otelfiber.Middleware(otelfiber.WithNext(func(c *fiber.Ctx) bool {
return c.Path() == "/health"
})))
Expand All @@ -62,7 +62,8 @@ func main() {
})

app.Get("/hello", func(c *fiber.Ctx) error {
return c.SendStatus(http.StatusOK)
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return c.SendString("<h1>Hello, World !</h1>")
})

app.Get("/distance", func(c *fiber.Ctx) error {
Expand All @@ -78,11 +79,11 @@ func main() {
return c.JSON(map[string]any{"distance": distance})
})

app.Get("/cpu", func(c *fiber.Ctx) error {
app.Post("/action/cpu-load", func(c *fiber.Ctx) error {
// Create a child span
_, childSpan := tracer.Start(c.UserContext(), "distance_computation")

for i := 0; i < 1_000_000; i++ {
for i := 0; i < 1_000; i++ {
start := geo.NewCoord3d(randFloat(-90, 90), randFloat(-180, 180), randFloat(0, 10000))
end := geo.NewCoord3d(randFloat(-90, 90), randFloat(-180, 180), randFloat(0, 10000))
geo.Distance3D(start, end)
Expand Down

0 comments on commit 1570575

Please sign in to comment.