# \[new beat\]Panic with the beat code

**URL:** <https://discuss.elastic.co/t/new-beat-panic-with-the-beat-code/39482>\
**Category:** Beats\
**Created:** [January 18, 2016, 8:50pm UTC](https://discuss.elastic.co/t/new-beat-panic-with-the-beat-code/39482 "2016-01-18T20:50:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pollux](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pollux/32/31600_2.png) [@pollux](https://discuss.elastic.co/u/pollux)\
**Post date:** [January 18, 2016, 8:50pm UTC](https://discuss.elastic.co/t/new-beat-panic-with-the-beat-code/39482/1 "2016-01-18T20:50:49Z")

</div>

Hi,

I would monitor file and folder size.

So I said "ok I saw all this community beat, it must be not so hard :D".

At first I'm trying using `filepath.Walk` with a custom (simple) callback function like that :

```auto
package main

import (
	"os"
	"log"
	"fmt"
	"path/filepath"
)

type FileSize struct {
	size int64
	nbFile int64
	nbDir int64
}

func (st *FileSize) MyWalk(path string, info os.FileInfo, err error) error {
	if info.IsDir() {
    st.nbDir += 1
  } else {
    st.nbFile += 1
    st.size += info.Size()
  }
	return nil
}

func main() {
	paths := []string{"./", "../"}
	for _, path := range paths {
		var st FileSize
		err := filepath.Walk(path, st.MyWalk)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("total size %d, nb file %d, nb folder %d", st.size, st.nbFile, st.nbDir)
	}
}

```

it is dumb, but it works.

the gh repo is [here](https://github.com/RemiDesgrange/FileSizeBeat) it's certainly not perfect and lack of comment, but the code is pretty much the same, and panic...

I know that a great idea would be to use fsnotify. But maybe I will try to get it working and then use fsnotify.

In advance thanks

---

<div class="post-metadata">

**Author:** ![steffens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steffens/32/79630_2.png) [@steffens](https://discuss.elastic.co/u/steffens)\
**Post date:** [January 19, 2016, 12:02am UTC](https://discuss.elastic.co/t/new-beat-panic-with-the-beat-code/39482/2 "2016-01-19T00:02:59Z")

</div>

So your beat is failing with a panic? I didn't test it yet, but can you also add a stack-trace? You are running on windows?

---

<div class="post-metadata">

**Author:** ![pollux](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pollux/32/31600_2.png) [@pollux](https://discuss.elastic.co/u/pollux)\
**Post date:** [January 19, 2016, 8:41am UTC](https://discuss.elastic.co/t/new-beat-panic-with-the-beat-code/39482/3 "2016-01-19T08:41:49Z")

</div>

The stack trace is prety heavy. Maybe I need to use channel ? At first I just want to do something that's working ,that's why I didn't use channel, goroutine etc... but if I need to...

stack trace :

> <https://gist.github.com/RemiDesgrange/b34a63ef6c2893fb0e3a>

---

<div class="post-metadata">

**Author:** ![steffens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steffens/32/79630_2.png) [@steffens](https://discuss.elastic.co/u/steffens)\
**Post date:** [January 19, 2016, 11:11am UTC](https://discuss.elastic.co/t/new-beat-panic-with-the-beat-code/39482/4 "2016-01-19T11:11:15Z")

</div>

I see you're not handling errors in your walk function callback. The stack trace indicates 'info' being nil. With os.FileInfo being an interface, it might be nil if an error occured while walking your tree.

For some debugging you can try to insert `logp.Debug("filesize", "path=%v, info=%v, err=%v\n", path, info, err)` and run your beat with `-d "filesize"`.

---

<div class="post-metadata">

**Author:** ![pollux](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pollux/32/31600_2.png) [@pollux](https://discuss.elastic.co/u/pollux)\
**Post date:** [January 19, 2016, 11:27am UTC](https://discuss.elastic.co/t/new-beat-panic-with-the-beat-code/39482/5 "2016-01-19T11:27:01Z")

</div>

thanks, I rewrote the method this morning. I'm currently cleaning the `fmt.Printf` debug message, i'm doing to commit very soon. Thanks anyway. I have to admit that this is my first "real" go code, and the stack trace scared me a bit 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 9:56pm UTC](https://discuss.elastic.co/t/new-beat-panic-with-the-beat-code/39482/6 "2017-07-05T21:56:35Z")

</div>


