Skip to content

Commit 24d11a3

Browse files
committed
feat: ⚡ add sample for new relic with goroutine
add sample for new relic with goroutine add sample for new relic with goroutine
0 parents  commit 24d11a3

File tree

10 files changed

+425
-0
lines changed

10 files changed

+425
-0
lines changed

.github/workflows/go.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
jobs:
11+
build_and_test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Setup go-task
15+
uses: pnorton5432/setup-task@v1
16+
with:
17+
task-version: 3.29.1
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
- name: Setup Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version: 'stable'
24+
check-latest: true
25+
- name: Task Build
26+
run: task build
27+
- name: Task Build for mage
28+
run: task build-gg
29+
- name: Test with gg build
30+
run: ./gg build

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Dependency directories (remove the comment below to include it)
18+
# vendor/
19+
20+
# Go workspace file
21+
go.work
22+
go.work.sum
23+
24+
# env file
25+
.env
26+
gg
27+
mage
28+
bin

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# golang-new-relic-go-routine-sample
2+
3+
This repository is demo how to use new-relic to instrument go application that contain go routines
4+
5+
## logic
6+
7+
```golan
8+
9+
```

Taskfile.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
version: '3'
2+
3+
tasks:
4+
default:
5+
cmds:
6+
- echo "This is task cmd"
7+
silent: true
8+
9+
build:
10+
cmds:
11+
- CGO_ENABLED=0 GOOS=linux go build -o bin/main cmd/main.go
12+
silent: true
13+
run:
14+
cmds:
15+
- ./bin/main
16+
deps:
17+
- build
18+
silent: true
19+
20+
build-mage:
21+
cmds:
22+
- CGO_ENABLED=0 GOOS=linux go build -o ./mage mage-tools/mage.go
23+
silent: true
24+
25+
build-gg:
26+
cmds:
27+
- ./mage -d mage-tools -compile ../gg
28+
deps:
29+
- build-mage
30+
silent: true
31+
32+
coverage:
33+
cmds:
34+
- go test -v -cover ./...
35+
silent: true
36+
test:
37+
cmds:
38+
- go test -v ./...
39+
silent: true
40+

cmd/main.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"log"
7+
"math/rand"
8+
"net/http"
9+
"strconv"
10+
"sync"
11+
"time"
12+
13+
"github.com/leetcode-golang-classroom/golang-new-relic-go-routine-sample/internal/config"
14+
"github.com/newrelic/go-agent/v3/newrelic"
15+
)
16+
17+
var (
18+
randomer *rand.Rand
19+
)
20+
21+
// init set initial values for variables used in the function
22+
func init() {
23+
randomer = rand.New(rand.NewSource(time.Now().UnixNano()))
24+
}
25+
26+
// Workshop > You may track errors using the Transaction.NoticeError method.
27+
// The easiest way to get started with NoticeError is to use errors based on Go's standard error interface.
28+
// https://github.com/newrelic/go-agent/blob/master/GUIDE.md#error-reporting
29+
func noticeErrorWithAttributes(w http.ResponseWriter, r *http.Request) {
30+
io.WriteString(w, "noticing an error!")
31+
32+
txn := newrelic.FromContext(r.Context())
33+
txn.NoticeError(newrelic.Error{
34+
Message: "uh oh. something went very wrong",
35+
Class: "errors are aggregated by class",
36+
Attributes: map[string]interface{}{
37+
"important_number": 97232,
38+
"relevant_string": "classError",
39+
},
40+
})
41+
println("Oops, there is an error!")
42+
}
43+
44+
func async(w http.ResponseWriter, r *http.Request) {
45+
// To access the transaction in your handler, use the newrelic.FromContext API.
46+
txn := newrelic.FromContext(r.Context())
47+
// This WaitGroup is used to wait for all the goroutines to finish.
48+
wg := &sync.WaitGroup{}
49+
println("goRoutines created!")
50+
51+
for i := 1; i < 9; i++ {
52+
wg.Add(1)
53+
i := i
54+
55+
// Workshop > trace asynchronous applications
56+
// The Transaction.NewGoroutine() allows transactions to create segments in multiple goroutines.
57+
// https://docs.newrelic.com/docs/apm/agents/go-agent/features/trace-asynchronous-applications
58+
go func(txn *newrelic.Transaction) {
59+
defer wg.Done()
60+
defer txn.StartSegment("goroutine" + strconv.Itoa(i)).End()
61+
println("goRoutine " + strconv.Itoa(i))
62+
63+
randomDelay := randomer.Intn(500)
64+
time.Sleep(time.Duration(randomDelay) * time.Millisecond)
65+
}(txn.NewGoroutine())
66+
}
67+
68+
// Workshop > Ensure the WaitGroup is done
69+
segment := txn.StartSegment("WaitGroup")
70+
wg.Wait()
71+
segment.End()
72+
w.Write([]byte("goRoutines success!"))
73+
}
74+
75+
func main() {
76+
nrApp, nrErr := newrelic.NewApplication(
77+
newrelic.ConfigAppName(config.AppConfig.AppName),
78+
newrelic.ConfigLicense(config.AppConfig.NewRelicLicenseKey),
79+
// newrelic.ConfigDebugLogger(os.Stdout),
80+
)
81+
82+
if nrErr != nil {
83+
log.Fatal(nrErr)
84+
}
85+
86+
if waitErr := nrApp.WaitForConnection(5 * time.Second); waitErr != nil {
87+
log.Fatalf("nrApp.WaitForConnection failed %v", waitErr)
88+
}
89+
90+
// add route for async call
91+
http.HandleFunc(newrelic.WrapHandleFunc(nrApp, "/error", noticeErrorWithAttributes))
92+
http.HandleFunc(newrelic.WrapHandleFunc(nrApp, "/async", async))
93+
err := http.ListenAndServe(fmt.Sprintf(":%v", config.AppConfig.Port), nil)
94+
if err != nil {
95+
log.Fatal(err)
96+
}
97+
// Wait for shut down to ensure data gets flushed
98+
nrApp.Shutdown(5 * time.Second)
99+
}

go.mod

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module github.com/leetcode-golang-classroom/golang-new-relic-go-routine-sample
2+
3+
go 1.22.4
4+
5+
require (
6+
github.com/magefile/mage v1.15.0
7+
github.com/spf13/viper v1.19.0
8+
)
9+
10+
require (
11+
golang.org/x/net v0.25.0 // indirect
12+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
13+
google.golang.org/grpc v1.65.0 // indirect
14+
google.golang.org/protobuf v1.34.2 // indirect
15+
)
16+
17+
require (
18+
github.com/fsnotify/fsnotify v1.7.0 // indirect
19+
github.com/hashicorp/hcl v1.0.0 // indirect
20+
github.com/magiconair/properties v1.8.7 // indirect
21+
github.com/mitchellh/mapstructure v1.5.0 // indirect
22+
github.com/newrelic/go-agent/v3 v3.35.1
23+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
24+
github.com/sagikazarmark/locafero v0.4.0 // indirect
25+
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
26+
github.com/sourcegraph/conc v0.3.0 // indirect
27+
github.com/spf13/afero v1.11.0 // indirect
28+
github.com/spf13/cast v1.6.0 // indirect
29+
github.com/spf13/pflag v1.0.5 // indirect
30+
github.com/subosito/gotenv v1.6.0 // indirect
31+
go.uber.org/atomic v1.9.0 // indirect
32+
go.uber.org/multierr v1.9.0 // indirect
33+
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
34+
golang.org/x/sys v0.20.0 // indirect
35+
golang.org/x/text v0.15.0 // indirect
36+
gopkg.in/ini.v1 v1.67.0 // indirect
37+
gopkg.in/yaml.v3 v3.0.1 // indirect
38+
)

go.sum

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
4+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
6+
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
7+
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
8+
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
9+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
10+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
11+
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
12+
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
13+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
14+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
15+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
16+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
17+
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
18+
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
19+
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
20+
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
21+
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
22+
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
23+
github.com/newrelic/go-agent/v3 v3.35.1 h1:N43qBNDILmnwLDCSfnE1yy6adyoVEU95nAOtdUgG4vA=
24+
github.com/newrelic/go-agent/v3 v3.35.1/go.mod h1:GNTda53CohAhkgsc7/gqSsJhDZjj8vaky5u+vKz7wqM=
25+
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
26+
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
27+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
28+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
29+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
30+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
31+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
32+
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
33+
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
34+
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
35+
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
36+
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
37+
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
38+
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
39+
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
40+
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
41+
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
42+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
43+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
44+
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
45+
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
46+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
47+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
48+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
49+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
50+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
51+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
52+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
53+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
54+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
55+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
56+
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
57+
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
58+
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
59+
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
60+
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
61+
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
62+
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
63+
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
64+
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
65+
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
66+
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
67+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
68+
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
69+
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
70+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 h1:Zy9XzmMEflZ/MAaA7vNcoebnRAld7FsPW1EeBB7V0m8=
71+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
72+
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
73+
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
74+
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
75+
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
76+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
77+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
78+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
79+
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
80+
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
81+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
82+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
83+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/config/config.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package config
2+
3+
import (
4+
"log"
5+
6+
"github.com/spf13/viper"
7+
)
8+
9+
type Config struct {
10+
NewRelicKey string `mapstructure:"NEW_RELIC_KEY"`
11+
NewRelicLicenseKey string `mapstructure:"NEW_RELIC_LICENSE_KEY"`
12+
AppName string `mapstructure:"APP_NAME"`
13+
Port string `mapstructure:"PORT"`
14+
}
15+
16+
var AppConfig *Config
17+
18+
func init() {
19+
v := viper.New()
20+
v.AddConfigPath(".")
21+
v.SetConfigName(".env")
22+
v.SetConfigType("env")
23+
v.AutomaticEnv()
24+
FailOnError(v.BindEnv("NEW_RELIC_KEY"), "failed to bind NEW_RELIC_KEY")
25+
FailOnError(v.BindEnv("NEW_RELIC_LICENSE_KEY"), "failed to bind NEW_RELIC_LICENSE_KEY")
26+
FailOnError(v.BindEnv("APP_NAME"), "failed to bind APP_NAME")
27+
FailOnError(v.BindEnv("PORT"), "failed to bind PORT")
28+
err := v.ReadInConfig()
29+
if err != nil {
30+
log.Println("Load from environment variable")
31+
}
32+
err = v.Unmarshal(&AppConfig)
33+
if err != nil {
34+
FailOnError(err, "Failed to read enivronment")
35+
}
36+
}
37+
38+
func FailOnError(err error, msg string) {
39+
if err != nil {
40+
log.Fatalf("%s: %s", msg, err)
41+
}
42+
}

mage-tools/mage.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build ignore
2+
// +build ignore
3+
4+
package main
5+
6+
import (
7+
"os"
8+
9+
"github.com/magefile/mage/mage"
10+
)
11+
12+
func main() { os.Exit(mage.Main()) }

0 commit comments

Comments
 (0)