Skip to content

Commit

Permalink
feat: metrics tagged with readable tenant name (#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
pboros committed Oct 25, 2022
1 parent c726b42 commit 8b75d32
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 179 deletions.
22 changes: 9 additions & 13 deletions server/metrics/tenant.go → server/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package metrics
package defaults

import (
"context"

"github.com/tigrisdata/tigris/server/request"
const (
// DefaultNamespaceName is for "default" namespace in the cluster which means all the databases created are under a single
// namespace.
// It is totally fine for a deployment to choose this and just have one namespace. The default assigned value for
// this namespace is 1.
DefaultNamespaceName string = "default_namespace"
DefaultNamespaceId = uint32(1)
UnknownValue = "unknown"
)

func GetNamespace(ctx context.Context) string {
namespace, err := request.GetNamespace(ctx)
if err != nil {
namespace = "unknown"
}
return namespace
}
2 changes: 2 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/tigrisdata/tigris/server/metrics"
"github.com/tigrisdata/tigris/server/muxer"
"github.com/tigrisdata/tigris/server/quota"
"github.com/tigrisdata/tigris/server/request"
"github.com/tigrisdata/tigris/server/tracing"
"github.com/tigrisdata/tigris/server/transaction"
"github.com/tigrisdata/tigris/store/kv"
Expand Down Expand Up @@ -89,6 +90,7 @@ func mainWithCode() int {
return 1
}

request.Init(tenantMgr)
_ = quota.Init(tenantMgr, &config.DefaultConfig)
defer quota.Cleanup()

Expand Down
14 changes: 9 additions & 5 deletions server/metadata/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/tigrisdata/tigris/errors"
"github.com/tigrisdata/tigris/schema"
"github.com/tigrisdata/tigris/server/config"
"github.com/tigrisdata/tigris/server/request"
"github.com/tigrisdata/tigris/server/defaults"
"github.com/tigrisdata/tigris/server/transaction"
"github.com/tigrisdata/tigris/store/kv"
"github.com/tigrisdata/tigris/store/search"
Expand Down Expand Up @@ -67,19 +67,23 @@ type NamespaceMetadata struct {
// and just have one namespace. The default assigned value for this namespace is 1.
type DefaultNamespace struct{}

type TenantGetter interface {
GetTenant(ctx context.Context, id string) (*Tenant, error)
}

// StrId returns id assigned to the namespace.
func (n *DefaultNamespace) StrId() string {
return request.DefaultNamespaceName
return defaults.DefaultNamespaceName
}

// Id returns id assigned to the namespace.
func (n *DefaultNamespace) Id() uint32 {
return request.DefaultNamespaceId
return defaults.DefaultNamespaceId
}

// Metadata returns metadata assigned to the namespace.
func (n *DefaultNamespace) Metadata() NamespaceMetadata {
return NewNamespaceMetadata(request.DefaultNamespaceId, request.DefaultNamespaceName, request.DefaultNamespaceName)
return NewNamespaceMetadata(defaults.DefaultNamespaceId, defaults.DefaultNamespaceName, defaults.DefaultNamespaceName)
}

func NewDefaultNamespace() *DefaultNamespace {
Expand Down Expand Up @@ -420,7 +424,7 @@ func (m *TenantManager) GetDatabaseAndCollectionId(db string, c string) (uint32,
m.RLock()
defer m.RUnlock()

tenant, ok := m.tenants[request.DefaultNamespaceName]
tenant, ok := m.tenants[defaults.DefaultNamespaceName]
if !ok {
return 0, 0
}
Expand Down
2 changes: 2 additions & 0 deletions server/metrics/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func getAuthOkTagKeys() []string {
"service",
"version",
"tigris_tenant",
"tigris_tenant_name",
}
}

Expand All @@ -44,6 +45,7 @@ func getAuthErrorTagKeys() []string {
"service",
"version",
"tigris_tenant",
"tigris_tenant_name",
"error_source",
"error_value",
}
Expand Down
2 changes: 2 additions & 0 deletions server/metrics/fdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func getFdbOkTagKeys() []string {
return []string{
"grpc_method",
"tigris_tenant",
"tigris_tenant_name",
"env",
"db",
"collection",
Expand All @@ -40,6 +41,7 @@ func getFdbErrorTagKeys() []string {
return []string{
"grpc_method",
"tigris_tenant",
"tigris_tenant_name",
"env",
"db",
"collection",
Expand Down
4 changes: 2 additions & 2 deletions server/metrics/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/rs/zerolog/log"
"github.com/tigrisdata/tigris/server/config"
"github.com/tigrisdata/tigris/server/request"
"github.com/tigrisdata/tigris/server/defaults"
ulog "github.com/tigrisdata/tigris/util/log"
"github.com/uber-go/tally"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -175,7 +175,7 @@ func (m *Measurement) GetSpanOptions() []tracer.StartSpanOption {

func (m *Measurement) AddTags(tags map[string]string) {
for k, v := range tags {
if _, exists := m.tags[k]; !exists || m.tags[k] == request.UnknownValue {
if _, exists := m.tags[k]; !exists || m.tags[k] == defaults.UnknownValue {
m.tags[k] = v
if m.span != nil {
// The span already exists, set the tag there as well
Expand Down
1 change: 1 addition & 0 deletions server/metrics/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func getNetworkTagKeys() []string {
return []string{
"grpc_method",
"tigris_tenant",
"tigris_tenant_name",
"env",
"db",
"collection",
Expand Down
2 changes: 2 additions & 0 deletions server/metrics/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func getRequestOkTagKeys() []string {
return []string{
"grpc_method",
"tigris_tenant",
"tigris_tenant_name",
"env",
"db",
"collection",
Expand All @@ -43,6 +44,7 @@ func getRequestErrorTagKeys() []string {
return []string{
"grpc_method",
"tigris_tenant",
"tigris_tenant_name",
"env",
"db",
"collection",
Expand Down
95 changes: 0 additions & 95 deletions server/metrics/requests_test.go

This file was deleted.

2 changes: 2 additions & 0 deletions server/metrics/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func getSearchOkTagKeys() []string {
return []string{
"grpc_method",
"tigris_tenant",
"tigris_tenant_name",
"env",
"db",
"collection",
Expand All @@ -40,6 +41,7 @@ func getSearchErrorTagKeys() []string {
return []string{
"grpc_method",
"tigris_tenant",
"tigris_tenant_name",
"env",
"db",
"collection",
Expand Down
2 changes: 2 additions & 0 deletions server/metrics/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func getSessionOkTagKeys() []string {
return []string{
"grpc_method",
"tigris_tenant",
"tigris_tenant_name",
"env",
"db",
"collection",
Expand All @@ -40,6 +41,7 @@ func getSessionErrorTagKeys() []string {
return []string{
"grpc_method",
"tigris_tenant",
"tigris_tenant_name",
"env",
"db",
"collection",
Expand Down
36 changes: 21 additions & 15 deletions server/metrics/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func getNameSpaceSizeTagKeys() []string {
"env",
"service",
"tigris_tenant",
"tigris_tenant_name",
"version",
}
}
Expand All @@ -44,6 +45,7 @@ func getDbSizeTagKeys() []string {
"env",
"service",
"tigris_tenant",
"tigris_tenant_name",
"version",
"db",
}
Expand All @@ -54,47 +56,51 @@ func getCollectionSizeTagKeys() []string {
"env",
"service",
"tigris_tenant",
"tigris_tenant_name",
"version",
"db",
"collection",
}
}

func getNamespaceSizeTags(namespaceName string) map[string]string {
func getNamespaceSizeTags(namespace string, namespaceName string) map[string]string {
return map[string]string{
"tigris_tenant": namespaceName,
"tigris_tenant": namespace,
"tigris_tenant_name": GetTenantNameTagValue(namespace, namespaceName),
}
}

func getDbSizeTags(namespaceName string, dbName string) map[string]string {
func getDbSizeTags(namespace string, namespaceName string, dbName string) map[string]string {
return map[string]string{
"tigris_tenant": namespaceName,
"db": dbName,
"tigris_tenant": namespace,
"tigris_tenant_name": GetTenantNameTagValue(namespace, namespaceName),
"db": dbName,
}
}

func getCollectionSizeTags(namespaceName string, dbName string, collectionName string) map[string]string {
func getCollectionSizeTags(namespace string, namespaceName string, dbName string, collectionName string) map[string]string {
return map[string]string{
"tigris_tenant": namespaceName,
"db": dbName,
"collection": collectionName,
"tigris_tenant": namespace,
"tigris_tenant_name": GetTenantNameTagValue(namespace, namespaceName),
"db": dbName,
"collection": collectionName,
}
}

func UpdateNameSpaceSizeMetrics(namespaceName string, size int64) {
func UpdateNameSpaceSizeMetrics(namespace string, namespaceName string, size int64) {
if NamespaceSize != nil {
NamespaceSize.Tagged(getNamespaceSizeTags(namespaceName)).Gauge("bytes").Update(float64(size))
NamespaceSize.Tagged(getNamespaceSizeTags(namespace, namespaceName)).Gauge("bytes").Update(float64(size))
}
}

func UpdateDbSizeMetrics(namespaceName string, dbName string, size int64) {
func UpdateDbSizeMetrics(namespace string, namespaceName string, dbName string, size int64) {
if NamespaceSize != nil {
DbSize.Tagged(getDbSizeTags(namespaceName, dbName)).Gauge("bytes").Update(float64(size))
DbSize.Tagged(getDbSizeTags(namespace, namespaceName, dbName)).Gauge("bytes").Update(float64(size))
}
}

func UpdateCollectionSizeMetrics(namespaceName string, dbName string, collectionName string, size int64) {
func UpdateCollectionSizeMetrics(namespace string, namespaceName string, dbName string, collectionName string, size int64) {
if NamespaceSize != nil {
CollectionSize.Tagged(getCollectionSizeTags(namespaceName, dbName, collectionName)).Gauge("bytes").Update(float64(size))
CollectionSize.Tagged(getCollectionSizeTags(namespace, namespaceName, dbName, collectionName)).Gauge("bytes").Update(float64(size))
}
}
7 changes: 4 additions & 3 deletions server/metrics/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
func TestSizeMetrics(t *testing.T) {
var testSize int64 = 1000
testNamespace := "test_namespace"
testNamespaceName := "test_namespace"
testDb := "test_db"
testCollection := "test_collection"

Expand All @@ -31,14 +32,14 @@ func TestSizeMetrics(t *testing.T) {
InitializeMetrics()

t.Run("Update namespace size metrics", func(t *testing.T) {
UpdateNameSpaceSizeMetrics(testNamespace, testSize)
UpdateNameSpaceSizeMetrics(testNamespace, testNamespaceName, testSize)
})

t.Run("Update database size metrics", func(t *testing.T) {
UpdateDbSizeMetrics(testNamespace, testDb, testSize)
UpdateDbSizeMetrics(testNamespace, testNamespaceName, testDb, testSize)
})

t.Run("Update collection size metrics", func(t *testing.T) {
UpdateCollectionSizeMetrics(testNamespace, testDb, testCollection, testSize)
UpdateCollectionSizeMetrics(testNamespace, testNamespaceName, testDb, testCollection, testSize)
})
}
Loading

0 comments on commit 8b75d32

Please sign in to comment.