Error when trying to parse trace using go tool trace - go

I was following tutorial from JustForFunc episode 22
Added those two lines at the start of main() in main.go:
trace.Start(os.Stdout)
defer trace.Stop()
build the binary using go build -o appName
Timed it with time ./appName > m.trace
And finally tried to open trace with go tool trace m.trace
but got following error:
2017/11/10 19:15:38 Parsing trace...
failed to parse trace: unknown event type 50 at offset 0x16
Little more background on my code(golang 1.9, linux) : it is a server for GET requests built with gin-gonic. I added extra line of code time.AfterFunc(20*time.Seconds, func(){closeServer()}) to close my server after 20 seconds so I could make few request to it and then stop server exiting program.

I found a solution to my problem.
I followed this tutorial https://making.pusher.com/go-tool-trace/.
Added code to main :
f, err := os.Create("trace.out")
if err != nil {
panic(err)
}
defer f.Close()
err = trace.Start(f)
if err != nil {
panic(err)
}
defer trace.Stop()
// Your program here
And it seems to be working fine. I have no idea what could cause this problem :(

Related

How to handle stdin/stdout with dlv

I am using Delve to debug and having issues with the best way to handle stdin/stdout.
The first problem is that I cannot read the console. I have a function that takes use onput from the console:
func readConsole() string {
reader := bufio.NewReader(os.Stdin)
entry, err := reader.ReadString('\n')
if err != nil {
tlog.Fatal(fmt.Errorf("readConsole(): Error reading console input. %v", err))
}
entry = strings.Replace(entry, "\n", "", -1)
return entry
}
The following "bad file descriptor" error is returned by ReadString():
F0208 21:03:56.574021 429026 configurator.go:81] readConsole(): Error reading console input. read /dev/stdin: bad file descriptor
The second problem is that fmt.Printf() works when I just run the app, but if I am stepping through source code fmt.Printf() does not display anything.
I get that dlv is competing for input and output via the console, but not sure how to manage the competing requirements.

What causes 'no such file or directory' after adding a test case and running go test?

The problem
After adding another test function to an existing test file running go test -v ./... fails due to several no such file or directory build errors after adding another test case. The error messages are seemingly unrelated to the changes, however.
The added test case can be found in the relevant code section at the bottom.
The error messages are:
open /tmp/go-build842273301/b118/vet.cfg: no such file or directory
open /tmp/go-build842273301/b155/vet.cfg: no such file or directory
# tornadowarnung.xyz/riotwatch/riot/static
vet: in tornadowarnung.xyz/riotwatch/riot/static, can't import facts for package "encoding/json": open $WORK/b036/vet.out: no such file or directory
# tornadowarnung.xyz/riotwatch/web/server/endpoint/static
vet: open $WORK/b121/vet.cfg: no such file or directory
open /tmp/go-build842273301/b115/vet.cfg: no such file or directory
open /tmp/go-build842273301/b001/vet.cfg: no such file or directory
# tornadowarnung.xyz/riotwatch/web/server
vet: open $WORK/b152/vet.cfg: no such file or directory
# tornadowarnung.xyz/riotwatch/web/server/endpoint/static
vet: open $WORK/b159/vet.cfg: no such file or directory
Because of that, some packages show their build failed:
FAIL tornadowarnung.xyz/riotwatch/riot/static [build failed]
FAIL tornadowarnung.xyz/riotwatch/web/server [build failed]
FAIL tornadowarnung.xyz/riotwatch/web/server/endpoint [build failed]
FAIL tornadowarnung.xyz/riotwatch/web/server/endpoint/static [build failed]
Relevant code
func TestLoader_ProfileIcon(t *testing.T) {
tempDir := os.TempDir()
l := Loader{
profileIconPath: tempDir,
}
defer os.RemoveAll(tempDir)
t.Run("returns expected content", func(t *testing.T) {
want := bytes.NewBufferString("image data")
fileName := "123456"
if err := createTestFile(t, tempDir, fileName, want); err != nil {
t.Fatal(err)
}
got, err := l.ProfileIcon(123456)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
})
t.Run("does not panic on missing file", func(t *testing.T) {
res, err := l.ProfileIcon(-1)
if err == nil {
t.Errorf("Expected an error but got error %v and result %v", nil, res)
}
})
}
func createTestFile(t *testing.T, tempDir string, fileName string, content *bytes.Buffer) error {
t.Helper()
f, err := os.Create(path2.Join(tempDir, fmt.Sprintf("%v.png", fileName)))
if err != nil {
return err
}
_, err = f.Write(content.Bytes())
if err != nil {
return err
}
return nil
}
Reproducing the error is difficult
On my Ubuntu machine having go 1.15 installed the error only occurs sometimes when I'm cloning the repository again or when I'm cleaning the test cache.
When running the image used in the Gitlab job golang:alpine locally and running the same commands I cannot reproduce this error every time. Sometimes it occurs but most of the time it doesn't.
What I've tried
I have tried to switch between go versions 1.13, 1.14, and 1.15 but every version yields the same result.
Switching to any other images like golang:latest, golang:1.14 or golang:1.13 doesn't help either.
I've tried googling for the error that occurs but I haven't found any results that are relevant or contain any solution I haven't already tried.
Reverting said commit will make the tests pass again. I've also reverted the commit and slowly tried to introduce the changes again manually. This makes the problems occur again.
os.TempDir doesn't create a new temporary directory for you, it returns the system's temp dir. By calling os.RemoveAll on it, you're blowing away the entire thing, including some scratch files used by the build and test process.
I could validate the behaviour on a MacOS.
Seems like there is something wrong with the os.TempDir().
Your tests ran when I created the dir myself with os.Mkdir(...).
You should create an Issue in the Go repository.

unstructured.UnstructuredList caused lots of reflect.go trace

I'm trying to use the unstructured.UnstructuredList to reuse come logic for configmap and secret.
However, after adding the ListAndDeployReferredObject, I started to see tons of trace as Starting reflector *unstructured.Unstructured was added to my log file.
Am I doing something odd or I'm missing some setting for using the unstructured.Unstructured?
Thanks in advance.
func (r *ReconcileSubscription) ListAndDeployReferredObject(instance *appv1alpha1.Subscription, gvk schema.GroupVersionKind, refObj referredObject) error {
insName := instance.GetName()
insNs := instance.GetNamespace()
uObjList := &unstructured.UnstructuredList{}
uObjList.SetGroupVersionKind(gvk)
opts := &client.ListOptions{Namespace: insNs}
err := r.Client.List(context.TODO(), uObjList, opts)
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("Failed to list referred objects with error %v ", err)
return err
}
// other logics...
}
I0326 23:05:58.955589 95169 reflector.go:120] Starting reflector *unstructured.Unstructured (10m0s) from pkg/mod/k8s.io/client-go#v0.0.0-20191016111102-bec269661e48/tools/cache/reflector.go:96
...
I0326 23:15:18.718932 95169 reflector.go:158] Listing and watching *unstructured.Unstructured from pkg/mod/k8s.io/client-go#v0.0.0-20191016111102-bec269661e48/tools/cache/reflector.go:96
I figured out these prints are normal, since we are using the dynamic client on our controller for caches

Can't get golang and the package bigquery to work for loading to big query

I am trying to figure out how to get a simple bq load command to work with https://godoc.org/cloud.google.com/go/bigquery#Table.LoaderFrom
Running it manually it looks like this:
bq load --source_format=AVRO --ignore_unknown_values --replace=true mydataset.mytable gs://mybucket/table/*
And running it in my golang with exec.Command() successfully looks like this:
exec.Command("bq", "load", "--source_format=AVRO", "--ignore_unknown_values",
"--replace=true", "mydataset.mytable",
"gs://mybucket/table/*")
However, I cannot get this program to run without a segmentation fault when trying to get the load and job.wait to run successfully it seems to be getting a segmentation violation at the job.wait line of the program
package main
import (
"context"
"log"
"cloud.google.com/go/bigquery"
)
func main(){
ctx := context.Background()
client, err := bigquery.NewClient(ctx, "my-project-id")
if err != nil {
// TODO: Handle error.
}
gcsRef := bigquery.NewGCSReference("gs://mybucket/table/*")
gcsRef.SourceFormat = "AVRO"
gcsRef.IgnoreUnknownValues = true
// TODO: set other options on the GCSReference.
ds := client.Dataset("mydataset")
loader := ds.Table("mytable").LoaderFrom(gcsRef)
// TODO: set other options on the Loader.
job, err := loader.Run(ctx)
if err != nil {
// TODO: Handle error.
}
status, err := job.Wait(ctx) //seg faults right here
if err != nil {
// TODO: Handle error.
}
if status.Err() != nil {
// TODO: Handle error.
}
}
The panic is probably coming from a nil pointer reference to the job variable.
I would suggest including a log.Fatal(err)
In all of your err!= nil blocks.
This will help get you closer to why job is not being assigned correctly.
When you're writing one off scripts like this one in go log.Fatal is a great way to exit the program and print exactly what the issue is.
With go you're always trying to bubble errors up the stack to determine if the code should continue to execute, if things can be recovered, or if it's just a fatal thing and you should end the program.
For more info on the logging package checkout here: https://golang.org/pkg/log/
If you're just starting out learning go here are some awesome resources that can help give you ideas on how different types of programs can be designed.
https://github.com/dashpradeep99/https-github.com-miguellgt-books/tree/master/go
Best,
Christopher

Golang http get request breaks on some but not all urls

Right now I'm fetching urls from indiegogo as part of a side project using the basic get request template found [here][1]. I then translate the byte data into a string using
responseText, err:= ioutil.ReadAll(response.Body)
trueText:= string(responseText)
with appropriate error handling where needed
It works fine for repeated attempts at getting and some other urls of varying length(at least as large as the previous url and some longer than the next).
Strangely, when I attempt to get it breaks and throws a runtime error of
panic: runtime error: index out of range
and exits with a status of 2. I'm curious as to what the issue could be.
I know it isn't indiegogo getting angry about my once a minute requests and cutting my connection because I can request continiously for 20 minutes at with no issue. Give it a bit of downtime and it still completely breaks on
Thanks for the assistance
EDIT, it appears as though it was a malformed bit of html in some of the pages that messed with a loop I was running based on the content that managed to break go in the runtime on only some urls. Thanks for the help
[1]:
There is no error when getting from the url and converting the body to the Go string type. For example,
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
url := "http://www.indiegogo.com/projects/culcharge-smallest-usb-charge-and-data-cable-for-iphone-and-android"
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
text := string(body)
fmt.Println(len(body), len(text))
}
Output:
66363 66363
You didn't provide us with a small fragment of code which compiles, runs, and fails in the manner you describe. That leaves us all guessing.

Resources