Refactor zipkin to add ability to get trace from traceId (#348)

This commit is contained in:
srinivashegde86 2019-03-28 11:42:55 -07:00 committed by Knative Prow Robot
parent 671c872a77
commit c35005418b
2 changed files with 35 additions and 24 deletions

View File

@ -19,8 +19,6 @@ limitations under the License.
package spoof
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
@ -229,33 +227,15 @@ func (sc *SpoofingClient) logZipkinTrace(spoofResp *Response) {
}
traceID := spoofResp.Header.Get(zipkin.ZipkinTraceIDHeader)
if err := zipkin.CheckZipkinPortAvailability(); err == nil {
sc.logf("port-forwarding for Zipkin is not-setup. Failing Zipkin Trace retrieval")
return
}
sc.logf("Logging Zipkin Trace for: %s", traceID)
sc.logf("Logging Zipkin Trace: %s", traceID)
zipkinTraceEndpoint := zipkin.ZipkinTraceEndpoint + traceID
// Sleep to ensure all traces are correctly pushed on the backend.
time.Sleep(5 * time.Second)
resp, err := http.Get(zipkinTraceEndpoint)
if err != nil {
sc.logf("Error retrieving Zipkin trace: %v", err)
return
}
defer resp.Body.Close()
trace, err := ioutil.ReadAll(resp.Body)
json, err := zipkin.JSONTrace(traceID)
if err != nil {
sc.logf("Error reading Zipkin trace response: %v", err)
return
sc.logf("Error getting zipkin trace: %v", err)
}
var prettyJSON bytes.Buffer
if error := json.Indent(&prettyJSON, trace, "", "\t"); error != nil {
sc.logf("JSON Parser Error while trying for Pretty-Format: %v, Original Response: %s", error, string(trace))
return
}
sc.logf("%s", prettyJSON.String())
sc.logf("%s", json)
}

View File

@ -19,6 +19,10 @@ limitations under the License.
package zipkin
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"sync"
"github.com/knative/pkg/test/logging"
@ -112,3 +116,30 @@ func CleanupZipkinTracingSetup(logf logging.FormatLogger) {
func CheckZipkinPortAvailability() error {
return monitoring.CheckPortAvailability(ZipkinPort)
}
// JSONTrace returns a trace for the given traceId in JSON format
func JSONTrace(traceID string) (string, error) {
// Check if zipkin port forwarding is setup correctly
if err := CheckZipkinPortAvailability(); err == nil {
return "", err
}
resp, err := http.Get(ZipkinTraceEndpoint + traceID)
if err != nil {
return "", err
}
defer resp.Body.Close()
trace, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, trace, "", "\t")
if err != nil {
return "", err
}
return prettyJSON.String(), nil
}