Skip to content

Commit

Permalink
Merge pull request #2955 from DataDog/cbeauchesne/golang-2799
Browse files Browse the repository at this point in the history
[golang] Use /healthcheck to get library version
  • Loading branch information
cbeauchesne committed Sep 9, 2024
2 parents e73596a + d164c7d commit a5473dd
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-graphql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:

- name: Compress logs
id: compress_logs
if: always()
if: always() && steps.build.outcome == 'success'
run: tar -czvf artifact.tar.gz $(ls | grep logs)
- name: Upload artifact
if: always() && steps.compress_logs.outcome == 'success'
Expand Down
3 changes: 2 additions & 1 deletion utils/_context/_scenarios/parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def configure(self, config):
self._clean_networks()

# https://github.com/DataDog/system-tests/issues/2799
if library in ("nodejs", "python"):
if library in ("nodejs", "python", "golang"):
output = _get_client().containers.run(
self.apm_test_server_definition.container_tag,
remove=True,
Expand Down Expand Up @@ -422,6 +422,7 @@ def golang_library_factory():
COPY {golang_reldir}/. /app
# download the proper tracer version
COPY utils/build/docker/golang/install_ddtrace.sh binaries* /binaries/
COPY utils/build/docker/golang/parametric/system_tests_library_version.sh system_tests_library_version.sh
RUN /binaries/install_ddtrace.sh
RUN go install
Expand Down
7 changes: 4 additions & 3 deletions utils/_context/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ def configure(self, replay):
)

# https://github.com/DataDog/system-tests/issues/2799
if self.library in ("nodejs", "python"):
if self.library in ("nodejs", "python", "golang"):
self.healthcheck = {
"test": f"curl --fail --silent --show-error --max-time 2 localhost:{self.port}/healthcheck",
"retries": 60,
Expand Down Expand Up @@ -715,13 +715,14 @@ def post_start(self):

# new way of getting info from the weblog. Only working for nodejs and python right now
# https://github.com/DataDog/system-tests/issues/2799
if self.library in ("nodejs", "python"):
if self.library in ("nodejs", "python", "golang"):
with open(self.healthcheck_log_file, mode="r", encoding="utf-8") as f:
data = json.load(f)
lib = data["library"]

self._library = LibraryVersion(lib["language"], lib["version"])
self.libddwaf_version = LibraryVersion("libddwaf", lib["libddwaf_version"]).version
if "libddwaf_version" in lib:
self.libddwaf_version = LibraryVersion("libddwaf", lib["libddwaf_version"]).version

if self.appsec_rules_version == "0.0.0" and "appsec_event_rules_version" in lib:
self.appsec_rules_version = LibraryVersion("appsec_rules", lib["appsec_event_rules_version"]).version
Expand Down
18 changes: 18 additions & 0 deletions utils/build/docker/golang/app/chi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/http"
"os"
"encoding/json"
"strconv"
"time"
"weblog/internal/rasp"
Expand Down Expand Up @@ -35,6 +36,23 @@ func main() {
w.Write([]byte("Hello, WAF!\n"))
})

mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {

healthCheck, err := common.GetHealtchCheck()
if err != nil {
http.Error(w, "Can't get JSON data", http.StatusInternalServerError)
}

jsonData, err := json.Marshal(healthCheck)
if err != nil {
http.Error(w, "Can't build JSON data", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
})

mux.HandleFunc("/waf/*", func(w http.ResponseWriter, r *http.Request) {
body, err := common.ParseBody(r)
if err == nil {
Expand Down
10 changes: 10 additions & 0 deletions utils/build/docker/golang/app/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ func main() {
return c.NoContent(http.StatusOK)
})

r.GET("/healthcheck", func(c echo.Context) error {
healthCheck, err := common.GetHealtchCheck()

if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}

return c.JSON(http.StatusOK, healthCheck)
})

r.Any("/*", func(c echo.Context) error {
return c.NoContent(http.StatusNotFound)
})
Expand Down
11 changes: 11 additions & 0 deletions utils/build/docker/golang/app/gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ func main() {
r.Any("/", func(ctx *gin.Context) {
ctx.Writer.WriteHeader(http.StatusOK)
})

r.GET("/healthcheck", func(ctx *gin.Context) {
healthCheck, err := common.GetHealtchCheck()

if err != nil {
ctx.JSON(http.StatusInternalServerError, err)
}

ctx.JSON(http.StatusOK, healthCheck)
})

r.Any("/waf", func(ctx *gin.Context) {
body, err := common.ParseBody(ctx.Request)
if err == nil {
Expand Down
19 changes: 19 additions & 0 deletions utils/build/docker/golang/app/gqlgen/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"net/http"
"encoding/json"
"weblog/gqlgen/graph"
"weblog/internal/common"

Expand Down Expand Up @@ -36,6 +37,24 @@ func main() {
w.WriteHeader(http.StatusOK)
})

mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {

healthCheck, err := common.GetHealtchCheck()
if err != nil {
http.Error(w, "Can't get JSON data", http.StatusInternalServerError)
return
}

jsonData, err := json.Marshal(healthCheck)
if err != nil {
http.Error(w, "Can't build JSON data", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
})

common.InitDatadog()

panic(http.ListenAndServe(":7777", mux))
Expand Down
18 changes: 18 additions & 0 deletions utils/build/docker/golang/app/graph-gophers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"net/http"
"encoding/json"
"weblog/internal/common"

graphqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/graph-gophers/graphql-go"
Expand Down Expand Up @@ -48,6 +49,23 @@ func main() {
w.WriteHeader(http.StatusOK)
})

mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {

healthCheck, err := common.GetHealtchCheck()
if err != nil {
http.Error(w, "Can't get JSON data", http.StatusInternalServerError)
}

jsonData, err := json.Marshal(healthCheck)
if err != nil {
http.Error(w, "Can't build JSON data", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
})

common.InitDatadog()

panic(http.ListenAndServe(":7777", mux))
Expand Down
19 changes: 19 additions & 0 deletions utils/build/docker/golang/app/graphql-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"net/http"
"encoding/json"
"weblog/internal/common"

graphqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/graphql-go/graphql"
Expand Down Expand Up @@ -72,6 +73,24 @@ func main() {
w.WriteHeader(http.StatusOK)
})

mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {

healthCheck, err := common.GetHealtchCheck()
if err != nil {
http.Error(w, "Can't get JSON data", http.StatusInternalServerError)
}

jsonData, err := json.Marshal(healthCheck)
if err != nil {
http.Error(w, "Can't build JSON data", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
})


common.InitDatadog()

panic(http.ListenAndServe(":7777", mux))
Expand Down
49 changes: 49 additions & 0 deletions utils/build/docker/golang/app/internal/common/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package common

import (
"os"
"errors"
"encoding/json"
"encoding/xml"
"io"
Expand All @@ -11,6 +13,20 @@ import (
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)


type DatadogInformations struct {
Language string `json:"language"`
Version string `json:"version"`
AppsecEventRulesVersion string `json:"appsec_event_rules_version"`
LibddwafVersion string `json:"libddwaf_version"`
}

type HealtchCheck struct {
Status string `json:"status"`
Library DatadogInformations `json:"library"`
}


func InitDatadog() {
span := tracer.StartSpan("init.service")
defer span.Finish()
Expand Down Expand Up @@ -52,3 +68,36 @@ func ForceSpanIndexingTags() []ddtrace.StartSpanOption {
tracer.Tag("_dd.filter.id", "system_tests_e2e"),
}
}


func GetHealtchCheck() (HealtchCheck, error) {
datadogInformations, err := GetDatadogInformations()

if err != nil {
return HealtchCheck{}, err
}

return HealtchCheck{
Status: "ok",
Library: datadogInformations,
}, nil
}

func GetDatadogInformations() (DatadogInformations, error) {

tracerVersion, err := os.ReadFile("SYSTEM_TESTS_LIBRARY_VERSION")
if err != nil {
return DatadogInformations{}, errors.New("Can't get SYSTEM_TESTS_LIBRARY_VERSION")
}

appsecRulesVersion, err := os.ReadFile("SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION")
if err != nil {
return DatadogInformations{}, errors.New("Can't get SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION")
}

return DatadogInformations{
Language: "golang",
Version: string(tracerVersion),
AppsecEventRulesVersion: string(appsecRulesVersion),
}, nil
}
17 changes: 17 additions & 0 deletions utils/build/docker/golang/app/net-http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ func main() {
w.WriteHeader(http.StatusOK)
})

mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {

healthCheck, err := common.GetHealtchCheck()
if err != nil {
http.Error(w, "Can't get JSON data", http.StatusInternalServerError)
}

jsonData, err := json.Marshal(healthCheck)
if err != nil {
http.Error(w, "Can't build JSON data", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
})

mux.HandleFunc("/waf", func(w http.ResponseWriter, r *http.Request) {
body, err := common.ParseBody(r)
if err == nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

cat SYSTEM_TESTS_LIBRARY_VERSION

0 comments on commit a5473dd

Please sign in to comment.