Hi, I'am using elastic.co SAAS and trying to test the APM instrumenting a sample golang app. (it's actually working good with nodeJS app)
In this sample I'am using the lastest version of the gorilla/mux router (1.7.0). I followed this quick tutorial here : https://www.codementor.io/codehakase/building-a-restful-api-with-golang-a6yivzqdo
Here is my golang code :
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"go.elastic.co/apm"
"go.elastic.co/apm/module/apmgorilla"
)
// The person Type (more like an object)
type Person struct {
ID string `json:"id,omitempty"`
Firstname string `json:"firstname,omitempty"`
Lastname string `json:"lastname,omitempty"`
Address *Address `json:"address,omitempty"`
}
type Address struct {
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
}
var people []Person
// Display all from the people var
func GetPeople(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(people)
}
// Display a single data
func GetPerson(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
for _, item := range people {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
json.NewEncoder(w).Encode(&Person{})
}
// create a new item
func CreatePerson(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
var person Person
_ = json.NewDecoder(r.Body).Decode(&person)
person.ID = params["id"]
people = append(people, person)
json.NewEncoder(w).Encode(people)
}
// Delete an item
func DeletePerson(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
for index, item := range people {
if item.ID == params["id"] {
people = append(people[:index], people[index+1:]...)
break
}
json.NewEncoder(w).Encode(people)
}
}
// main function to boot up everything
func main() {
log.Print("ELASTIC_APM_SERVICE_NAME:", os.Getenv("ELASTIC_APM_SERVICE_NAME"))
log.Print("ELASTIC_APM_SERVER_URL:", os.Getenv("ELASTIC_APM_SERVER_URL"))
log.Print("ELASTIC_APM_SECRET_TOKEN:", os.Getenv("ELASTIC_APM_SECRET_TOKEN"))
router := mux.NewRouter()
router.Use(apmgorilla.Middleware())
people = append(people, Person{ID: "1", Firstname: "John", Lastname: "Doe", Address: &Address{City: "City X", State: "State X"}})
people = append(people, Person{ID: "2", Firstname: "Koko", Lastname: "Doe", Address: &Address{City: "City Z", State: "State Y"}})
people = append(people, Person{ID: "3", Firstname: "Kiki", Lastname: "Doe", Address: &Address{City: "City Z", State: "State Z"}})
router.HandleFunc("/people", GetPeople).Methods("GET")
router.HandleFunc("/people/{id}", GetPerson).Methods("GET")
router.HandleFunc("/people/{id}", CreatePerson).Methods("POST")
router.HandleFunc("/people/{id}", DeletePerson).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8000", router))
}
Environment variables are well displayed when I print them in the main function, the service responds fine, but I have no trace in the APM and no error in log.
Thank you for your help.