You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

trace.go 8.9 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pubsub
  15. import (
  16. "context"
  17. "log"
  18. "sync"
  19. "go.opencensus.io/plugin/ocgrpc"
  20. "go.opencensus.io/stats"
  21. "go.opencensus.io/stats/view"
  22. "go.opencensus.io/tag"
  23. "google.golang.org/api/option"
  24. "google.golang.org/grpc"
  25. )
  26. func openCensusOptions() []option.ClientOption {
  27. return []option.ClientOption{
  28. option.WithGRPCDialOption(grpc.WithStatsHandler(&ocgrpc.ClientHandler{})),
  29. }
  30. }
  31. // The following keys are used to tag requests with a specific topic/subscription ID.
  32. var (
  33. keyTopic = tag.MustNewKey("topic")
  34. keySubscription = tag.MustNewKey("subscription")
  35. )
  36. // In the following, errors are used if status is not "OK".
  37. var (
  38. keyStatus = tag.MustNewKey("status")
  39. keyError = tag.MustNewKey("error")
  40. )
  41. const statsPrefix = "cloud.google.com/go/pubsub/"
  42. // The following are measures recorded in publish/subscribe flows.
  43. var (
  44. // PublishedMessages is a measure of the number of messages published, which may include errors.
  45. // It is EXPERIMENTAL and subject to change or removal without notice.
  46. PublishedMessages = stats.Int64(statsPrefix+"published_messages", "Number of PubSub message published", stats.UnitDimensionless)
  47. // PublishLatency is a measure of the number of milliseconds it took to publish a bundle,
  48. // which may consist of one or more messages.
  49. // It is EXPERIMENTAL and subject to change or removal without notice.
  50. PublishLatency = stats.Float64(statsPrefix+"publish_roundtrip_latency", "The latency in milliseconds per publish batch", stats.UnitMilliseconds)
  51. // PullCount is a measure of the number of messages pulled.
  52. // It is EXPERIMENTAL and subject to change or removal without notice.
  53. PullCount = stats.Int64(statsPrefix+"pull_count", "Number of PubSub messages pulled", stats.UnitDimensionless)
  54. // AckCount is a measure of the number of messages acked.
  55. // It is EXPERIMENTAL and subject to change or removal without notice.
  56. AckCount = stats.Int64(statsPrefix+"ack_count", "Number of PubSub messages acked", stats.UnitDimensionless)
  57. // NackCount is a measure of the number of messages nacked.
  58. // It is EXPERIMENTAL and subject to change or removal without notice.
  59. NackCount = stats.Int64(statsPrefix+"nack_count", "Number of PubSub messages nacked", stats.UnitDimensionless)
  60. // ModAckCount is a measure of the number of messages whose ack-deadline was modified.
  61. // It is EXPERIMENTAL and subject to change or removal without notice.
  62. ModAckCount = stats.Int64(statsPrefix+"mod_ack_count", "Number of ack-deadlines modified", stats.UnitDimensionless)
  63. // ModAckTimeoutCount is a measure of the number ModifyAckDeadline RPCs that timed out.
  64. // It is EXPERIMENTAL and subject to change or removal without notice.
  65. ModAckTimeoutCount = stats.Int64(statsPrefix+"mod_ack_timeout_count", "Number of ModifyAckDeadline RPCs that timed out", stats.UnitDimensionless)
  66. // StreamOpenCount is a measure of the number of times a streaming-pull stream was opened.
  67. // It is EXPERIMENTAL and subject to change or removal without notice.
  68. StreamOpenCount = stats.Int64(statsPrefix+"stream_open_count", "Number of calls opening a new streaming pull", stats.UnitDimensionless)
  69. // StreamRetryCount is a measure of the number of times a streaming-pull operation was retried.
  70. // It is EXPERIMENTAL and subject to change or removal without notice.
  71. StreamRetryCount = stats.Int64(statsPrefix+"stream_retry_count", "Number of retries of a stream send or receive", stats.UnitDimensionless)
  72. // StreamRequestCount is a measure of the number of requests sent on a streaming-pull stream.
  73. // It is EXPERIMENTAL and subject to change or removal without notice.
  74. StreamRequestCount = stats.Int64(statsPrefix+"stream_request_count", "Number gRPC StreamingPull request messages sent", stats.UnitDimensionless)
  75. // StreamResponseCount is a measure of the number of responses received on a streaming-pull stream.
  76. // It is EXPERIMENTAL and subject to change or removal without notice.
  77. StreamResponseCount = stats.Int64(statsPrefix+"stream_response_count", "Number of gRPC StreamingPull response messages received", stats.UnitDimensionless)
  78. )
  79. var (
  80. // PublishedMessagesView is a cumulative sum of PublishedMessages.
  81. // It is EXPERIMENTAL and subject to change or removal without notice.
  82. PublishedMessagesView *view.View
  83. // PublishLatencyView is a distribution of PublishLatency.
  84. // It is EXPERIMENTAL and subject to change or removal without notice.
  85. PublishLatencyView *view.View
  86. // PullCountView is a cumulative sum of PullCount.
  87. // It is EXPERIMENTAL and subject to change or removal without notice.
  88. PullCountView *view.View
  89. // AckCountView is a cumulative sum of AckCount.
  90. // It is EXPERIMENTAL and subject to change or removal without notice.
  91. AckCountView *view.View
  92. // NackCountView is a cumulative sum of NackCount.
  93. // It is EXPERIMENTAL and subject to change or removal without notice.
  94. NackCountView *view.View
  95. // ModAckCountView is a cumulative sum of ModAckCount.
  96. // It is EXPERIMENTAL and subject to change or removal without notice.
  97. ModAckCountView *view.View
  98. // ModAckTimeoutCountView is a cumulative sum of ModAckTimeoutCount.
  99. // It is EXPERIMENTAL and subject to change or removal without notice.
  100. ModAckTimeoutCountView *view.View
  101. // StreamOpenCountView is a cumulative sum of StreamOpenCount.
  102. // It is EXPERIMENTAL and subject to change or removal without notice.
  103. StreamOpenCountView *view.View
  104. // StreamRetryCountView is a cumulative sum of StreamRetryCount.
  105. // It is EXPERIMENTAL and subject to change or removal without notice.
  106. StreamRetryCountView *view.View
  107. // StreamRequestCountView is a cumulative sum of StreamRequestCount.
  108. // It is EXPERIMENTAL and subject to change or removal without notice.
  109. StreamRequestCountView *view.View
  110. // StreamResponseCountView is a cumulative sum of StreamResponseCount.
  111. // It is EXPERIMENTAL and subject to change or removal without notice.
  112. StreamResponseCountView *view.View
  113. )
  114. func init() {
  115. PublishedMessagesView = createCountView(stats.Measure(PublishedMessages), keyTopic, keyStatus, keyError)
  116. PublishLatencyView = createDistView(PublishLatency, keyTopic, keyStatus, keyError)
  117. PullCountView = createCountView(PullCount, keySubscription)
  118. AckCountView = createCountView(AckCount, keySubscription)
  119. NackCountView = createCountView(NackCount, keySubscription)
  120. ModAckCountView = createCountView(ModAckCount, keySubscription)
  121. ModAckTimeoutCountView = createCountView(ModAckTimeoutCount, keySubscription)
  122. StreamOpenCountView = createCountView(StreamOpenCount, keySubscription)
  123. StreamRetryCountView = createCountView(StreamRetryCount, keySubscription)
  124. StreamRequestCountView = createCountView(StreamRequestCount, keySubscription)
  125. StreamResponseCountView = createCountView(StreamResponseCount, keySubscription)
  126. DefaultPublishViews = []*view.View{
  127. PublishedMessagesView,
  128. PublishLatencyView,
  129. }
  130. DefaultSubscribeViews = []*view.View{
  131. PullCountView,
  132. AckCountView,
  133. NackCountView,
  134. ModAckCountView,
  135. ModAckTimeoutCountView,
  136. StreamOpenCountView,
  137. StreamRetryCountView,
  138. StreamRequestCountView,
  139. StreamResponseCountView,
  140. }
  141. }
  142. // The following arrays are the default views related to publish/subscribe operations provided by this package.
  143. // It is EXPERIMENTAL and subject to change or removal without notice.
  144. var (
  145. DefaultPublishViews []*view.View
  146. DefaultSubscribeViews []*view.View
  147. )
  148. func createCountView(m stats.Measure, keys ...tag.Key) *view.View {
  149. return &view.View{
  150. Name: m.Name(),
  151. Description: m.Description(),
  152. TagKeys: keys,
  153. Measure: m,
  154. Aggregation: view.Sum(),
  155. }
  156. }
  157. func createDistView(m stats.Measure, keys ...tag.Key) *view.View {
  158. return &view.View{
  159. Name: m.Name(),
  160. Description: m.Description(),
  161. TagKeys: keys,
  162. Measure: m,
  163. Aggregation: view.Distribution(0, 25, 50, 75, 100, 200, 400, 600, 800, 1000, 2000, 4000, 6000),
  164. }
  165. }
  166. var logOnce sync.Once
  167. // withSubscriptionKey returns a new context modified with the subscriptionKey tag map.
  168. func withSubscriptionKey(ctx context.Context, subName string) context.Context {
  169. ctx, err := tag.New(ctx, tag.Upsert(keySubscription, subName))
  170. if err != nil {
  171. logOnce.Do(func() {
  172. log.Printf("pubsub: error creating tag map for 'subscribe' key: %v", err)
  173. })
  174. }
  175. return ctx
  176. }
  177. func recordStat(ctx context.Context, m *stats.Int64Measure, n int64) {
  178. stats.Record(ctx, m.M(n))
  179. }