Go agent does not seem to send any trace to APM

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.

Ok, I solved my problem so I post the solution here in case other people experience the same :

We are using Elastic Cloud (the SaaS version of elasticsearch). So we have an HTTPS endpoint which looks like https://0732555c72f...

Following the documentation I had configured 3 environment variables :

ELASTIC_APM_SECRET_TOKEN
ELASTIC_APM_SERVICE_NAME
ELASTIC_APM_SERVER_URL

It was not working.

So I just set a 4th one :

ELASTIC_APM_VERIFY_SERVER_CERT=false

And it worked.

Hi @cabrinoob,

That's unusual. I would not recommend setting ELASTIC_APM_VERIFY_SERVER_CERT, as it will make your data susceptible to MITM attacks/snooping.

I've just created a cloud deployment and confirmed that the Go agent can successfully communicate with the server without disabling certificate verification. It's probably something about your environment.

What operating system are you running? Is your app containerised? What happens if you run curl https://0732555c72f... from the same host as your Go application? It may be helpful to see the errors that the Go agent is experiencing, which you can log to a file by setting

ELASTIC_APM_LOG_FILE=<path/to/file>
ELASTIC_APM_LOG_LEVEL=debug

You can alternatively set ELASTIC_APM_LOG_FILE=stderr to send to stderr.

Hi, my application is containerised and deployed on K8S.
I activated the APM log traces and I have this error :

{"level":"debug","time":"2019-02-18T08:15:12Z","message":"request failed: sending request failed: Post https://0732555c72....cloud.es.io:443/intake/v2/events: x509: certificate signed by unknown authority (next request in ~0s)"}

Hi, my application is containerised and deployed on K8S.

I suspect you need to update CA certificates in the container image. If you're using Alpine as the base image, try adding this to your Dockerfile:

RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*

(As mentioned in Install Certificates in Alpine Image to establish Secured Communication (SSL/TLS) | HackerNoon)

1 Like

That works! Thank you!

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.