Skip to content

Commit

Permalink
chore: refactor coords and add better logging (#110)
Browse files Browse the repository at this point in the history
* chore: refactor coords and add better logging

Signed-off-by: Said Sef <saidsef@gmail.com>

fix: trigger ci workflow when go files are modified

* chore: reduce k8s resources

The monitoring application showed it is overprovisioned
  • Loading branch information
saidsef authored Aug 11, 2024
1 parent 5b64976 commit 9fdac83
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- "go.mod"
- "go.sum"
- "Dockerfile"
- "**/*.go"
- ".github/workflows/docker.yml"
pull_request:
branches:
Expand All @@ -19,6 +20,7 @@ on:
- "go.mod"
- "go.sum"
- "Dockerfile"
- "**/*.go"
- ".github/workflows/docker.yml"
workflow_dispatch:

Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
FROM docker.io/golang:1.22-alpine3.19 AS builder
LABEL maintainer="Said Sef <saidsef@gmail.com> (saidsef.co.uk/)"

ENV PORT ${PORT:-8080}

WORKDIR /app
COPY . /app/
RUN apk add --no-cache curl git && \
Expand All @@ -11,6 +9,7 @@ RUN apk add --no-cache curl git && \

FROM docker.io/alpine:3.20
LABEL maintainer="Said Sef <saidsef@gmail.com> (saidsef.co.uk/)"
ENV PORT=${PORT:-8080}

Check warning on line 12 in Dockerfile

View workflow job for this annotation

GitHub Actions / build

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$PORT' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

Check warning on line 12 in Dockerfile

View workflow job for this annotation

GitHub Actions / build

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$PORT' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

USER nobody

Expand Down
4 changes: 2 additions & 2 deletions charts/reverse-geocoding/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ apiVersion: v2
name: reverse-geocoding
description: Reverse GeoCode Helm chart for Kubernetes
type: application
version: &version 0.3.1
version: &version 0.3.2
appVersion: *version
kubeVersion: ">= 1.27"
keywords:
- reverse-geocoding
- reverse geocoding
- geocoding
- kubernetes
maintainers:
Expand Down
6 changes: 3 additions & 3 deletions charts/reverse-geocoding/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ ingress:

resources:
limits:
cpu: 100m
memory: 512Mi
cpu: 60m
memory: 256Mi
requests:
cpu: 100m
cpu: 30m
memory: 128Mi

livenessProbe:
Expand Down
8 changes: 4 additions & 4 deletions deployment/base/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ spec:
name: http
resources:
requests:
memory: "50Mi"
cpu: "20m"
memory: "30Mi"
cpu: "10m"
limits:
memory: "100Mi"
cpu: "40m"
memory: "60Mi"
cpu: "20m"
livenessProbe:
exec:
command:
Expand Down
5 changes: 5 additions & 0 deletions internal/geo/coordinates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package geo
import (
"encoding/json"
"strconv"

"github.com/saidsef/faas-reverse-geocoding/internal/utils"
)

// Coordinates defines a structure for geographical coordinates with latitude and longitude.
Expand All @@ -29,16 +31,19 @@ func (c *Coordinates) UnmarshalJSON(data []byte) error {
}

if err := json.Unmarshal(data, &aux); err != nil {
utils.Logger.Errorf("Error parsing json payload: %s", err)
return err
}

lat, err := strconv.ParseFloat(aux.Lat, 32)
if err != nil {
utils.Logger.Errorf("Error parsing Lat json payload: %s", err)
return err
}

long, err := strconv.ParseFloat(aux.Long, 32)
if err != nil {
utils.Logger.Errorf("Error parsing Long json payload: %s", err)
return err
}

Expand Down
14 changes: 12 additions & 2 deletions internal/handlers/latitude_longitude.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@ func handlePostRequest(w http.ResponseWriter, r *http.Request) {

if err := decodeRequestBody(r, &c); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
if utils.Verbose {
utils.Logger.Errorf("Error decoding json payload: %s", err)
}
return
}

if err := validateCoordinates(c); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
if utils.Verbose {
utils.Logger.Errorf("Error validating json payload: %s", err)
}
return
}

Expand Down Expand Up @@ -120,14 +126,18 @@ func handleCacheMiss(w http.ResponseWriter, c geo.Coordinates, cacheKey string)
defer resp.Body.Close()

if err := decodeResponseBody(resp, &location); err != nil {
utils.Logger.Errorf("Failed to decode response body: %v", err)
http.Error(w, "Failed to decode response", http.StatusInternalServerError)
if utils.Verbose {
utils.Logger.Errorf("Failed to decode response body: %v", err)
}
return
}

if resp.StatusCode != http.StatusOK {
utils.Logger.Errorf("Failed to get proper status code response: %d", resp.StatusCode)
http.Error(w, fmt.Sprintf("External API error: %s", location), resp.StatusCode)
if utils.Verbose {
utils.Logger.Errorf("Failed to get proper status code response: %d", resp.StatusCode)
}
return
}

Expand Down
6 changes: 4 additions & 2 deletions internal/httpclient/logging_round_tripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ func (lrt *LoggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, er
duration := time.Since(startTime)

if err != nil {
utils.Logger.Errorf("HTTP request error: %s", err)
return nil, err
if utils.Verbose {
utils.Logger.Errorf("HTTP request error: %s", err)
}
return resp, err
}

utils.Logger.Infof("Request: %s %s %s, Response: %d, Duration: %s, remote: %s", req.Method, req.URL, req.Proto, resp.StatusCode, duration, req.RemoteAddr)
Expand Down

0 comments on commit 9fdac83

Please sign in to comment.