Fixed local logging and added call stack to errors (#14)

* Fixed local logs file size

* Added call stack to errors

* addressed PR comments
This commit is contained in:
Bhaskar Brahma 2019-04-30 16:29:59 -07:00 коммит произвёл GitHub
Родитель c86d73656b
Коммит 0f0878740b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 46 добавлений и 30 удалений

3
.gitattributes поставляемый Normal file
Просмотреть файл

@ -0,0 +1,3 @@
# Set the default behavior, in case people don't have core.autocrlf set.
*.go text=auto

3
.gitignore поставляемый
Просмотреть файл

@ -11,3 +11,6 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
.idea
#log files from tests
*.log

Просмотреть файл

@ -5,6 +5,8 @@ package configuration
import (
"encoding/json"
"fmt"
"github.com/Azure/azure-extension-foundation/errorhelper"
"io/ioutil"
"os"
)
@ -68,7 +70,7 @@ func GetConfiguration() Configuration {
var readDiskConfiguration = func(path string) ([]byte, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
return nil, errorhelper.AddStackToError(err)
}
return content, nil
@ -77,12 +79,12 @@ var readDiskConfiguration = func(path string) ([]byte, error) {
var setConfiguration = func(config *Configuration) {
configuration, err := SerializeConfiguration(config)
if err != nil {
panic("unable to serialize configuration from environment")
panic(fmt.Sprintf("unable to serialize configuration from environment\n%+v", err))
}
err = os.Setenv(EnvironmentConfigurationKey, string(configuration))
if err != nil {
panic("unable to set configuration to environment")
panic(fmt.Sprintf("unable to set configuration to environment\n%+v", err))
}
}
@ -97,18 +99,20 @@ var getEnvironmentConfiguration = func() Configuration {
if exists {
err := DeserializeConfiguration([]byte(value), &configuration)
if err != nil {
panic("unable to deserialize configuration from environment")
panic(fmt.Sprintf("unable to deserialize configuration from environment\n%+v", err))
}
}
return configuration
}
var SerializeConfiguration = func(configuration *Configuration) ([]byte, error) {
return json.Marshal(configuration)
value, err := json.Marshal(configuration)
return value, errorhelper.AddStackToError(err)
}
var DeserializeConfiguration = func(data []byte, configuration *Configuration) error {
return json.Unmarshal(data, &configuration)
err := json.Unmarshal(data, &configuration)
return errorhelper.AddStackToError(err)
}
var getDefaultConfiguration = func() Configuration {

Просмотреть файл

@ -47,14 +47,14 @@ func IsSignatureValid(signedFilePath string, outputFilePath string, keyrings []s
err := cmd.CommandError
if err != nil {
// TODO: trace signature validation success
// TODO: trace signature validation failure
return false, NewGpgExecuteError(err.Error())
}
if ret != 0 {
return false, NewGpgExecuteError(fmt.Sprintf("Gpg execution returned code: %v", ret))
}
return true, nil
// TODO: trace signature validation failure
// TODO: trace signature validation success
}
//No GPG keyring was able to verify the signed file
return false, nil

Просмотреть файл

@ -6,6 +6,7 @@ package jrds
import (
"encoding/json"
"fmt"
"github.com/Azure/azure-extension-foundation/errorhelper"
"github.com/Azure/azure-extension-foundation/httputil"
"time"
)
@ -177,7 +178,7 @@ func (jrds *JrdsClient) issuePostRequest(url string, payload interface{}, out in
code, _, err := jrds.client.Post(url, headers, body)
if err != nil {
return NewRequestError(fmt.Sprintf("request error %v : %v\n", url, code))
NewRequestError(fmt.Sprintf("request error %v : %v\n%+v", url, code, err))
}
if code == 401 {
@ -185,7 +186,8 @@ func (jrds *JrdsClient) issuePostRequest(url string, payload interface{}, out in
}
if code != 200 {
return NewRequestInvalidStatusError(fmt.Sprintf("invalid return code for %v : %v\n", url, code))
return NewRequestInvalidStatusError(
errorhelper.NewErrorWithStack(fmt.Sprintf("invalid return code for %v : %v\n", url, code)).Error())
}
if out != nil {
@ -201,7 +203,7 @@ func (jrds *JrdsClient) issueGetRequest(url string, out interface{}) error {
code, body, err := jrds.client.Get(url, jrds.getDefaultHeaders())
if err != nil {
return NewRequestError(fmt.Sprintf("request error %v : %v\n", url, code))
return NewRequestError(fmt.Sprintf("request error %v : %v\n%+v", url, code, err))
}
if code == 401 {
@ -209,12 +211,13 @@ func (jrds *JrdsClient) issueGetRequest(url string, out interface{}) error {
}
if code != 200 {
return NewRequestInvalidStatusError(fmt.Sprintf("invalid return code for %v : %v\n", url, code))
return NewRequestInvalidStatusError(
errorhelper.NewErrorWithStack(fmt.Sprintf("invalid return code for %v : %v\n", url, code)).Error())
}
if out != nil {
if err := json.Unmarshal(body, out); err != nil {
return fmt.Errorf("failed to unmarshal request response: %v", err)
return fmt.Errorf("failed to unmarshal request response: %+v", err)
}
}

Просмотреть файл

@ -54,7 +54,8 @@ var writeToDisk = func(msg string) {
}
// rotate if needed; only keep 2 iteration of log file
if info.Size() > int64(math.Pow(1, 7)) {
// around 10 MB
if info.Size() > int64(math.Pow(10, 7)) {
// rotate
file.Close()
os.Rename(logPath, fmt.Sprintf("%v.1", logPath))

Просмотреть файл

@ -9,6 +9,7 @@ import (
"github.com/Azure/azure-automation-go-worker/internal/jrds"
"github.com/Azure/azure-automation-go-worker/internal/tracer"
"github.com/Azure/azure-automation-go-worker/main/sandbox/runtime"
"github.com/Azure/azure-extension-foundation/errorhelper"
"os"
"path/filepath"
"time"
@ -49,7 +50,7 @@ type jrdsClient interface {
func NewJob(sandboxId string, jobData jrds.JobData, jrdsClient jrdsClient) Job {
workingDirectory := filepath.Join(configuration.GetWorkingDirectory(), *jobData.JobId)
err := os.MkdirAll(workingDirectory, 0750)
panicOnError("Unable to create job working directory", err)
panicOnError("Unable to create job working directory", errorhelper.AddStackToError(err))
return Job{
Id: *jobData.JobId,

Просмотреть файл

@ -3,7 +3,9 @@
package runtime
import "fmt"
import (
"github.com/Azure/azure-extension-foundation/errorhelper"
)
const (
pythonExtension Extension = "py"
@ -43,7 +45,7 @@ var GetLanguage = func(definitionKind DefinitionKind) (Language, error) {
language = Language{extension: bashExtension, interpreter: getBashInterpreter()}
break
default:
return Language{}, fmt.Errorf("unsupported language")
return Language{}, errorhelper.NewErrorWithStack("unsupported language")
}
return language, nil
}

Просмотреть файл

@ -6,6 +6,7 @@ package runtime
import (
"github.com/Azure/azure-automation-go-worker/internal/jrds"
"github.com/Azure/azure-automation-go-worker/pkg/executil"
"github.com/Azure/azure-extension-foundation/errorhelper"
"os"
"path/filepath"
)
@ -35,11 +36,7 @@ func NewRuntime(language Language, runbook Runbook, jobData jrds.JobData, workin
func (runtime *Runtime) Initialize() error {
runbookPath := getRunbookPathOnDisk(runtime.workingDirectory, runtime.runbook)
err := writeRunbookToDisk(runbookPath, runtime.runbook)
if err != nil {
return err
}
return nil
return errorhelper.AddStackToError(err)
}
func (runtime *Runtime) IsSupported() bool {
@ -94,12 +91,12 @@ var writeRunbookToDisk = func(path string, runbook Runbook) error {
const permission = 0640
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, permission)
if err != nil {
return err
return errorhelper.AddStackToError(err)
}
_, err = file.Write([]byte(runbook.Definition))
if err != nil {
return err
return errorhelper.AddStackToError(err)
}
file.Close()

Просмотреть файл

@ -5,6 +5,7 @@ import (
"github.com/Azure/azure-automation-go-worker/internal/configuration"
"github.com/Azure/azure-automation-go-worker/internal/tracer"
"github.com/Azure/azure-automation-go-worker/pkg/executil"
"github.com/Azure/azure-extension-foundation/errorhelper"
"os"
"path/filepath"
"strings"
@ -41,7 +42,7 @@ func (s *Sandbox) CreateBaseDirectory() error {
const permission = 0750
err := os.MkdirAll(s.workingDirectory, permission) // TODO: change sb permission
if err != nil {
return err
return errorhelper.AddStackToError(err)
}
return nil
@ -78,7 +79,7 @@ func (s *Sandbox) Cleanup() error {
err := os.RemoveAll(s.workingDirectory)
if err != nil {
return err
return errorhelper.AddStackToError(err)
}
return nil

Просмотреть файл

@ -4,7 +4,7 @@
package executil
import (
"fmt"
"github.com/Azure/azure-extension-foundation/errorhelper"
"io"
"os/exec"
)
@ -44,7 +44,7 @@ func NewAsyncCommand(stdout func(str string), stderr func(str string), workingDi
func (cmd *AsyncCommand) Kill() error {
if cmd.cmd == nil {
return fmt.Errorf("nil cmd")
return errorhelper.NewErrorWithStack("nil cmd")
}
return cmd.cmd.Process.Kill()
return errorhelper.AddStackToError(cmd.cmd.Process.Kill())
}

Просмотреть файл

@ -5,6 +5,7 @@ package executil
import (
"bufio"
"github.com/Azure/azure-extension-foundation/errorhelper"
"io"
"os/exec"
"syscall"
@ -36,7 +37,7 @@ func executeAsyncCommand(command *AsyncCommand) error {
err := command.cmd.Start()
if err != nil {
return err
return errorhelper.AddStackToError(err)
}
command.IsRunning = true

Просмотреть файл