Skip to content

Commit 7c246d8

Browse files
dandimeodandimeonerpaula
authored
Add Debug capability to arangoproxy (#289)
* debug field in config * debug function in logger * prepend [DEBUG] to Debug messages, add arangosh code injection devug message * debug mode working, more debug messages * add debug messages to openapi spec processing and save cache function * prettier configuration debug --------- Co-authored-by: dandimeo <[email protected]> Co-authored-by: Paula Mihu <[email protected]>
1 parent 008e3a9 commit 7c246d8

File tree

10 files changed

+38
-41
lines changed

10 files changed

+38
-41
lines changed
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
webserver: ':8080'
2-
logFile: log.txt
31
datasetsFile: '../internal/utils/datasets.json'
42
cache: '/home/site/data/'
5-
repositories:
6-
- name: stable
7-
type: single
8-
version: "3.12"
9-
url: http://192.168.129.2:8529
10-
- name: stable
11-
type: cluster
12-
version: "3.12"
13-
url: http://192.168.129.3:8529
3+
debug: false
4+
repositories: []

toolchain/arangoproxy/cmd/main.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,12 @@ func init() {
3030
models.Conf.Override = strings.ReplaceAll(override, ",", "|")
3131

3232
models.Logger.Printf(startupBanner)
33-
models.Logger.Printf("./arangoproxy -help for help usage\n\n")
34-
models.Logger.Printf("Init Setup\n")
3533

36-
if help {
37-
models.Logger.Printf("Usage: ...\n")
38-
os.Exit(0)
39-
}
40-
models.Logger.Printf("Configuration:\n%v\n", models.Conf)
41-
42-
models.Logger.Printf("Setup Done\n---------\n")
34+
models.Logger.Printf("Configuration:\n%s\n", models.Conf.String())
4335

4436
}
4537

4638
func main() {
47-
models.Logger.Printf("Available endpoints:\n - /js\n - /aql\n - /curl\n - /openapi\n")
48-
models.Logger.Printf("Starting Server at :8080\n")
49-
5039
if useServers {
5140
internal.InitRepositories()
5241
models.Logger.Printf("[INIT] Repositories Init Done")

toolchain/arangoproxy/internal/arangosh/arangosh.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func ExecRoutine(example chan map[string]interface{}, outChannel chan string) {
3232

3333
func Exec(exampleName string, code, filepath string, repository models.Repository) (output string) {
3434
code = format.AdjustCodeForArangosh(code)
35+
models.Logger.Debug("[%s] [arangosh.Exec] Injecting Code:\n%s", exampleName, code)
3536

3637
cmd := []byte(code)
3738
_, err := repository.StdinPipe.Write(cmd)

toolchain/arangoproxy/internal/cache.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func SaveCachedExampleResponse(chnl chan map[string]interface{}) error {
1919
entryName := fmt.Sprintf("%s_%s", exampleResponse.Options.Name, exampleResponse.Options.Type)
2020
responseHash, err := utils.EncodeToBase64(exampleResponse)
2121

22+
models.Logger.Debug("[%s] Saving To Cache:\nInput Hash: %s\nOutput Hash:%s", entryName, requestHash, responseHash)
23+
2224
newCacheEntry := make(map[string]map[string]string)
2325
newCacheEntry[entryName] = make(map[string]string)
2426
newCacheEntry[entryName]["request"] = requestHash
@@ -34,7 +36,12 @@ func SaveCachedExampleResponse(chnl chan map[string]interface{}) error {
3436
cache[entryName] = newCacheEntry[entryName]
3537
cacheJson, _ := json.MarshalIndent(cache, "", "\t")
3638
err = os.WriteFile(cacheFilepath, cacheJson, 0644)
37-
39+
if err != nil {
40+
models.Logger.Printf("[%s] [ERROR] Error saving cache: %s", err.Error())
41+
models.Logger.Summary("<li><error code=9><strong>%s</strong><strong> ERROR Saving Cache: %s</strong></error>", exampleResponse.Options.Name, err.Error())
42+
} else {
43+
models.Logger.Debug("[%s] Cache Saved", err)
44+
}
3845
}
3946
}
4047
}

toolchain/arangoproxy/internal/init.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
func InitRepositories() {
1414
models.Repositories = make(map[string]models.Repository)
15-
fmt.Printf("Repositories found in config file %s\n", models.Conf.Repositories)
1615
var wg sync.WaitGroup
1716

1817
for _, repo := range models.Conf.Repositories {
@@ -55,5 +54,5 @@ func openRepoStream(repository *models.Repository) {
5554

5655
repository.StdinPipe = stdin
5756
repository.StdoutPipe = stdout
58-
models.Logger.Printf("[openRepoStream] Done - Streams: \n%s\n%s", repository.StdinPipe, repository.StdoutPipe)
57+
models.Logger.Printf("[openRepoStream] Opened Stream for %s succesfully", repository.Version)
5958
}

toolchain/arangoproxy/internal/models/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
import (
4+
"fmt"
45
"os"
56

67
"gopkg.in/yaml.v3"
@@ -10,6 +11,7 @@ type Config struct {
1011
Repositories []Repository `yaml:"repositories"` // ArangoDB instances
1112
Cache string `yaml:"cache"` // Cache configuration
1213
Datasets string `yaml:"datasetsFile"`
14+
Debug bool `yaml:"debug"`
1315
Override string `yaml:"-"`
1416
}
1517

@@ -24,3 +26,7 @@ func LoadConfig(file string) error {
2426
err = yaml.Unmarshal(fileStream, &Conf)
2527
return err
2628
}
29+
30+
func (c Config) String() string {
31+
return fmt.Sprintf(" Cache: %s\n Datasets: %s\n Debug: %t\n Override: %s\n Repositories: %s", c.Cache, c.Datasets, c.Debug, c.Override, c.Repositories)
32+
}

toolchain/arangoproxy/internal/models/example.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,16 @@ import (
1616
"gopkg.in/yaml.v3"
1717
)
1818

19-
type ExampleType string
2019
type RenderType string
2120

2221
const (
23-
JS ExampleType = "js"
24-
AQL ExampleType = "aql"
25-
HTTP ExampleType = "http"
26-
2722
INPUT RenderType = "input"
2823
OUTPUT RenderType = "output"
2924
INPUT_OUTPUT RenderType = "input/output"
3025
)
3126

3227
// @Example represents an example request to be supplied to an arango instance
3328
type Example struct {
34-
Type ExampleType `json:"type"`
3529
Options ExampleOptions `json:"options"` // The codeblock yaml part
3630
Code string `json:"code"`
3731
Repository Repository `json:"-"`
@@ -99,7 +93,9 @@ func ParseExample(request io.Reader, headers http.Header) (Example, error) {
9993

10094
code := strings.Replace(string(decodedRequest), string(options), "", -1)
10195

102-
return Example{Type: "", Options: optionsYaml, Code: code, Base64Request: string(req)}, nil
96+
Logger.Debug("[%s] Example Information:\n%s", optionsYaml.Name, optionsYaml.String())
97+
98+
return Example{Options: optionsYaml, Code: code, Base64Request: string(req)}, nil
10399
}
104100

105101
func (r Example) String() string {
@@ -112,12 +108,7 @@ func (r Example) String() string {
112108
}
113109

114110
func (r ExampleOptions) String() string {
115-
j, err := json.Marshal(r)
116-
if err != nil {
117-
return ""
118-
}
119-
120-
return string(j)
111+
return fmt.Sprintf("Type: %s\nVersion: %s\nSaveCache: %s\nPosition: %s\n", r.Type, r.Version, r.SaveCache, r.Position)
121112
}
122113

123114
type ExampleResponse struct {

toolchain/arangoproxy/internal/models/logger.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ func init() {
1919
summaryWriter := io.Writer(logFile)
2020

2121
Logger = new(ArangoproxyLogger)
22-
Logger.logger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
22+
Logger.logger = log.New(os.Stdout, "", 0)
2323
Logger.summary = log.New(summaryWriter, "", 0)
2424
}
2525

2626
func (l *ArangoproxyLogger) Printf(s string, args ...any) {
2727
l.logger.Printf(s, args...)
2828
}
2929

30+
func (l *ArangoproxyLogger) Debug(s string, args ...any) {
31+
if Conf.Debug {
32+
l.logger.Printf("[DEBUG] "+s+"\n", args...)
33+
}
34+
}
35+
3036
func (l *ArangoproxyLogger) Summary(s string, args ...any) {
3137
s = strings.ReplaceAll(s, "\n", "\n<br>")
3238
l.summary.Printf(s+"<br>", args...)

toolchain/arangoproxy/internal/models/repository.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ type Repository struct {
1717
var Repositories map[string]Repository
1818

1919
func GetRepository(typ, version string) (Repository, error) {
20-
Logger.Printf("Executing on server %s %s", typ, version)
2120
if repository, exists := Repositories[fmt.Sprintf("%s_%s", typ, version)]; exists {
2221
return repository, nil
2322
}

toolchain/arangoproxy/internal/service/service.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type JSService struct{}
4747

4848
func (service JSService) Execute(request models.Example, cacheChannel chan map[string]interface{}, exampleChannel chan map[string]interface{}, outputChannel chan string) (res models.ExampleResponse) {
4949
repository, err := models.GetRepository(request.Options.Type, request.Options.Version)
50+
models.Logger.Debug("[%s] Choosen Repository: %s", request.Options.Name, repository.Version)
5051
if err != nil {
5152
responseMsg := fmt.Sprintf("A server for version %s has not been used during generation", request.Options.Version)
5253
res = *models.NewExampleResponse(request.Code, responseMsg, request.Options)
@@ -69,7 +70,9 @@ var curlFormatter = format.CurlFormatter{}
6970

7071
func (service CurlService) Execute(request models.Example, cacheChannel chan map[string]interface{}, exampleChannel chan map[string]interface{}, outputChannel chan string) (res models.ExampleResponse, err error) {
7172
commands := curlFormatter.FormatCommand(request.Code)
73+
7274
repository, err := models.GetRepository(request.Options.Type, request.Options.Version)
75+
models.Logger.Debug("[%s] Choosen Repository: %s", request.Options.Name, repository.Version)
7376
if err != nil {
7477
responseMsg := fmt.Sprintf("A server for version %s has not been used during generation", request.Options.Version)
7578
res = *models.NewExampleResponse(request.Code, responseMsg, request.Options)
@@ -98,7 +101,9 @@ var AQLFormatter = format.AQLFormatter{}
98101

99102
func (service AQLService) Execute(request models.Example, cacheChannel chan map[string]interface{}, exampleChannel chan map[string]interface{}, outputChannel chan string) (res models.AQLResponse) {
100103
commands := AQLFormatter.FormatRequestCode(request.Code, request.Options.BindVars)
104+
101105
repository, err := models.GetRepository(request.Options.Type, request.Options.Version)
106+
models.Logger.Debug("[%s] Choosen Repository: %s", request.Options.Name, repository.Version)
102107
if err != nil {
103108
responseMsg := fmt.Sprintf("A server for version %s has not been used during generation", request.Options.Version)
104109
res.ExampleResponse.Input, res.ExampleResponse.Options, res.ExampleResponse.Output = request.Code, request.Options, responseMsg
@@ -167,6 +172,9 @@ func (service OpenapiService) ProcessOpenapiSpec(spec map[string]interface{}, he
167172

168173
spec["version"] = version
169174

175+
specDebug, _ := json.Marshal(spec)
176+
models.Logger.Debug("[ProcessOpenapiSpec] Processing Spec %s", specDebug)
177+
170178
path := reflect.ValueOf(spec["paths"].(map[string]interface{})).MapKeys()[0].String()
171179
method := reflect.ValueOf(spec["paths"].(map[string]interface{})[path].(map[string]interface{})).MapKeys()[0].String()
172180
spec["paths"].(map[string]interface{})[path].(map[string]interface{})[method].(map[string]interface{})["summary"] = summary

0 commit comments

Comments
 (0)