vitess.io/vitess@v0.16.2/go/vt/throttler/throttlerz.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package throttler
    18  
    19  import (
    20  	"fmt"
    21  	"html/template"
    22  	"net/http"
    23  	"strings"
    24  
    25  	"vitess.io/vitess/go/vt/log"
    26  )
    27  
    28  const listHTML = `<!DOCTYPE html>
    29  <title>{{len .Throttlers}} Active Throttler(s)</title>
    30  <ul>
    31  {{range .Throttlers}}
    32    <li>
    33      <a href="/throttlerz/{{.}}">{{.}}</a>
    34    </li>
    35  {{end}}
    36  </ul>
    37  `
    38  
    39  const detailsHTML = `<!DOCTYPE html>
    40  <title>Details for Throttler '{{.}}'</title>
    41  <a href="/throttlerlogz/{{.}}">adapative throttling log</a>
    42  TODO(mberlin): Add graphs here.
    43  `
    44  
    45  var (
    46  	listTemplate = template.Must(template.New("list").Parse(listHTML))
    47  
    48  	detailsTemplate = template.Must(template.New("details").Parse(detailsHTML))
    49  )
    50  
    51  func init() {
    52  	http.HandleFunc("/throttlerz/", func(w http.ResponseWriter, r *http.Request) {
    53  		throttlerzHandler(w, r, GlobalManager)
    54  	})
    55  }
    56  
    57  func throttlerzHandler(w http.ResponseWriter, r *http.Request, m *managerImpl) {
    58  	// Longest supported URL: /throttlerz/<name>
    59  	parts := strings.SplitN(r.URL.Path, "/", 3)
    60  
    61  	if len(parts) != 3 {
    62  		errMsg := fmt.Sprintf("invalid /throttlerz path: %q expected paths: /throttlerz or /throttlerz/<throttler name>", r.URL.Path)
    63  		http.Error(w, errMsg, http.StatusInternalServerError)
    64  		return
    65  	}
    66  
    67  	name := parts[2]
    68  	if name == "" {
    69  		listThrottlers(w, m)
    70  		return
    71  	}
    72  
    73  	showThrottlerDetails(w, name)
    74  }
    75  
    76  func listThrottlers(w http.ResponseWriter, m *managerImpl) {
    77  	throttlers := m.Throttlers()
    78  
    79  	// Log error
    80  	if err := listTemplate.Execute(w, map[string]any{
    81  		"Throttlers": throttlers,
    82  	}); err != nil {
    83  		log.Errorf("listThrottlers failed :%v", err)
    84  	}
    85  }
    86  
    87  func showThrottlerDetails(w http.ResponseWriter, name string) {
    88  	// Log error
    89  	if err := detailsTemplate.Execute(w, name); err != nil {
    90  		log.Errorf("showThrottlerDetails failed :%v", err)
    91  	}
    92  }