I have built a quick demo that shows that the labels are `inherited` by the 
spawning goroutine for profiling. (I clearly hit the golang-nuts button too 
quickly for this question)

https://play.golang.org/p/SbgtWB1fIKB

Here we have a fairly pointless function which calculates Fibonacci a 
number of times. Once as a straight function call and once in a goroutine. 
When we look at the profile we can see all our functions accounted for

   
(pprof) top10
Showing nodes accounting for 2010ms, 100% of 2010ms total
Showing top 10 nodes out of 12
      flat  flat%   sum%        cum   cum%
     980ms 48.76% 48.76%      980ms 48.76%  main.calcFib_I
     910ms 45.27% 94.03%      910ms 45.27%  main.calcFib_II
     120ms  5.97%   100%      120ms  5.97%  runtime.newstack
         0     0%   100%      980ms 48.76%  main.doWithLabels
         0     0%   100%      980ms 48.76%  main.doWithLabels.func1
         0     0%   100%      980ms 48.76%  main.iteratedFib_I
         0     0%   100%      910ms 45.27%  main.iteratedFib_II
         0     0%   100%      980ms 48.76%  main.main
         0     0%   100%      980ms 48.76%  main.rootFunc
         0     0%   100%      980ms 48.76%  runtime.main

In the example above we can see that our `calcFib_I` and `calcFib_II` 
account for pretty much all of the profile time. (Please note that 
`calcFib_II` is the version which is called from a separate goroutine)

If we then look at just the `foo` tag we see

(pprof) tagfocus=foo
(pprof) top10
Active filters:
   tagfocus=foo
Showing nodes accounting for 0.91s, 45.27% of 2.01s total
Showing top 10 nodes out of 12
      flat  flat%   sum%        cum   cum%
     0.51s 25.37% 25.37%      0.51s 25.37%  main.calcFib_I
     0.39s 19.40% 44.78%      0.39s 19.40%  main.calcFib_II
     0.01s   0.5% 45.27%      0.01s   0.5%  runtime.newstack
         0     0% 45.27%      0.51s 25.37%  main.doWithLabels
         0     0% 45.27%      0.51s 25.37%  main.doWithLabels.func1
         0     0% 45.27%      0.51s 25.37%  main.iteratedFib_I
         0     0% 45.27%      0.39s 19.40%  main.iteratedFib_II
         0     0% 45.27%      0.51s 25.37%  main.main
         0     0% 45.27%      0.51s 25.37%  main.rootFunc
         0     0% 45.27%      0.51s 25.37%  runtime.main

looking at `bar` tag we see

(pprof) tagfocus=bar
(pprof) top10
Active filters:
   tagfocus=bar
Showing nodes accounting for 970ms, 48.26% of 2010ms total
      flat  flat%   sum%        cum   cum%
     520ms 25.87% 25.87%      520ms 25.87%  main.calcFib_II
     450ms 22.39% 48.26%      450ms 22.39%  main.calcFib_I
         0     0% 48.26%      450ms 22.39%  main.doWithLabels
         0     0% 48.26%      450ms 22.39%  main.doWithLabels.func1
         0     0% 48.26%      450ms 22.39%  main.iteratedFib_I
         0     0% 48.26%      520ms 25.87%  main.iteratedFib_II
         0     0% 48.26%      450ms 22.39%  main.main
         0     0% 48.26%      450ms 22.39%  main.rootFunc
         0     0% 48.26%      450ms 22.39%  runtime.main
         0     0% 48.26%      450ms 22.39%  runtime/pprof.Do

So it behaves in a sensible way. In particular we get to see function calls 
in spawned goroutines grouped under the profiling labels attached to the 
goroutines that created them.

So all is well, I think.

On Thursday, 3 January 2019 16:09:09 UTC+1, Francis wrote:
>
> Please note the original example won't run properly. `pprof.Labels()` 
> takes an even number of strings, since it's setting key/value pairs.
>
> func handleFoo(ctx context.Context) {
>     labels := pprof.Labels("foo", "foo")
>     pprof.Do(ctx, labels, func(ctx context.Context) {
>         go interestingFunc()
>     })
> }
>
> Works better.
>
> On Thursday, 3 January 2019 15:22:06 UTC+1, Francis wrote:
>>
>>
>> I would like to use pprof labels, but it seems like I would need to pass 
>> a `context.Context` into all new goroutines and re-call pprof.Do at each of 
>> these stages. Am I mistaken here.
>>
>> specifically if we had
>>
>> unc handleFoo(ctx context.Context) {
>>     pprof.Do(ctx, pprof.Labels("foo"), func(ctx context.Context) {
>>         go interestingFunc()
>>     })
>> }
>>
>> My understanding is that `interestingFunc` would not be profiled with the 
>> ‘foo’ label.
>> If this is correct it presents a problem that I can’t group profiles 
>> under a label when I don’t control the spawning of goroutines myself.
>> i.e. if a library spawns goroutines the work they do will simply appear 
>> under the global profile
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to