go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/pprof/portal.go (about)

     1  // Copyright 2017 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package pprof
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"html"
    21  	"html/template"
    22  
    23  	"go.chromium.org/luci/server/portal"
    24  )
    25  
    26  type portalPage struct {
    27  	portal.BasePage
    28  }
    29  
    30  func (portalPage) Title(c context.Context) (string, error) {
    31  	return "Pprof profiling", nil
    32  }
    33  
    34  func (portalPage) Overview(c context.Context) (template.HTML, error) {
    35  	return `<p>This page allows to generate special tokens that authenticate
    36  access to Go pprof endpoints, such as <code>/debug/pprof/heap</code>.</p>
    37  
    38  <p>Normally (when using <a href="https://golang.org/pkg/net/http/pprof/">net/http/pprof</a>
    39  package) they are wide open to anyone who has access to the serving port. This
    40  is inconvenient in environments that don't easily allow filtering requests
    41  before they hit the server (GAE Flex is an example). Using tokens for
    42  authentication allows exposing pprof endpoints directly.</p>
    43  
    44  <p>Usage:</p>
    45  <ul>
    46  <li>Hit "Generate pprof token" button below.</li>
    47  <li>Copy the token it produces.</li>
    48  <li>Pass it as <code>?tok=...</code> URL parameter to pprof endpoints.</li>
    49  </ul>
    50  
    51  <p>Remember that in most cases (e.g. on GAE Flex, and other load-balanced
    52  environments) requests are routed to some random backend process, so hitting
    53  same profile page multiple times may result in completely different profiles.
    54  </p>
    55  `, nil
    56  }
    57  
    58  func (portalPage) Actions(c context.Context) ([]portal.Action, error) {
    59  	return []portal.Action{
    60  		{
    61  			ID:    "GeneratePprofToken",
    62  			Title: "Generate pprof token",
    63  			Callback: func(c context.Context) (string, template.HTML, error) {
    64  				tok, err := generateToken(c)
    65  				if err != nil {
    66  					return "", "", err
    67  				}
    68  				return "Done", template.HTML(fmt.Sprintf(
    69  					`<p>The generated pprof token is:</p><pre>%s</pre>`+
    70  						`<p>It is valid for 12 hours. Pass it a pprof endpoint via `+
    71  						`<code>?tok=...</code> URL parameter, e.g</p>`+
    72  						`<pre>$ go tool pprof https://host/debug/pprof/heap?tok=%s...</pre>`,
    73  					html.EscapeString(tok), html.EscapeString(tok[:5]))), nil
    74  			},
    75  		},
    76  	}, nil
    77  }
    78  
    79  func init() {
    80  	portal.RegisterPage("pprof", portalPage{})
    81  }