Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTML pages to view tenants with overrides #3332

Merged
merged 8 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* [ENHANCEMENT] Added a `parquet-page` cache role for page level caching. [#3196](https://github.com/grafana/tempo/pull/3196) (@joe-elliott)
* [ENHANCEMENT] Update opentelemetry-collector-contrib dependency to the latest version, v0.89.0 [#3148](https://github.com/grafana/tempo/pull/3148) (@gebn)
* [ENHANCEMENT] Update memcached default image in jsonnet for multiple CVE [#3310](https://github.com/grafana/tempo/pull/3310) (@zalegrala)
* [ENHANCEMENT] Add /status/overrides/{tenant} endpoint [#3244](https://github.com/grafana/tempo/pull/3244) (@kvrhdn)
* [ENHANCEMENT] Add HTML pages /status/overrides and /status/overrides/{tenant} [#3244](https://github.com/grafana/tempo/pull/3244) [#3332](https://github.com/grafana/tempo/pull/3332) (@kvrhdn)
* [BUGFIX] Prevent building parquet iterators that would loop forever. [#3159](https://github.com/grafana/tempo/pull/3159) (@mapno)
* [BUGFIX] Sanitize name in mapped dimensions in span-metrics processor [#3171](https://github.com/grafana/tempo/pull/3171) (@mapno)
* [BUGFIX] Fixed an issue where cached footers were requested then ignored. [#3196](https://github.com/grafana/tempo/pull/3196) (@joe-elliott)
Expand Down
22 changes: 0 additions & 22 deletions cmd/tempo/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ func (t *App) Run() error {
t.Server.HTTP().Path("/ready").Handler(t.readyHandler(sm))
t.Server.HTTP().Path("/status").Handler(t.statusHandler()).Methods("GET")
t.Server.HTTP().Path("/status/{endpoint}").Handler(t.statusHandler()).Methods("GET")
t.Server.HTTP().Path("/status/overrides/{tenant}").Handler(t.tenantOverridesHandler()).Methods("GET")
grpc_health_v1.RegisterHealthServer(t.Server.GRPC(), grpcutil.NewHealthCheck(sm))

// Let's listen for events from this manager, and log them.
Expand Down Expand Up @@ -373,8 +372,6 @@ func (t *App) statusHandler() http.HandlerFunc {

switch endpoint {
case "runtime_config":
fallthrough
case "overrides":
err := t.writeRuntimeConfig(&msg, r)
if err != nil {
errs = append(errs, err)
Expand Down Expand Up @@ -523,25 +520,6 @@ func (t *App) writeStatusEndpoints(w io.Writer) error {
return nil
}

func (t *App) tenantOverridesHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

tenant, ok := vars["tenant"]
if !ok {
http.Error(w, "must specify tenant ID", http.StatusBadRequest)
return
}

err := t.Overrides.WriteTenantOverrides(w, r, tenant)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
level.Error(log.Logger).Log("msg", "error writing response", "endpoint", r.URL.String(), "err", err)
return
}
}
}

func (t *App) buildinfoHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
Expand Down
3 changes: 3 additions & 0 deletions cmd/tempo/app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func (t *App) initOverrides() (services.Service, error) {
prometheus.MustRegister(t.Overrides)
}

t.Server.HTTP().Path("/status/overrides").HandlerFunc(overrides.TenantsHandler(t.Overrides)).Methods("GET")
t.Server.HTTP().Path("/status/overrides/{tenant}").HandlerFunc(overrides.TenantStatusHandler(t.Overrides)).Methods("GET")

return t.Overrides, nil
}

Expand Down
8 changes: 6 additions & 2 deletions docs/sources/tempo/api_docs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,17 @@ Displays the override configuration.
Query parameter:
- `mode = (diff)`: Show the difference between defaults and overrides.

Alias: `/status/overrides`
```
GET /status/overrides
```

Displays all tenants that have non-default overrides configured.

```
GET /status/overrides/{tenant}
```

Displays the override configuration for the specified tenant.
Displays all overrides configured for the specified tenant.

```
GET /status/usage-stats
Expand Down
4 changes: 3 additions & 1 deletion modules/overrides/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type Service interface {
type Interface interface {
prometheus.Collector

// GetTenantIDs returns all tenants that have non-default overrides.
GetTenantIDs() []string

// GetRuntimeOverridesFor returns the runtime overrides set for the given user excluding
// overrides from the user-configurable overrides, if enabled.
GetRuntimeOverridesFor(userID string) *Overrides
Expand Down Expand Up @@ -68,5 +71,4 @@ type Interface interface {

// Management API
WriteStatusRuntimeConfig(w io.Writer, r *http.Request) error
WriteTenantOverrides(w io.Writer, r *http.Request, tenant string) error
}
71 changes: 71 additions & 0 deletions modules/overrides/overrides_tenant_status_http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package overrides

import (
_ "embed" // Used to embed html templates
"fmt"
"html/template"
"net/http"
"time"

"github.com/gorilla/mux"
"gopkg.in/yaml.v2"

"github.com/grafana/tempo/pkg/util"
)

//go:embed tenant_status.gohtml
var tenantStatusPageHTML string
var tenantStatusTemplate = template.Must(template.New("webpage").Parse(tenantStatusPageHTML))

type tenantStatusPageContents struct {
Now time.Time `json:"now"`
Tenant string `json:"tenant"`
RuntimeOverrides string `json:"runtime_overrides"`
UserConfigurableOverrides string `json:"user_configurable_overrides"`
}

func TenantStatusHandler(o Interface) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)

tenantID := vars["tenant"]
if tenantID == "" {
util.WriteTextResponse(w, "Tenant ID can't be empty")
return
}

// runtime overrides
overrides := o.GetRuntimeOverridesFor(tenantID)
runtimeOverrides, err := yaml.Marshal(overrides)
if err != nil {
util.WriteTextResponse(w, fmt.Sprintf("Marshalling runtime overrides failed: %s", err))
return
}

// user-configurable overrides
var userConfigurableOverrides string

if userConfigOverridesManager, ok := o.(*userConfigurableOverridesManager); ok {
overrides := userConfigOverridesManager.getTenantLimits(tenantID)
if overrides != nil {
marshalledOverrides, err := yaml.Marshal(overrides)
if err != nil {
util.WriteTextResponse(w, fmt.Sprintf("Marshalling user-configurable overrides failed: %s", err))
return
}
userConfigurableOverrides = string(marshalledOverrides)
} else {
userConfigurableOverrides = "No user-configurable overrides set"
}
} else {
userConfigurableOverrides = "User-configurable overrides are not enabled"
}

util.RenderHTTPResponse(w, tenantStatusPageContents{
Now: time.Now(),
Tenant: tenantID,
RuntimeOverrides: string(runtimeOverrides),
UserConfigurableOverrides: userConfigurableOverrides,
}, tenantStatusTemplate, req)
}
}
83 changes: 83 additions & 0 deletions modules/overrides/overrides_tenants_http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package overrides

import (
_ "embed" // Used to embed html templates
"html/template"
"net/http"
"sort"
"strings"
"time"

"github.com/grafana/tempo/pkg/util"
)

//go:embed tenants.gohtml
yvrhdn marked this conversation as resolved.
Show resolved Hide resolved
var tenantsPageHTML string
var tenantsTemplate = template.Must(template.New("webpage").Parse(tenantsPageHTML))

type tenantsPageContents struct {
Now time.Time `json:"now"`
Tenants []tenantsPageTenant `json:"tenants,omitempty"`
}

type tenantsPageTenant struct {
Name string `json:"name"`
HasRuntimeOverrides bool `json:"has_runtime_overrides"`
HasUserConfigurableOverrides bool `json:"has_user_configurable_overrides"`
}

func TenantsHandler(o Interface) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
tenants := make(map[string]tenantsPageTenant)

// runtime overrides
var runtimeTenants []string
switch o := o.(type) {
case *runtimeConfigOverridesManager:
runtimeTenants = o.GetTenantIDs()
case *userConfigurableOverridesManager:
runtimeTenants = o.Interface.GetTenantIDs()
default:
util.WriteTextResponse(w, "Internal error happened when retrieving runtime overrides")
}
for _, tenant := range runtimeTenants {
tenants[tenant] = tenantsPageTenant{
Name: tenant,
HasRuntimeOverrides: true,
HasUserConfigurableOverrides: false,
}
}

// user-configurable overrides
userConfigurableOverridesManager, ok := o.(*userConfigurableOverridesManager)
if ok {
for _, tenant := range userConfigurableOverridesManager.GetTenantIDs() {
_, hasRuntimeOverrides := tenants[tenant]

tenants[tenant] = tenantsPageTenant{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can this be simplified? Either by using *tenantsPageTenant or making use of how tenants[tenant] will return an uninitialized struct (with HasRunTimeOverrides=false)

Name: tenant,
HasRuntimeOverrides: hasRuntimeOverrides,
HasUserConfigurableOverrides: true,
}
}
}

var tenantsList []tenantsPageTenant
yvrhdn marked this conversation as resolved.
Show resolved Hide resolved
for _, tenant := range tenants {
tenantsList = append(tenantsList, tenant)
}

sortTenantsPageTenant(tenantsList)

util.RenderHTTPResponse(w, tenantsPageContents{
Now: time.Now(),
Tenants: tenantsList,
}, tenantsTemplate, req)
}
}

func sortTenantsPageTenant(list []tenantsPageTenant) {
sort.Slice(list, func(i, j int) bool {
return strings.Compare(list[i].Name, list[j].Name) < 0
})
}
23 changes: 12 additions & 11 deletions modules/overrides/runtime_config_overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,21 @@ func (o *runtimeConfigOverridesManager) WriteStatusRuntimeConfig(w io.Writer, r
return nil
}

func (o *runtimeConfigOverridesManager) GetRuntimeOverridesFor(userID string) *Overrides {
return o.getOverridesForUser(userID)
}

func (o *runtimeConfigOverridesManager) WriteTenantOverrides(w io.Writer, _ *http.Request, userID string) error {
overrides := o.getOverridesForUser(userID)
func (o *runtimeConfigOverridesManager) GetTenantIDs() []string {
tenantOverrides := o.tenantOverrides()
if tenantOverrides == nil {
return nil
}

out, err := yaml.Marshal(overrides)
if err != nil {
return err
var ids []string
for tenant := range tenantOverrides.TenantLimits {
ids = append(ids, tenant)
}
return ids
}

_, err = w.Write(out)
return err
func (o *runtimeConfigOverridesManager) GetRuntimeOverridesFor(userID string) *Overrides {
return o.getOverridesForUser(userID)
}

// IngestionRateStrategy returns whether the ingestion rate limit should be individually applied
Expand Down
17 changes: 17 additions & 0 deletions modules/overrides/tenant_status.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{- /*gotype: github.com/grafana/mimir/pkg/storegateway.tenantsPageContents*/ -}}
yvrhdn marked this conversation as resolved.
Show resolved Hide resolved
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Overrides: tenant</title>
</head>
<body>
<h1>Overrides: tenant</h1>
<p>Current time: {{ .Now }}</p>
<p>Showing overrides for tenant: <strong>{{ .Tenant }}</strong></p>
<h3>User-configurable overrides</h3>
<pre><code>{{ .UserConfigurableOverrides }}</code></pre>
<h3>Runtime overrides</h3>
<pre><code>{{ .RuntimeOverrides }}</code></pre>
</body>
</html>
30 changes: 30 additions & 0 deletions modules/overrides/tenants.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{- /*gotype: github.com/grafana/mimir/pkg/storegateway.tenantsPageContents*/ -}}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Overrides: tenants</title>
</head>
<body>
<h1>Overrides: tenants</h1>
<p>Current time: {{ .Now }}</p>
<table border="1" cellpadding="5" style="border-collapse: collapse">
<thead>
<tr>
<th>Tenant</th>
<th>Runtime overrides</th>
<th>User-configurable overrides</th>
</tr>
</thead>
<tbody style="font-family: monospace;">
{{ range .Tenants }}
<tr>
<td><a href="overrides/{{ .Name }}">{{ .Name }}</a></td>
<td>{{ if .HasRuntimeOverrides }}x{{ end }}</td>
<td>{{ if .HasUserConfigurableOverrides }}x{{ end }}</td>
</tr>
{{ end }}
</tbody>
</table>
</body>
</html>
33 changes: 12 additions & 21 deletions modules/overrides/user_configurable_overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ type userConfigurableOverridesManager struct {
logger log.Logger
}

var _ Service = (*userConfigurableOverridesManager)(nil)
var (
_ Service = (*userConfigurableOverridesManager)(nil)
_ Interface = (*userConfigurableOverridesManager)(nil)
)

// newUserConfigOverrides wraps the given overrides with user-configurable overrides.
func newUserConfigOverrides(cfg *UserConfigurableOverridesConfig, subOverrides Service) (*userConfigurableOverridesManager, error) {
Expand Down Expand Up @@ -195,6 +198,14 @@ func (o *userConfigurableOverridesManager) setTenantLimit(userID string, limits
}
}

func (o *userConfigurableOverridesManager) GetTenantIDs() []string {
var ids []string
for tenant := range o.getAllTenantLimits() {
yvrhdn marked this conversation as resolved.
Show resolved Hide resolved
ids = append(ids, tenant)
}
return ids
}

func (o *userConfigurableOverridesManager) Forwarders(userID string) []string {
if forwarders, ok := o.getTenantLimits(userID).GetForwarders(); ok {
return forwarders
Expand Down Expand Up @@ -316,26 +327,6 @@ func (o *userConfigurableOverridesManager) WriteStatusRuntimeConfig(w io.Writer,
return nil
}

type statusTenantOverrides struct {
UserConfigurableLimits *userconfigurableoverrides.Limits `yaml:"user_configurable_limits"`
RuntimeOverrides *Overrides `yaml:"runtime_overrides"`
}

func (o *userConfigurableOverridesManager) WriteTenantOverrides(w io.Writer, _ *http.Request, userID string) error {
overrides := statusTenantOverrides{
UserConfigurableLimits: o.getTenantLimits(userID),
RuntimeOverrides: o.GetRuntimeOverridesFor(userID),
}

out, err := yaml.Marshal(overrides)
if err != nil {
return err
}

_, err = w.Write(out)
return err
}

func (o *userConfigurableOverridesManager) Describe(ch chan<- *prometheus.Desc) {
// TODO for now just pass along to the underlying overrides, in the future we should export
// the user-config overrides as well
Expand Down
Loading