1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Go で New Relic を試してみた

Posted at

作成:2023年7月30日
お試しで New Relic を触ってみました。

ライセンスキーの確認

このリンク先にあるリンク「アカウントのライセンスキー」でライセンスキーを確認します。

エージェントのインストール

import するためにインストールします。

MacBook Terminal
go get github.com/newrelic/go-agent/v3/newrelic

main.go を作成

シンプルなサンプルプログラムを作りました。
import して、Goエージェントを初期化します。上記で確認したライセンスキーを入力します。

main.go
package main

import (
	"fmt"
	"net/http"
	"github.com/newrelic/go-agent/v3/newrelic"
)

func main() {
	app, err := newrelic.NewApplication(
		newrelic.ConfigAppName("好きなアプリ名を入力してください"),
		newrelic.ConfigLicense("ライセンスキーを入力してください"),
	)
	if nil != err {
		fmt.Println("failed to setup new relic")
	} else {
		fmt.Println("start new relic")
	}

	http.HandleFunc(newrelic.WrapHandleFunc(app, "/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello World!!!")
	}))
	http.ListenAndServe(":3000", nil)
}

main.go を実行して Web transactions time 等を確認

MacBook Terminal 1
% go run main.go 
start new relic

main.go を実行してから、数分待ってから curl を実行します。

MacBook Terminal 2
% curl http://127.0.0.1:3000 
Hello World!!!%                                                                          % curl http://127.0.0.1:3000
Hello World!!!%                                                                                                                                                                                             ryo@Ryo-MacBook-Pro ~ % curl http://127.0.0.1:3000
Hello World!!!%     

見ることができました。

参考

Go向けNew Relicのインストール
New Relic Go agentに入門

メモ: Goライブラリを使うときは、New Relic infrastructure agent を Docker上で実行しなくてもよかったです

情報が、
Goプログラム → New Relic infrastructure agent → New Relic server
の順番で流れていきます、と思ってましたが、Goプログラムから直接サーバーに飛んでました。

下の画面からコマンドをコピーしました。

ライセンスキーを入力してローカルのDockerコンテナ上で New Relic infrastructure agent を起動します。

MacBook Terminal
docker run \
 -d \
 --name newrelic-infra \
 --network=host \
 --cap-add=SYS_PTRACE \
 --privileged \
 --pid=host \
 -v "/:/host:ro" \
 -v "/var/run/docker.sock:/var/run/docker.sock" \
 -e  NRIA_LICENSE_KEY=ライセンスキーを入力してください \
 newrelic/infrastructure:latest

以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?