github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/trace/details.go (about)

     1  package trace
     2  
     3  import (
     4  	"regexp"
     5  	"sort"
     6  	"strings"
     7  )
     8  
     9  type Detailer interface {
    10  	Details() Details
    11  }
    12  
    13  type Details uint64
    14  
    15  func (d Details) Details() Details {
    16  	return d
    17  }
    18  
    19  func (d Details) String() string {
    20  	ss := make([]string, 0)
    21  	for bit, name := range detailsMap {
    22  		if d&bit != 0 {
    23  			ss = append(ss, name)
    24  		}
    25  	}
    26  	sort.Strings(ss)
    27  
    28  	return strings.Join(ss, "|")
    29  }
    30  
    31  const (
    32  	DriverNetEvents Details = 1 << iota // for bitmask: 1, 2, 4, 8, 16, 32, ...
    33  	DriverConnEvents
    34  	DriverBalancerEvents
    35  	DriverResolverEvents
    36  	DriverRepeaterEvents
    37  	DriverCredentialsEvents
    38  
    39  	TableSessionLifeCycleEvents
    40  	TableSessionQueryInvokeEvents
    41  	TableSessionQueryStreamEvents
    42  	TableSessionTransactionEvents
    43  	TablePoolLifeCycleEvents
    44  	TablePoolSessionLifeCycleEvents
    45  	TablePoolAPIEvents
    46  
    47  	TopicControlPlaneEvents
    48  
    49  	TopicReaderCustomerEvents
    50  
    51  	TopicReaderStreamLifeCycleEvents
    52  	TopicReaderStreamEvents
    53  	TopicReaderMessageEvents
    54  	TopicReaderPartitionEvents
    55  
    56  	TopicWriterStreamLifeCycleEvents
    57  	TopicWriterStreamEvents
    58  
    59  	DatabaseSQLConnectorEvents
    60  	DatabaseSQLConnEvents
    61  	DatabaseSQLTxEvents
    62  	DatabaseSQLStmtEvents
    63  
    64  	RetryEvents
    65  
    66  	DiscoveryEvents
    67  
    68  	SchemeEvents
    69  
    70  	ScriptingEvents
    71  
    72  	RatelimiterEvents
    73  
    74  	CoordinationEvents
    75  
    76  	// Deprecated: has no effect now
    77  	DriverClusterEvents
    78  
    79  	DriverEvents = DriverConnEvents |
    80  		DriverBalancerEvents |
    81  		DriverResolverEvents |
    82  		DriverRepeaterEvents |
    83  		DriverCredentialsEvents
    84  
    85  	TableEvents = TableSessionLifeCycleEvents |
    86  		TableSessionQueryInvokeEvents |
    87  		TableSessionQueryStreamEvents |
    88  		TableSessionTransactionEvents |
    89  		TablePoolLifeCycleEvents |
    90  		TablePoolSessionLifeCycleEvents |
    91  		TablePoolAPIEvents
    92  
    93  	TablePoolEvents = TablePoolLifeCycleEvents |
    94  		TablePoolSessionLifeCycleEvents |
    95  		TablePoolAPIEvents
    96  
    97  	TableSessionQueryEvents = TableSessionQueryInvokeEvents |
    98  		TableSessionQueryStreamEvents
    99  	TableSessionEvents = TableSessionLifeCycleEvents |
   100  		TableSessionQueryEvents |
   101  		TableSessionTransactionEvents
   102  
   103  	TopicReaderEvents = TopicReaderCustomerEvents | TopicReaderStreamEvents | TopicReaderMessageEvents |
   104  		TopicReaderPartitionEvents |
   105  		TopicReaderStreamLifeCycleEvents
   106  
   107  	TopicEvents = TopicControlPlaneEvents | TopicReaderEvents
   108  
   109  	DatabaseSQLEvents = DatabaseSQLConnectorEvents |
   110  		DatabaseSQLConnEvents |
   111  		DatabaseSQLTxEvents |
   112  		DatabaseSQLStmtEvents
   113  
   114  	DetailsAll = ^Details(0) // All bits enabled
   115  )
   116  
   117  var (
   118  	detailsMap = map[Details]string{
   119  		DriverEvents:            "ydb.driver",
   120  		DriverBalancerEvents:    "ydb.driver.balancer",
   121  		DriverResolverEvents:    "ydb.driver.resolver",
   122  		DriverRepeaterEvents:    "ydb.driver.repeater",
   123  		DriverConnEvents:        "ydb.driver.conn",
   124  		DriverCredentialsEvents: "ydb.driver.credentials",
   125  
   126  		DiscoveryEvents: "ydb.discovery",
   127  
   128  		RetryEvents: "ydb.retry",
   129  
   130  		SchemeEvents: "ydb.scheme",
   131  
   132  		ScriptingEvents: "ydb.scripting",
   133  
   134  		CoordinationEvents: "ydb.coordination",
   135  
   136  		RatelimiterEvents: "ydb.ratelimiter",
   137  
   138  		TableEvents:                     "ydb.table",
   139  		TableSessionLifeCycleEvents:     "ydb.table.session",
   140  		TableSessionQueryInvokeEvents:   "ydb.table.session.query.invoke",
   141  		TableSessionQueryStreamEvents:   "ydb.table.session.query.stream",
   142  		TableSessionTransactionEvents:   "ydb.table.session.tx",
   143  		TablePoolLifeCycleEvents:        "ydb.table.pool",
   144  		TablePoolSessionLifeCycleEvents: "ydb.table.pool.session",
   145  		TablePoolAPIEvents:              "ydb.table.pool.api",
   146  
   147  		DatabaseSQLEvents:          "ydb.database.sql",
   148  		DatabaseSQLConnectorEvents: "ydb.database.sql.connector",
   149  		DatabaseSQLConnEvents:      "ydb.database.sql.conn",
   150  		DatabaseSQLTxEvents:        "ydb.database.sql.tx",
   151  		DatabaseSQLStmtEvents:      "ydb.database.sql.stmt",
   152  
   153  		TopicEvents:                      "ydb.topic",
   154  		TopicControlPlaneEvents:          "ydb.topic.controlplane",
   155  		TopicReaderEvents:                "ydb.topic.reader",
   156  		TopicReaderStreamEvents:          "ydb.topic.reader.stream",
   157  		TopicReaderMessageEvents:         "ydb.topic.reader.message",
   158  		TopicReaderPartitionEvents:       "ydb.topic.reader.partition",
   159  		TopicReaderStreamLifeCycleEvents: "ydb.topic.reader.lifecycle",
   160  		TopicWriterStreamLifeCycleEvents: "ydb.topic.writer.lifecycle",
   161  		TopicWriterStreamEvents:          "ydb.topic.writer.stream",
   162  	}
   163  	defaultDetails = DetailsAll
   164  )
   165  
   166  type matchDetailsOptionsHolder struct {
   167  	defaultDetails Details
   168  	posixMatch     bool
   169  }
   170  
   171  type matchDetailsOption func(h *matchDetailsOptionsHolder)
   172  
   173  func WithDefaultDetails(defaultDetails Details) matchDetailsOption {
   174  	return func(h *matchDetailsOptionsHolder) {
   175  		h.defaultDetails = defaultDetails
   176  	}
   177  }
   178  
   179  func WithPOSIXMatch() matchDetailsOption {
   180  	return func(h *matchDetailsOptionsHolder) {
   181  		h.posixMatch = true
   182  	}
   183  }
   184  
   185  func MatchDetails(pattern string, opts ...matchDetailsOption) (d Details) {
   186  	var (
   187  		h = &matchDetailsOptionsHolder{
   188  			defaultDetails: defaultDetails,
   189  		}
   190  		re  *regexp.Regexp
   191  		err error
   192  	)
   193  
   194  	for _, o := range opts {
   195  		if o != nil {
   196  			o(h)
   197  		}
   198  	}
   199  	if h.posixMatch {
   200  		re, err = regexp.CompilePOSIX(pattern)
   201  	} else {
   202  		re, err = regexp.Compile(pattern)
   203  	}
   204  	if err != nil {
   205  		return h.defaultDetails
   206  	}
   207  	for k, v := range detailsMap {
   208  		if re.MatchString(v) {
   209  			d |= k
   210  		}
   211  	}
   212  	if d == 0 {
   213  		return h.defaultDetails
   214  	}
   215  
   216  	return d
   217  }