Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

151 linhas
5.6 KiB

  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. // +build go1.8
  15. package pubsub
  16. import (
  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. "golang.org/x/net/context"
  24. "google.golang.org/api/option"
  25. "google.golang.org/grpc"
  26. )
  27. func openCensusOptions() []option.ClientOption {
  28. return []option.ClientOption{
  29. option.WithGRPCDialOption(grpc.WithStatsHandler(&ocgrpc.ClientHandler{})),
  30. }
  31. }
  32. var subscriptionKey tag.Key
  33. func init() {
  34. var err error
  35. if subscriptionKey, err = tag.NewKey("subscription"); err != nil {
  36. log.Fatal("cannot create 'subscription' key")
  37. }
  38. }
  39. const statsPrefix = "cloud.google.com/go/pubsub/"
  40. var (
  41. // PullCount is a measure of the number of messages pulled.
  42. // It is EXPERIMENTAL and subject to change or removal without notice.
  43. PullCount = stats.Int64(statsPrefix+"pull_count", "Number of PubSub messages pulled", stats.UnitNone)
  44. // AckCount is a measure of the number of messages acked.
  45. // It is EXPERIMENTAL and subject to change or removal without notice.
  46. AckCount = stats.Int64(statsPrefix+"ack_count", "Number of PubSub messages acked", stats.UnitNone)
  47. // NackCount is a measure of the number of messages nacked.
  48. // It is EXPERIMENTAL and subject to change or removal without notice.
  49. NackCount = stats.Int64(statsPrefix+"nack_count", "Number of PubSub messages nacked", stats.UnitNone)
  50. // ModAckCount is a measure of the number of messages whose ack-deadline was modified.
  51. // It is EXPERIMENTAL and subject to change or removal without notice.
  52. ModAckCount = stats.Int64(statsPrefix+"mod_ack_count", "Number of ack-deadlines modified", stats.UnitNone)
  53. // StreamOpenCount is a measure of the number of times a streaming-pull stream was opened.
  54. // It is EXPERIMENTAL and subject to change or removal without notice.
  55. StreamOpenCount = stats.Int64(statsPrefix+"stream_open_count", "Number of calls opening a new streaming pull", stats.UnitNone)
  56. // StreamRetryCount is a measure of the number of times a streaming-pull operation was retried.
  57. // It is EXPERIMENTAL and subject to change or removal without notice.
  58. StreamRetryCount = stats.Int64(statsPrefix+"stream_retry_count", "Number of retries of a stream send or receive", stats.UnitNone)
  59. // StreamRequestCount is a measure of the number of requests sent on a streaming-pull stream.
  60. // It is EXPERIMENTAL and subject to change or removal without notice.
  61. StreamRequestCount = stats.Int64(statsPrefix+"stream_request_count", "Number gRPC StreamingPull request messages sent", stats.UnitNone)
  62. // StreamResponseCount is a measure of the number of responses received on a streaming-pull stream.
  63. // It is EXPERIMENTAL and subject to change or removal without notice.
  64. StreamResponseCount = stats.Int64(statsPrefix+"stream_response_count", "Number of gRPC StreamingPull response messages received", stats.UnitNone)
  65. // PullCountView is a cumulative sum of PullCount.
  66. // It is EXPERIMENTAL and subject to change or removal without notice.
  67. PullCountView *view.View
  68. // AckCountView is a cumulative sum of AckCount.
  69. // It is EXPERIMENTAL and subject to change or removal without notice.
  70. AckCountView *view.View
  71. // NackCountView is a cumulative sum of NackCount.
  72. // It is EXPERIMENTAL and subject to change or removal without notice.
  73. NackCountView *view.View
  74. // ModAckCountView is a cumulative sum of ModAckCount.
  75. // It is EXPERIMENTAL and subject to change or removal without notice.
  76. ModAckCountView *view.View
  77. // StreamOpenCountView is a cumulative sum of StreamOpenCount.
  78. // It is EXPERIMENTAL and subject to change or removal without notice.
  79. StreamOpenCountView *view.View
  80. // StreamRetryCountView is a cumulative sum of StreamRetryCount.
  81. // It is EXPERIMENTAL and subject to change or removal without notice.
  82. StreamRetryCountView *view.View
  83. // StreamRequestCountView is a cumulative sum of StreamRequestCount.
  84. // It is EXPERIMENTAL and subject to change or removal without notice.
  85. StreamRequestCountView *view.View
  86. // StreamResponseCountView is a cumulative sum of StreamResponseCount.
  87. // It is EXPERIMENTAL and subject to change or removal without notice.
  88. StreamResponseCountView *view.View
  89. )
  90. func init() {
  91. PullCountView = countView(PullCount)
  92. AckCountView = countView(AckCount)
  93. NackCountView = countView(NackCount)
  94. ModAckCountView = countView(ModAckCount)
  95. StreamOpenCountView = countView(StreamOpenCount)
  96. StreamRetryCountView = countView(StreamRetryCount)
  97. StreamRequestCountView = countView(StreamRequestCount)
  98. StreamResponseCountView = countView(StreamResponseCount)
  99. }
  100. func countView(m *stats.Int64Measure) *view.View {
  101. return &view.View{
  102. Name: m.Name(),
  103. Description: m.Description(),
  104. TagKeys: []tag.Key{subscriptionKey},
  105. Measure: m,
  106. Aggregation: view.Sum(),
  107. }
  108. }
  109. var logOnce sync.Once
  110. func withSubscriptionKey(ctx context.Context, subName string) context.Context {
  111. ctx, err := tag.New(ctx, tag.Upsert(subscriptionKey, subName))
  112. if err != nil {
  113. logOnce.Do(func() {
  114. log.Printf("pubsub: error creating tag map: %v", err)
  115. })
  116. }
  117. return ctx
  118. }
  119. func recordStat(ctx context.Context, m *stats.Int64Measure, n int64) {
  120. stats.Record(ctx, m.M(n))
  121. }