Skip to content

Commit 884c09e

Browse files
committed
⚡feat: ✨ add new relic segment sample
add new relic segment sample add github action for build
0 parents  commit 884c09e

File tree

10 files changed

+552
-0
lines changed

10 files changed

+552
-0
lines changed

.github/workflows/go.yml

+30
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

+28
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+
bin
27+
mage
28+
gg

README.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# golang-new-relic-transaction-segment-sample
2+
3+
This repository is demo how to use new relic and segment to profile golang application
4+
5+
## implementation
6+
7+
```golang
8+
package main
9+
10+
import (
11+
"errors"
12+
"fmt"
13+
"log"
14+
"math/rand"
15+
"os"
16+
"time"
17+
18+
"github.com/leetcode-golang-classroom/golang-new-relic-transaction-segment-sample/internal/config"
19+
"github.com/newrelic/go-agent/v3/newrelic"
20+
)
21+
22+
var (
23+
nrApp *newrelic.Application
24+
nrErr error
25+
randomer *rand.Rand
26+
)
27+
28+
func main() {
29+
nrApp, nrErr = newrelic.NewApplication(
30+
newrelic.ConfigAppName(config.AppConfig.AppName),
31+
newrelic.ConfigLicense(config.AppConfig.NewRelicLicenseKey),
32+
newrelic.ConfigDebugLogger(os.Stdout),
33+
34+
// Workshop > Additional configuration via a config function
35+
// https://docs.newrelic.com/docs/apm/agents/go-agent/configuration/go-agent-configuration
36+
func(cfg *newrelic.Config) {
37+
cfg.Enabled = true
38+
cfg.DistributedTracer.Enabled = true
39+
cfg.Labels = map[string]string{
40+
"Env": "Dev",
41+
"Function": "Greetings",
42+
"Platform": "My Machine",
43+
"Team": "API",
44+
}
45+
},
46+
)
47+
if nrErr != nil {
48+
log.Fatalln("unable to start NR instrumentation -", nrErr)
49+
}
50+
51+
if waitErr := nrApp.WaitForConnection(5 * time.Second); waitErr != nil {
52+
log.Fatal("nrApp.WaitForConnection failed")
53+
}
54+
55+
// Request a greeting message
56+
message, err := Hello("Tony Stark")
57+
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
fmt.Println(message)
62+
63+
nrApp.Shutdown(5 * time.Second)
64+
}
65+
66+
// init set initial values for variables used in the function
67+
func init() {
68+
randomer = rand.New(rand.NewSource(time.Now().UnixNano()))
69+
}
70+
71+
// Hello returns a greeting for the name person
72+
func Hello(name string) (string, error) {
73+
// Monitor a transaction
74+
nrTxnTracer := nrApp.StartTransaction("Hello")
75+
defer nrTxnTracer.End()
76+
77+
// If no name was given, return an error with a message
78+
if name == "" {
79+
return name, errors.New("empty name")
80+
}
81+
82+
// Create a message using a random format
83+
message := fmt.Sprintf(randomFormat(), name)
84+
85+
// custom attributes by using method in a transaction
86+
nrTxnTracer.AddAttribute("message", message)
87+
return message, nil
88+
}
89+
90+
// randomFormat returns one of a set of greeting messages
91+
func randomFormat() string {
92+
// Set up transaction to trace
93+
nrTxnTracer := nrApp.StartTransaction("randomFormat")
94+
defer nrTxnTracer.End()
95+
96+
randomDelayOuter := randomer.Intn(40)
97+
time.Sleep(time.Duration(randomDelayOuter) * time.Microsecond)
98+
99+
// create a segment for Formats
100+
nrSegment := nrTxnTracer.StartSegment("Formats")
101+
102+
// Random sleep to simulate delays
103+
randomDelayInner := randomer.Intn(80)
104+
time.Sleep(time.Duration(randomDelayInner) * time.Microsecond)
105+
106+
// A slice of message formats.
107+
formats := []string{
108+
"Hi, %v. Welcome!",
109+
"Great to see you, %v!",
110+
"Good day, %v! Well met!",
111+
"%v! Hi there!",
112+
"Greetings %v!",
113+
"Hello there, %v!",
114+
}
115+
116+
// end a segment
117+
nrSegment.End()
118+
// Return a randomly selected message format by specifying
119+
// a random index for the slice of formats.
120+
return formats[randomer.Intn(len(formats))]
121+
}
122+
123+
```

Taskfile.yml

+40
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

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"log"
7+
"math/rand"
8+
"os"
9+
"time"
10+
11+
"github.com/leetcode-golang-classroom/golang-new-relic-transaction-segment-sample/internal/config"
12+
"github.com/newrelic/go-agent/v3/newrelic"
13+
)
14+
15+
var (
16+
nrApp *newrelic.Application
17+
nrErr error
18+
randomer *rand.Rand
19+
)
20+
21+
func main() {
22+
nrApp, nrErr = newrelic.NewApplication(
23+
newrelic.ConfigAppName(config.AppConfig.AppName),
24+
newrelic.ConfigLicense(config.AppConfig.NewRelicLicenseKey),
25+
newrelic.ConfigDebugLogger(os.Stdout),
26+
27+
// Workshop > Additional configuration via a config function
28+
// https://docs.newrelic.com/docs/apm/agents/go-agent/configuration/go-agent-configuration
29+
func(cfg *newrelic.Config) {
30+
cfg.Enabled = true
31+
cfg.DistributedTracer.Enabled = true
32+
cfg.Labels = map[string]string{
33+
"Env": "Dev",
34+
"Function": "Greetings",
35+
"Platform": "My Machine",
36+
"Team": "API",
37+
}
38+
},
39+
)
40+
if nrErr != nil {
41+
log.Fatalln("unable to start NR instrumentation -", nrErr)
42+
}
43+
44+
if waitErr := nrApp.WaitForConnection(5 * time.Second); waitErr != nil {
45+
log.Fatal("nrApp.WaitForConnection failed")
46+
}
47+
48+
// Request a greeting message
49+
message, err := Hello("Tony Stark")
50+
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
fmt.Println(message)
55+
56+
nrApp.Shutdown(5 * time.Second)
57+
}
58+
59+
// init set initial values for variables used in the function
60+
func init() {
61+
randomer = rand.New(rand.NewSource(time.Now().UnixNano()))
62+
}
63+
64+
// Hello returns a greeting for the name person
65+
func Hello(name string) (string, error) {
66+
// Monitor a transaction
67+
nrTxnTracer := nrApp.StartTransaction("Hello")
68+
defer nrTxnTracer.End()
69+
70+
// If no name was given, return an error with a message
71+
if name == "" {
72+
return name, errors.New("empty name")
73+
}
74+
75+
// Create a message using a random format
76+
message := fmt.Sprintf(randomFormat(), name)
77+
78+
// custom attributes by using method in a transaction
79+
nrTxnTracer.AddAttribute("message", message)
80+
return message, nil
81+
}
82+
83+
// randomFormat returns one of a set of greeting messages
84+
func randomFormat() string {
85+
// Set up transaction to trace
86+
nrTxnTracer := nrApp.StartTransaction("randomFormat")
87+
defer nrTxnTracer.End()
88+
89+
randomDelayOuter := randomer.Intn(40)
90+
time.Sleep(time.Duration(randomDelayOuter) * time.Microsecond)
91+
92+
// create a segment for Formats
93+
nrSegment := nrTxnTracer.StartSegment("Formats")
94+
95+
// Random sleep to simulate delays
96+
randomDelayInner := randomer.Intn(80)
97+
time.Sleep(time.Duration(randomDelayInner) * time.Microsecond)
98+
99+
// A slice of message formats.
100+
formats := []string{
101+
"Hi, %v. Welcome!",
102+
"Great to see you, %v!",
103+
"Good day, %v! Well met!",
104+
"%v! Hi there!",
105+
"Greetings %v!",
106+
"Hello there, %v!",
107+
}
108+
109+
// end a segment
110+
nrSegment.End()
111+
// Return a randomly selected message format by specifying
112+
// a random index for the slice of formats.
113+
return formats[randomer.Intn(len(formats))]
114+
}

go.mod

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module github.com/leetcode-golang-classroom/golang-new-relic-transaction-segment-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+
)

0 commit comments

Comments
 (0)