Skip to content

Commit

Permalink
Add toggle to inject the tenant ID to generated metrics (#3638)
Browse files Browse the repository at this point in the history
  • Loading branch information
yvrhdn authored May 28, 2024
1 parent 7c68b25 commit 6206fcd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

* [CHANGE] Update Alpine image version to 3.20 [#3710](https://github.com/grafana/tempo/pull/3710) (@joe-elliott)
* [ENHANCEMENT] TraceQL - Add support for trace:id and span:id [#3670](https://github.com/grafana/tempo/pull/3670) (@ie-pham)
* [ENHANCEMENT] Add toggle to inject the tenant ID to generated metrics [#3638](https://github.com/grafana/tempo/pull/3638) (@kvrhdn)
* [BUGFIX] Fix TraceQL queries involving non boolean operations between statics and attributes. [#3698](https://github.com/grafana/tempo/pull/3698) (@joe-elliott)

## v2.5.0-rc.0
Expand Down
3 changes: 3 additions & 0 deletions docs/sources/tempo/configuration/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ metrics_generator:
# A list of labels that will be added to all generated metrics.
[external_labels: <map>]

# If set, the tenant ID will added as label with the given label name to all generated metrics.
[inject_tenant_id_as: <string>]

# The maximum length of label names. Label names exceeding this limit will be truncated.
[max_label_name_length: <int> | default = 1024]

Expand Down
2 changes: 2 additions & 0 deletions modules/generator/registry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Config struct {
// ExternalLabels are added to any time series generated by this instance.
ExternalLabels map[string]string `yaml:"external_labels,omitempty"`

InjectTenantIDAs string `yaml:"inject_tenant_id_as,omitempty"`

// MaxLabelNameLength configures the maximum length of label names. Label names exceeding
// this limit will be truncated.
MaxLabelNameLength int `yaml:"max_label_name_length"`
Expand Down
4 changes: 4 additions & 0 deletions modules/generator/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func New(cfg *Config, overrides Overrides, tenant string, appendable storage.App
hostname, _ := os.Hostname()
externalLabels["__metrics_gen_instance"] = hostname

if cfg.InjectTenantIDAs != "" {
externalLabels[cfg.InjectTenantIDAs] = tenant
}

r := &ManagedRegistry{
onShutdown: cancel,

Expand Down
19 changes: 19 additions & 0 deletions modules/generator/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,25 @@ func TestManagedRegistry_externalLabels(t *testing.T) {
collectRegistryMetricsAndAssert(t, registry, appender, expectedSamples)
}

func TestManagedRegistry_injectTenantIDAs(t *testing.T) {
appender := &capturingAppender{}

cfg := &Config{
InjectTenantIDAs: "__tempo_tenant",
}
registry := New(cfg, &mockOverrides{}, "test", appender, log.NewNopLogger())
defer registry.Close()

counter := registry.NewCounter("my_counter")
counter.Inc(nil, 1.0)

expectedSamples := []sample{
newSample(map[string]string{"__name__": "my_counter", "__metrics_gen_instance": mustGetHostname(), "__tempo_tenant": "test"}, 0, 0),
newSample(map[string]string{"__name__": "my_counter", "__metrics_gen_instance": mustGetHostname(), "__tempo_tenant": "test"}, 0, 1),
}
collectRegistryMetricsAndAssert(t, registry, appender, expectedSamples)
}

func TestManagedRegistry_maxSeries(t *testing.T) {
appender := &capturingAppender{}

Expand Down

0 comments on commit 6206fcd

Please sign in to comment.