Skip to content

Commit ffc7541

Browse files
authored
Adding the golangci-lint linter to the repository (jenkinsfile) (#157)
* Download and run linter as part of CI * Set golangci-lint output * Update golangci-lint output * Escape single quotes * Split sh directive * Remove unnecessary parentheses * Adding separate linter shell script * Update Jenkinsfile * Update permissions on shell script * Simplify shell script * Implement changes required by linter * Move lint to makefile * Delete golangci-lint.sh * Update Jenkinsfile to reference make lint * Use error shorthand * Add missing error handling
1 parent b864a1b commit ffc7541

19 files changed

+345
-160
lines changed

.ci/Jenkinsfile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ pipeline {
4343
withGithubNotify(context: "Lint") {
4444
withGoEnv(){
4545
dir("${BASE_DIR}/apm-lambda-extension"){
46-
sh(label: 'lint', script: '''
47-
go mod tidy && git diff --exit-code
48-
gofmt -l . | read && echo "Code differs from gofmt's style. Run 'gofmt -w .'" 1>&2 && exit 1 || true
49-
''')
46+
sh(label: 'lint-prep', script: 'go mod tidy && git diff --exit-code')
47+
sh(label: 'lint-run', script: 'make lint')
5048
sh(label: 'Go vet', script: 'go vet')
5149
}
5250
}

apm-lambda-extension/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ endif
1515

1616
export AWS_FOLDER GOARCH ARCHITECTURE
1717

18+
lint:
19+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.0
20+
golangci-lint --version
21+
golangci-lint run
22+
1823
build:
1924
GOOS=linux go build -o bin/extensions/apm-lambda-extension main.go
2025
chmod +x bin/extensions/apm-lambda-extension

apm-lambda-extension/e2e-testing/e2e_test.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package e2e_testing
1+
package e2eTesting
22

33
import (
44
"elastic/apm-lambda-extension/extension"
5-
"errors"
65
"flag"
76
"fmt"
87
"io"
@@ -40,10 +39,12 @@ func TestEndToEnd(t *testing.T) {
4039
t.Skip("Skipping E2E tests. Please set the env. variable RUN_E2E_TESTS=true if you want to run them.")
4140
}
4241

42+
extension.Log.Info("If the end-to-end tests are failing unexpectedly, please verify that Docker is running on your machine.")
43+
4344
languageName := strings.ToLower(*langPtr)
4445
supportedLanguages := []string{"nodejs", "python", "java"}
4546
if !IsStringInSlice(languageName, supportedLanguages) {
46-
ProcessError(errors.New(fmt.Sprintf("Unsupported language %s ! Supported languages are %v", languageName, supportedLanguages)))
47+
ProcessError(fmt.Errorf(fmt.Sprintf("Unsupported language %s ! Supported languages are %v", languageName, supportedLanguages)))
4748
}
4849

4950
samPath := "sam-" + languageName
@@ -73,22 +74,22 @@ func TestEndToEnd(t *testing.T) {
7374

7475
resultsChan := make(chan string, 1)
7576

76-
uuid := runTestWithTimer(samPath, samServiceName, ts.URL, *rebuildPtr, *timerPtr, resultsChan)
77-
extension.Log.Infof("UUID generated during the test : %s", uuid)
78-
if uuid == "" {
77+
testUuid := runTestWithTimer(samPath, samServiceName, ts.URL, *rebuildPtr, *timerPtr, resultsChan)
78+
extension.Log.Infof("UUID generated during the test : %s", testUuid)
79+
if testUuid == "" {
7980
t.Fail()
8081
}
8182
extension.Log.Infof("Querying the mock server for transaction bound to %s...", samServiceName)
82-
assert.True(t, strings.Contains(mockAPMServerLog, uuid))
83+
assert.True(t, strings.Contains(mockAPMServerLog, testUuid))
8384
}
8485

8586
func runTestWithTimer(path string, serviceName string, serverURL string, buildFlag bool, lambdaFuncTimeout int, resultsChan chan string) string {
8687
timer := time.NewTimer(time.Duration(lambdaFuncTimeout) * time.Second * 2)
8788
defer timer.Stop()
8889
go runTest(path, serviceName, serverURL, buildFlag, lambdaFuncTimeout, resultsChan)
8990
select {
90-
case uuid := <-resultsChan:
91-
return uuid
91+
case testUuid := <-resultsChan:
92+
return testUuid
9293
case <-timer.C:
9394
return ""
9495
}
@@ -132,7 +133,9 @@ func retrieveJavaAgent(samJavaPath string, version string) {
132133
resp, err := http.Get(fmt.Sprintf("https://github.com/elastic/apm-agent-java/releases/download/v%[1]s/elastic-apm-java-aws-lambda-layer-%[1]s.zip", version))
133134
ProcessError(err)
134135
defer resp.Body.Close()
135-
io.Copy(out, resp.Body)
136+
if _, err = io.Copy(out, resp.Body); err != nil {
137+
extension.Log.Errorf("Could not retrieve java agent : %v", err)
138+
}
136139

137140
// Unzip archive and delete it
138141
extension.Log.Info("Unzipping Java Agent archive...")
@@ -147,6 +150,8 @@ func changeJavaAgentPermissions(samJavaPath string) {
147150
agentFiles, err := ioutil.ReadDir(agentFolderPath)
148151
ProcessError(err)
149152
for _, f := range agentFiles {
150-
os.Chmod(filepath.Join(agentFolderPath, f.Name()), 0755)
153+
if err = os.Chmod(filepath.Join(agentFolderPath, f.Name()), 0755); err != nil {
154+
extension.Log.Errorf("Could not change java agent permissions : %v", err)
155+
}
151156
}
152157
}

apm-lambda-extension/e2e-testing/e2e_util.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package e2e_testing
1+
package e2eTesting
22

33
import (
44
"archive/zip"
@@ -35,7 +35,9 @@ func RunCommandInDir(command string, args []string, dir string) {
3535
e.Dir = dir
3636
stdout, _ := e.StdoutPipe()
3737
stderr, _ := e.StderrPipe()
38-
e.Start()
38+
if err := e.Start(); err != nil {
39+
extension.Log.Errorf("Could not retrieve run %s : %v", command, err)
40+
}
3941
scannerOut := bufio.NewScanner(stdout)
4042
for scannerOut.Scan() {
4143
m := scannerOut.Text()
@@ -46,17 +48,16 @@ func RunCommandInDir(command string, args []string, dir string) {
4648
m := scannerErr.Text()
4749
extension.Log.Tracef(m)
4850
}
49-
e.Wait()
51+
if err := e.Wait(); err != nil {
52+
extension.Log.Errorf("Could not wait for the execution of %s : %v", command, err)
53+
}
5054

5155
}
5256

5357
// FolderExists returns true if the specified folder exists, and false else.
5458
func FolderExists(path string) bool {
5559
_, err := os.Stat(path)
56-
if err == nil {
57-
return true
58-
}
59-
return false
60+
return err == nil
6061
}
6162

6263
// ProcessError is a shorthand function to handle fatal errors, the idiomatic Go way.
@@ -75,7 +76,10 @@ func Unzip(archivePath string, destinationFolderPath string) {
7576
defer openedArchive.Close()
7677

7778
// Permissions setup
78-
os.MkdirAll(destinationFolderPath, 0755)
79+
err = os.MkdirAll(destinationFolderPath, 0755)
80+
if err != nil {
81+
extension.Log.Errorf("Could not create folders required to unzip, %v", err)
82+
}
7983

8084
// Closure required, so that Close() calls do not pile up when unzipping archives with a lot of files
8185
extractAndWriteFile := func(f *zip.File) error {
@@ -84,7 +88,7 @@ func Unzip(archivePath string, destinationFolderPath string) {
8488
return err
8589
}
8690
defer func() {
87-
if err := rc.Close(); err != nil {
91+
if err = rc.Close(); err != nil {
8892
panic(err)
8993
}
9094
}()
@@ -97,9 +101,13 @@ func Unzip(archivePath string, destinationFolderPath string) {
97101
}
98102

99103
if f.FileInfo().IsDir() {
100-
os.MkdirAll(path, f.Mode())
104+
if err := os.MkdirAll(path, f.Mode()); err != nil {
105+
extension.Log.Errorf("Could not unzip folder : %v", err)
106+
}
101107
} else {
102-
os.MkdirAll(filepath.Dir(path), f.Mode())
108+
if err = os.MkdirAll(filepath.Dir(path), f.Mode()); err != nil {
109+
extension.Log.Errorf("Could not unzip file : %v", err)
110+
}
103111
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
104112
ProcessError(err)
105113
defer f.Close()
@@ -135,7 +143,7 @@ func GetDecompressedBytesFromRequest(req *http.Request) ([]byte, error) {
135143

136144
switch req.Header.Get("Content-Encoding") {
137145
case "deflate":
138-
reader := bytes.NewReader([]byte(rawBytes))
146+
reader := bytes.NewReader(rawBytes)
139147
zlibreader, err := zlib.NewReader(reader)
140148
if err != nil {
141149
return nil, fmt.Errorf("could not create zlib.NewReader: %v", err)
@@ -146,7 +154,7 @@ func GetDecompressedBytesFromRequest(req *http.Request) ([]byte, error) {
146154
}
147155
return bodyBytes, nil
148156
case "gzip":
149-
reader := bytes.NewReader([]byte(rawBytes))
157+
reader := bytes.NewReader(rawBytes)
150158
zlibreader, err := gzip.NewReader(reader)
151159
if err != nil {
152160
return nil, fmt.Errorf("could not create gzip.NewReader: %v", err)

apm-lambda-extension/extension/apm_server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ var ApmServerTransportState = ApmServerTransportStateType{
5656
ReconnectionCount: -1,
5757
}
5858

59-
// todo: can this be a streaming or streaming style call that keeps the
60-
// connection open across invocations?
6159
func PostToApmServer(client *http.Client, agentData AgentData, config *extensionConfig, ctx context.Context) error {
60+
// todo: can this be a streaming or streaming style call that keeps the
61+
// connection open across invocations?
6262
if !IsTransportStatusHealthyOrPending() {
6363
return errors.New("transport status is unhealthy")
6464
}

0 commit comments

Comments
 (0)