Integrate BACKBEAT SDK and resolve KACHING license validation
Major integrations and fixes: - Added BACKBEAT SDK integration for P2P operation timing - Implemented beat-aware status tracking for distributed operations - Added Docker secrets support for secure license management - Resolved KACHING license validation via HTTPS/TLS - Updated docker-compose configuration for clean stack deployment - Disabled rollback policies to prevent deployment failures - Added license credential storage (CHORUS-DEV-MULTI-001) Technical improvements: - BACKBEAT P2P operation tracking with phase management - Enhanced configuration system with file-based secrets - Improved error handling for license validation - Clean separation of KACHING and CHORUS deployment stacks 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1
vendor/go.opencensus.io/AUTHORS
generated
vendored
Normal file
1
vendor/go.opencensus.io/AUTHORS
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
Google Inc.
|
||||
202
vendor/go.opencensus.io/LICENSE
generated
vendored
Normal file
202
vendor/go.opencensus.io/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
75
vendor/go.opencensus.io/internal/tagencoding/tagencoding.go
generated
vendored
Normal file
75
vendor/go.opencensus.io/internal/tagencoding/tagencoding.go
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
// Package tagencoding contains the tag encoding
|
||||
// used interally by the stats collector.
|
||||
package tagencoding // import "go.opencensus.io/internal/tagencoding"
|
||||
|
||||
// Values represent the encoded buffer for the values.
|
||||
type Values struct {
|
||||
Buffer []byte
|
||||
WriteIndex int
|
||||
ReadIndex int
|
||||
}
|
||||
|
||||
func (vb *Values) growIfRequired(expected int) {
|
||||
if len(vb.Buffer)-vb.WriteIndex < expected {
|
||||
tmp := make([]byte, 2*(len(vb.Buffer)+1)+expected)
|
||||
copy(tmp, vb.Buffer)
|
||||
vb.Buffer = tmp
|
||||
}
|
||||
}
|
||||
|
||||
// WriteValue is the helper method to encode Values from map[Key][]byte.
|
||||
func (vb *Values) WriteValue(v []byte) {
|
||||
length := len(v) & 0xff
|
||||
vb.growIfRequired(1 + length)
|
||||
|
||||
// writing length of v
|
||||
vb.Buffer[vb.WriteIndex] = byte(length)
|
||||
vb.WriteIndex++
|
||||
|
||||
if length == 0 {
|
||||
// No value was encoded for this key
|
||||
return
|
||||
}
|
||||
|
||||
// writing v
|
||||
copy(vb.Buffer[vb.WriteIndex:], v[:length])
|
||||
vb.WriteIndex += length
|
||||
}
|
||||
|
||||
// ReadValue is the helper method to decode Values to a map[Key][]byte.
|
||||
func (vb *Values) ReadValue() []byte {
|
||||
// read length of v
|
||||
length := int(vb.Buffer[vb.ReadIndex])
|
||||
vb.ReadIndex++
|
||||
if length == 0 {
|
||||
// No value was encoded for this key
|
||||
return nil
|
||||
}
|
||||
|
||||
// read value of v
|
||||
v := make([]byte, length)
|
||||
endIdx := vb.ReadIndex + length
|
||||
copy(v, vb.Buffer[vb.ReadIndex:endIdx])
|
||||
vb.ReadIndex = endIdx
|
||||
return v
|
||||
}
|
||||
|
||||
// Bytes returns a reference to already written bytes in the Buffer.
|
||||
func (vb *Values) Bytes() []byte {
|
||||
return vb.Buffer[:vb.WriteIndex]
|
||||
}
|
||||
19
vendor/go.opencensus.io/metric/metricdata/doc.go
generated
vendored
Normal file
19
vendor/go.opencensus.io/metric/metricdata/doc.go
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package metricdata contains the metrics data model.
|
||||
//
|
||||
// This is an EXPERIMENTAL package, and may change in arbitrary ways without
|
||||
// notice.
|
||||
package metricdata // import "go.opencensus.io/metric/metricdata"
|
||||
38
vendor/go.opencensus.io/metric/metricdata/exemplar.go
generated
vendored
Normal file
38
vendor/go.opencensus.io/metric/metricdata/exemplar.go
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metricdata
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Exemplars keys.
|
||||
const (
|
||||
AttachmentKeySpanContext = "SpanContext"
|
||||
)
|
||||
|
||||
// Exemplar is an example data point associated with each bucket of a
|
||||
// distribution type aggregation.
|
||||
//
|
||||
// Their purpose is to provide an example of the kind of thing
|
||||
// (request, RPC, trace span, etc.) that resulted in that measurement.
|
||||
type Exemplar struct {
|
||||
Value float64 // the value that was recorded
|
||||
Timestamp time.Time // the time the value was recorded
|
||||
Attachments Attachments // attachments (if any)
|
||||
}
|
||||
|
||||
// Attachments is a map of extra values associated with a recorded data point.
|
||||
type Attachments map[string]interface{}
|
||||
35
vendor/go.opencensus.io/metric/metricdata/label.go
generated
vendored
Normal file
35
vendor/go.opencensus.io/metric/metricdata/label.go
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metricdata
|
||||
|
||||
// LabelKey represents key of a label. It has optional
|
||||
// description attribute.
|
||||
type LabelKey struct {
|
||||
Key string
|
||||
Description string
|
||||
}
|
||||
|
||||
// LabelValue represents the value of a label.
|
||||
// The zero value represents a missing label value, which may be treated
|
||||
// differently to an empty string value by some back ends.
|
||||
type LabelValue struct {
|
||||
Value string // string value of the label
|
||||
Present bool // flag that indicated whether a value is present or not
|
||||
}
|
||||
|
||||
// NewLabelValue creates a new non-nil LabelValue that represents the given string.
|
||||
func NewLabelValue(val string) LabelValue {
|
||||
return LabelValue{Value: val, Present: true}
|
||||
}
|
||||
46
vendor/go.opencensus.io/metric/metricdata/metric.go
generated
vendored
Normal file
46
vendor/go.opencensus.io/metric/metricdata/metric.go
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metricdata
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.opencensus.io/resource"
|
||||
)
|
||||
|
||||
// Descriptor holds metadata about a metric.
|
||||
type Descriptor struct {
|
||||
Name string // full name of the metric
|
||||
Description string // human-readable description
|
||||
Unit Unit // units for the measure
|
||||
Type Type // type of measure
|
||||
LabelKeys []LabelKey // label keys
|
||||
}
|
||||
|
||||
// Metric represents a quantity measured against a resource with different
|
||||
// label value combinations.
|
||||
type Metric struct {
|
||||
Descriptor Descriptor // metric descriptor
|
||||
Resource *resource.Resource // resource against which this was measured
|
||||
TimeSeries []*TimeSeries // one time series for each combination of label values
|
||||
}
|
||||
|
||||
// TimeSeries is a sequence of points associated with a combination of label
|
||||
// values.
|
||||
type TimeSeries struct {
|
||||
LabelValues []LabelValue // label values, same order as keys in the metric descriptor
|
||||
Points []Point // points sequence
|
||||
StartTime time.Time // time we started recording this time series
|
||||
}
|
||||
193
vendor/go.opencensus.io/metric/metricdata/point.go
generated
vendored
Normal file
193
vendor/go.opencensus.io/metric/metricdata/point.go
generated
vendored
Normal file
@@ -0,0 +1,193 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metricdata
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Point is a single data point of a time series.
|
||||
type Point struct {
|
||||
// Time is the point in time that this point represents in a time series.
|
||||
Time time.Time
|
||||
// Value is the value of this point. Prefer using ReadValue to switching on
|
||||
// the value type, since new value types might be added.
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
//go:generate stringer -type ValueType
|
||||
|
||||
// NewFloat64Point creates a new Point holding a float64 value.
|
||||
func NewFloat64Point(t time.Time, val float64) Point {
|
||||
return Point{
|
||||
Value: val,
|
||||
Time: t,
|
||||
}
|
||||
}
|
||||
|
||||
// NewInt64Point creates a new Point holding an int64 value.
|
||||
func NewInt64Point(t time.Time, val int64) Point {
|
||||
return Point{
|
||||
Value: val,
|
||||
Time: t,
|
||||
}
|
||||
}
|
||||
|
||||
// NewDistributionPoint creates a new Point holding a Distribution value.
|
||||
func NewDistributionPoint(t time.Time, val *Distribution) Point {
|
||||
return Point{
|
||||
Value: val,
|
||||
Time: t,
|
||||
}
|
||||
}
|
||||
|
||||
// NewSummaryPoint creates a new Point holding a Summary value.
|
||||
func NewSummaryPoint(t time.Time, val *Summary) Point {
|
||||
return Point{
|
||||
Value: val,
|
||||
Time: t,
|
||||
}
|
||||
}
|
||||
|
||||
// ValueVisitor allows reading the value of a point.
|
||||
type ValueVisitor interface {
|
||||
VisitFloat64Value(float64)
|
||||
VisitInt64Value(int64)
|
||||
VisitDistributionValue(*Distribution)
|
||||
VisitSummaryValue(*Summary)
|
||||
}
|
||||
|
||||
// ReadValue accepts a ValueVisitor and calls the appropriate method with the
|
||||
// value of this point.
|
||||
// Consumers of Point should use this in preference to switching on the type
|
||||
// of the value directly, since new value types may be added.
|
||||
func (p Point) ReadValue(vv ValueVisitor) {
|
||||
switch v := p.Value.(type) {
|
||||
case int64:
|
||||
vv.VisitInt64Value(v)
|
||||
case float64:
|
||||
vv.VisitFloat64Value(v)
|
||||
case *Distribution:
|
||||
vv.VisitDistributionValue(v)
|
||||
case *Summary:
|
||||
vv.VisitSummaryValue(v)
|
||||
default:
|
||||
panic("unexpected value type")
|
||||
}
|
||||
}
|
||||
|
||||
// Distribution contains summary statistics for a population of values. It
|
||||
// optionally contains a histogram representing the distribution of those
|
||||
// values across a set of buckets.
|
||||
type Distribution struct {
|
||||
// Count is the number of values in the population. Must be non-negative. This value
|
||||
// must equal the sum of the values in bucket_counts if a histogram is
|
||||
// provided.
|
||||
Count int64
|
||||
// Sum is the sum of the values in the population. If count is zero then this field
|
||||
// must be zero.
|
||||
Sum float64
|
||||
// SumOfSquaredDeviation is the sum of squared deviations from the mean of the values in the
|
||||
// population. For values x_i this is:
|
||||
//
|
||||
// Sum[i=1..n]((x_i - mean)^2)
|
||||
//
|
||||
// Knuth, "The Art of Computer Programming", Vol. 2, page 323, 3rd edition
|
||||
// describes Welford's method for accumulating this sum in one pass.
|
||||
//
|
||||
// If count is zero then this field must be zero.
|
||||
SumOfSquaredDeviation float64
|
||||
// BucketOptions describes the bounds of the histogram buckets in this
|
||||
// distribution.
|
||||
//
|
||||
// A Distribution may optionally contain a histogram of the values in the
|
||||
// population.
|
||||
//
|
||||
// If nil, there is no associated histogram.
|
||||
BucketOptions *BucketOptions
|
||||
// Bucket If the distribution does not have a histogram, then omit this field.
|
||||
// If there is a histogram, then the sum of the values in the Bucket counts
|
||||
// must equal the value in the count field of the distribution.
|
||||
Buckets []Bucket
|
||||
}
|
||||
|
||||
// BucketOptions describes the bounds of the histogram buckets in this
|
||||
// distribution.
|
||||
type BucketOptions struct {
|
||||
// Bounds specifies a set of bucket upper bounds.
|
||||
// This defines len(bounds) + 1 (= N) buckets. The boundaries for bucket
|
||||
// index i are:
|
||||
//
|
||||
// [0, Bounds[i]) for i == 0
|
||||
// [Bounds[i-1], Bounds[i]) for 0 < i < N-1
|
||||
// [Bounds[i-1], +infinity) for i == N-1
|
||||
Bounds []float64
|
||||
}
|
||||
|
||||
// Bucket represents a single bucket (value range) in a distribution.
|
||||
type Bucket struct {
|
||||
// Count is the number of values in each bucket of the histogram, as described in
|
||||
// bucket_bounds.
|
||||
Count int64
|
||||
// Exemplar associated with this bucket (if any).
|
||||
Exemplar *Exemplar
|
||||
}
|
||||
|
||||
// Summary is a representation of percentiles.
|
||||
type Summary struct {
|
||||
// Count is the cumulative count (if available).
|
||||
Count int64
|
||||
// Sum is the cumulative sum of values (if available).
|
||||
Sum float64
|
||||
// HasCountAndSum is true if Count and Sum are available.
|
||||
HasCountAndSum bool
|
||||
// Snapshot represents percentiles calculated over an arbitrary time window.
|
||||
// The values in this struct can be reset at arbitrary unknown times, with
|
||||
// the requirement that all of them are reset at the same time.
|
||||
Snapshot Snapshot
|
||||
}
|
||||
|
||||
// Snapshot represents percentiles over an arbitrary time.
|
||||
// The values in this struct can be reset at arbitrary unknown times, with
|
||||
// the requirement that all of them are reset at the same time.
|
||||
type Snapshot struct {
|
||||
// Count is the number of values in the snapshot. Optional since some systems don't
|
||||
// expose this. Set to 0 if not available.
|
||||
Count int64
|
||||
// Sum is the sum of values in the snapshot. Optional since some systems don't
|
||||
// expose this. If count is 0 then this field must be zero.
|
||||
Sum float64
|
||||
// Percentiles is a map from percentile (range (0-100.0]) to the value of
|
||||
// the percentile.
|
||||
Percentiles map[float64]float64
|
||||
}
|
||||
|
||||
//go:generate stringer -type Type
|
||||
|
||||
// Type is the overall type of metric, including its value type and whether it
|
||||
// represents a cumulative total (since the start time) or if it represents a
|
||||
// gauge value.
|
||||
type Type int
|
||||
|
||||
// Metric types.
|
||||
const (
|
||||
TypeGaugeInt64 Type = iota
|
||||
TypeGaugeFloat64
|
||||
TypeGaugeDistribution
|
||||
TypeCumulativeInt64
|
||||
TypeCumulativeFloat64
|
||||
TypeCumulativeDistribution
|
||||
TypeSummary
|
||||
)
|
||||
16
vendor/go.opencensus.io/metric/metricdata/type_string.go
generated
vendored
Normal file
16
vendor/go.opencensus.io/metric/metricdata/type_string.go
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
// Code generated by "stringer -type Type"; DO NOT EDIT.
|
||||
|
||||
package metricdata
|
||||
|
||||
import "strconv"
|
||||
|
||||
const _Type_name = "TypeGaugeInt64TypeGaugeFloat64TypeGaugeDistributionTypeCumulativeInt64TypeCumulativeFloat64TypeCumulativeDistributionTypeSummary"
|
||||
|
||||
var _Type_index = [...]uint8{0, 14, 30, 51, 70, 91, 117, 128}
|
||||
|
||||
func (i Type) String() string {
|
||||
if i < 0 || i >= Type(len(_Type_index)-1) {
|
||||
return "Type(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _Type_name[_Type_index[i]:_Type_index[i+1]]
|
||||
}
|
||||
27
vendor/go.opencensus.io/metric/metricdata/unit.go
generated
vendored
Normal file
27
vendor/go.opencensus.io/metric/metricdata/unit.go
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metricdata
|
||||
|
||||
// Unit is a string encoded according to the case-sensitive abbreviations from the
|
||||
// Unified Code for Units of Measure: http://unitsofmeasure.org/ucum.html
|
||||
type Unit string
|
||||
|
||||
// Predefined units. To record against a unit not represented here, create your
|
||||
// own Unit type constant from a string.
|
||||
const (
|
||||
UnitDimensionless Unit = "1"
|
||||
UnitBytes Unit = "By"
|
||||
UnitMilliseconds Unit = "ms"
|
||||
)
|
||||
78
vendor/go.opencensus.io/metric/metricproducer/manager.go
generated
vendored
Normal file
78
vendor/go.opencensus.io/metric/metricproducer/manager.go
generated
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
// Copyright 2019, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metricproducer
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Manager maintains a list of active producers. Producers can register
|
||||
// with the manager to allow readers to read all metrics provided by them.
|
||||
// Readers can retrieve all producers registered with the manager,
|
||||
// read metrics from the producers and export them.
|
||||
type Manager struct {
|
||||
mu sync.RWMutex
|
||||
producers map[Producer]struct{}
|
||||
}
|
||||
|
||||
var prodMgr *Manager
|
||||
var once sync.Once
|
||||
|
||||
// GlobalManager is a single instance of producer manager
|
||||
// that is used by all producers and all readers.
|
||||
func GlobalManager() *Manager {
|
||||
once.Do(func() {
|
||||
prodMgr = &Manager{}
|
||||
prodMgr.producers = make(map[Producer]struct{})
|
||||
})
|
||||
return prodMgr
|
||||
}
|
||||
|
||||
// AddProducer adds the producer to the Manager if it is not already present.
|
||||
func (pm *Manager) AddProducer(producer Producer) {
|
||||
if producer == nil {
|
||||
return
|
||||
}
|
||||
pm.mu.Lock()
|
||||
defer pm.mu.Unlock()
|
||||
pm.producers[producer] = struct{}{}
|
||||
}
|
||||
|
||||
// DeleteProducer deletes the producer from the Manager if it is present.
|
||||
func (pm *Manager) DeleteProducer(producer Producer) {
|
||||
if producer == nil {
|
||||
return
|
||||
}
|
||||
pm.mu.Lock()
|
||||
defer pm.mu.Unlock()
|
||||
delete(pm.producers, producer)
|
||||
}
|
||||
|
||||
// GetAll returns a slice of all producer currently registered with
|
||||
// the Manager. For each call it generates a new slice. The slice
|
||||
// should not be cached as registration may change at any time. It is
|
||||
// typically called periodically by exporter to read metrics from
|
||||
// the producers.
|
||||
func (pm *Manager) GetAll() []Producer {
|
||||
pm.mu.Lock()
|
||||
defer pm.mu.Unlock()
|
||||
producers := make([]Producer, len(pm.producers))
|
||||
i := 0
|
||||
for producer := range pm.producers {
|
||||
producers[i] = producer
|
||||
i++
|
||||
}
|
||||
return producers
|
||||
}
|
||||
28
vendor/go.opencensus.io/metric/metricproducer/producer.go
generated
vendored
Normal file
28
vendor/go.opencensus.io/metric/metricproducer/producer.go
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright 2019, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package metricproducer
|
||||
|
||||
import (
|
||||
"go.opencensus.io/metric/metricdata"
|
||||
)
|
||||
|
||||
// Producer is a source of metrics.
|
||||
type Producer interface {
|
||||
// Read should return the current values of all metrics supported by this
|
||||
// metric provider.
|
||||
// The returned metrics should be unique for each combination of name and
|
||||
// resource.
|
||||
Read() []*metricdata.Metric
|
||||
}
|
||||
164
vendor/go.opencensus.io/resource/resource.go
generated
vendored
Normal file
164
vendor/go.opencensus.io/resource/resource.go
generated
vendored
Normal file
@@ -0,0 +1,164 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package resource provides functionality for resource, which capture
|
||||
// identifying information about the entities for which signals are exported.
|
||||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Environment variables used by FromEnv to decode a resource.
|
||||
const (
|
||||
EnvVarType = "OC_RESOURCE_TYPE"
|
||||
EnvVarLabels = "OC_RESOURCE_LABELS"
|
||||
)
|
||||
|
||||
// Resource describes an entity about which identifying information and metadata is exposed.
|
||||
// For example, a type "k8s.io/container" may hold labels describing the pod name and namespace.
|
||||
type Resource struct {
|
||||
Type string
|
||||
Labels map[string]string
|
||||
}
|
||||
|
||||
// EncodeLabels encodes a labels map to a string as provided via the OC_RESOURCE_LABELS environment variable.
|
||||
func EncodeLabels(labels map[string]string) string {
|
||||
sortedKeys := make([]string, 0, len(labels))
|
||||
for k := range labels {
|
||||
sortedKeys = append(sortedKeys, k)
|
||||
}
|
||||
sort.Strings(sortedKeys)
|
||||
|
||||
s := ""
|
||||
for i, k := range sortedKeys {
|
||||
if i > 0 {
|
||||
s += ","
|
||||
}
|
||||
s += k + "=" + strconv.Quote(labels[k])
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
var labelRegex = regexp.MustCompile(`^\s*([[:ascii:]]{1,256}?)=("[[:ascii:]]{0,256}?")\s*,`)
|
||||
|
||||
// DecodeLabels decodes a serialized label map as used in the OC_RESOURCE_LABELS variable.
|
||||
// A list of labels of the form `<key1>="<value1>",<key2>="<value2>",...` is accepted.
|
||||
// Domain names and paths are accepted as label keys.
|
||||
// Most users will want to use FromEnv instead.
|
||||
func DecodeLabels(s string) (map[string]string, error) {
|
||||
m := map[string]string{}
|
||||
// Ensure a trailing comma, which allows us to keep the regex simpler
|
||||
s = strings.TrimRight(strings.TrimSpace(s), ",") + ","
|
||||
|
||||
for len(s) > 0 {
|
||||
match := labelRegex.FindStringSubmatch(s)
|
||||
if len(match) == 0 {
|
||||
return nil, fmt.Errorf("invalid label formatting, remainder: %s", s)
|
||||
}
|
||||
v := match[2]
|
||||
if v == "" {
|
||||
v = match[3]
|
||||
} else {
|
||||
var err error
|
||||
if v, err = strconv.Unquote(v); err != nil {
|
||||
return nil, fmt.Errorf("invalid label formatting, remainder: %s, err: %s", s, err)
|
||||
}
|
||||
}
|
||||
m[match[1]] = v
|
||||
|
||||
s = s[len(match[0]):]
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// FromEnv is a detector that loads resource information from the OC_RESOURCE_TYPE
|
||||
// and OC_RESOURCE_labelS environment variables.
|
||||
func FromEnv(context.Context) (*Resource, error) {
|
||||
res := &Resource{
|
||||
Type: strings.TrimSpace(os.Getenv(EnvVarType)),
|
||||
}
|
||||
labels := strings.TrimSpace(os.Getenv(EnvVarLabels))
|
||||
if labels == "" {
|
||||
return res, nil
|
||||
}
|
||||
var err error
|
||||
if res.Labels, err = DecodeLabels(labels); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
var _ Detector = FromEnv
|
||||
|
||||
// merge resource information from b into a. In case of a collision, a takes precedence.
|
||||
func merge(a, b *Resource) *Resource {
|
||||
if a == nil {
|
||||
return b
|
||||
}
|
||||
if b == nil {
|
||||
return a
|
||||
}
|
||||
res := &Resource{
|
||||
Type: a.Type,
|
||||
Labels: map[string]string{},
|
||||
}
|
||||
if res.Type == "" {
|
||||
res.Type = b.Type
|
||||
}
|
||||
for k, v := range b.Labels {
|
||||
res.Labels[k] = v
|
||||
}
|
||||
// Labels from resource a overwrite labels from resource b.
|
||||
for k, v := range a.Labels {
|
||||
res.Labels[k] = v
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// Detector attempts to detect resource information.
|
||||
// If the detector cannot find resource information, the returned resource is nil but no
|
||||
// error is returned.
|
||||
// An error is only returned on unexpected failures.
|
||||
type Detector func(context.Context) (*Resource, error)
|
||||
|
||||
// MultiDetector returns a Detector that calls all input detectors in order and
|
||||
// merges each result with the previous one. In case a type of label key is already set,
|
||||
// the first set value is takes precedence.
|
||||
// It returns on the first error that a sub-detector encounters.
|
||||
func MultiDetector(detectors ...Detector) Detector {
|
||||
return func(ctx context.Context) (*Resource, error) {
|
||||
return detectAll(ctx, detectors...)
|
||||
}
|
||||
}
|
||||
|
||||
// detectall calls all input detectors sequentially an merges each result with the previous one.
|
||||
// It returns on the first error that a sub-detector encounters.
|
||||
func detectAll(ctx context.Context, detectors ...Detector) (*Resource, error) {
|
||||
var res *Resource
|
||||
for _, d := range detectors {
|
||||
r, err := d(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res = merge(res, r)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
68
vendor/go.opencensus.io/stats/doc.go
generated
vendored
Normal file
68
vendor/go.opencensus.io/stats/doc.go
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/*
|
||||
Package stats contains support for OpenCensus stats recording.
|
||||
|
||||
OpenCensus allows users to create typed measures, record measurements,
|
||||
aggregate the collected data, and export the aggregated data.
|
||||
|
||||
# Measures
|
||||
|
||||
A measure represents a type of data point to be tracked and recorded.
|
||||
For example, latency, request Mb/s, and response Mb/s are measures
|
||||
to collect from a server.
|
||||
|
||||
Measure constructors such as Int64 and Float64 automatically
|
||||
register the measure by the given name. Each registered measure needs
|
||||
to be unique by name. Measures also have a description and a unit.
|
||||
|
||||
Libraries can define and export measures. Application authors can then
|
||||
create views and collect and break down measures by the tags they are
|
||||
interested in.
|
||||
|
||||
# Recording measurements
|
||||
|
||||
Measurement is a data point to be collected for a measure. For example,
|
||||
for a latency (ms) measure, 100 is a measurement that represents a 100ms
|
||||
latency event. Measurements are created from measures with
|
||||
the current context. Tags from the current context are recorded with the
|
||||
measurements if they are any.
|
||||
|
||||
Recorded measurements are dropped immediately if no views are registered for them.
|
||||
There is usually no need to conditionally enable and disable
|
||||
recording to reduce cost. Recording of measurements is cheap.
|
||||
|
||||
Libraries can always record measurements, and applications can later decide
|
||||
on which measurements they want to collect by registering views. This allows
|
||||
libraries to turn on the instrumentation by default.
|
||||
|
||||
# Exemplars
|
||||
|
||||
For a given recorded measurement, the associated exemplar is a diagnostic map
|
||||
that gives more information about the measurement.
|
||||
|
||||
When aggregated using a Distribution aggregation, an exemplar is kept for each
|
||||
bucket in the Distribution. This allows you to easily find an example of a
|
||||
measurement that fell into each bucket.
|
||||
|
||||
For example, if you also use the OpenCensus trace package and you
|
||||
record a measurement with a context that contains a sampled trace span,
|
||||
then the trace span will be added to the exemplar associated with the measurement.
|
||||
|
||||
When exported to a supporting back end, you should be able to easily navigate
|
||||
to example traces that fell into each bucket in the Distribution.
|
||||
*/
|
||||
package stats // import "go.opencensus.io/stats"
|
||||
31
vendor/go.opencensus.io/stats/internal/record.go
generated
vendored
Normal file
31
vendor/go.opencensus.io/stats/internal/record.go
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"go.opencensus.io/tag"
|
||||
)
|
||||
|
||||
// DefaultRecorder will be called for each Record call.
|
||||
var DefaultRecorder func(tags *tag.Map, measurement interface{}, attachments map[string]interface{})
|
||||
|
||||
// MeasurementRecorder will be called for each Record call. This is the same as DefaultRecorder but
|
||||
// avoids interface{} conversion.
|
||||
// This will be a func(tags *tag.Map, measurement []Measurement, attachments map[string]interface{}) type,
|
||||
// but is interface{} here to avoid import loops
|
||||
var MeasurementRecorder interface{}
|
||||
|
||||
// SubscriptionReporter reports when a view subscribed with a measure.
|
||||
var SubscriptionReporter func(measure string)
|
||||
109
vendor/go.opencensus.io/stats/measure.go
generated
vendored
Normal file
109
vendor/go.opencensus.io/stats/measure.go
generated
vendored
Normal file
@@ -0,0 +1,109 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package stats
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Measure represents a single numeric value to be tracked and recorded.
|
||||
// For example, latency, request bytes, and response bytes could be measures
|
||||
// to collect from a server.
|
||||
//
|
||||
// Measures by themselves have no outside effects. In order to be exported,
|
||||
// the measure needs to be used in a View. If no Views are defined over a
|
||||
// measure, there is very little cost in recording it.
|
||||
type Measure interface {
|
||||
// Name returns the name of this measure.
|
||||
//
|
||||
// Measure names are globally unique (among all libraries linked into your program).
|
||||
// We recommend prefixing the measure name with a domain name relevant to your
|
||||
// project or application.
|
||||
//
|
||||
// Measure names are never sent over the wire or exported to backends.
|
||||
// They are only used to create Views.
|
||||
Name() string
|
||||
|
||||
// Description returns the human-readable description of this measure.
|
||||
Description() string
|
||||
|
||||
// Unit returns the units for the values this measure takes on.
|
||||
//
|
||||
// Units are encoded according to the case-sensitive abbreviations from the
|
||||
// Unified Code for Units of Measure: http://unitsofmeasure.org/ucum.html
|
||||
Unit() string
|
||||
}
|
||||
|
||||
// measureDescriptor is the untyped descriptor associated with each measure.
|
||||
// Int64Measure and Float64Measure wrap measureDescriptor to provide typed
|
||||
// recording APIs.
|
||||
// Two Measures with the same name will have the same measureDescriptor.
|
||||
type measureDescriptor struct {
|
||||
subs int32 // access atomically
|
||||
|
||||
name string
|
||||
description string
|
||||
unit string
|
||||
}
|
||||
|
||||
func (m *measureDescriptor) subscribe() {
|
||||
atomic.StoreInt32(&m.subs, 1)
|
||||
}
|
||||
|
||||
func (m *measureDescriptor) subscribed() bool {
|
||||
return atomic.LoadInt32(&m.subs) == 1
|
||||
}
|
||||
|
||||
var (
|
||||
mu sync.RWMutex
|
||||
measures = make(map[string]*measureDescriptor)
|
||||
)
|
||||
|
||||
func registerMeasureHandle(name, desc, unit string) *measureDescriptor {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if stored, ok := measures[name]; ok {
|
||||
return stored
|
||||
}
|
||||
m := &measureDescriptor{
|
||||
name: name,
|
||||
description: desc,
|
||||
unit: unit,
|
||||
}
|
||||
measures[name] = m
|
||||
return m
|
||||
}
|
||||
|
||||
// Measurement is the numeric value measured when recording stats. Each measure
|
||||
// provides methods to create measurements of their kind. For example, Int64Measure
|
||||
// provides M to convert an int64 into a measurement.
|
||||
type Measurement struct {
|
||||
v float64
|
||||
m Measure
|
||||
desc *measureDescriptor
|
||||
}
|
||||
|
||||
// Value returns the value of the Measurement as a float64.
|
||||
func (m Measurement) Value() float64 {
|
||||
return m.v
|
||||
}
|
||||
|
||||
// Measure returns the Measure from which this Measurement was created.
|
||||
func (m Measurement) Measure() Measure {
|
||||
return m.m
|
||||
}
|
||||
55
vendor/go.opencensus.io/stats/measure_float64.go
generated
vendored
Normal file
55
vendor/go.opencensus.io/stats/measure_float64.go
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package stats
|
||||
|
||||
// Float64Measure is a measure for float64 values.
|
||||
type Float64Measure struct {
|
||||
desc *measureDescriptor
|
||||
}
|
||||
|
||||
// M creates a new float64 measurement.
|
||||
// Use Record to record measurements.
|
||||
func (m *Float64Measure) M(v float64) Measurement {
|
||||
return Measurement{
|
||||
m: m,
|
||||
desc: m.desc,
|
||||
v: v,
|
||||
}
|
||||
}
|
||||
|
||||
// Float64 creates a new measure for float64 values.
|
||||
//
|
||||
// See the documentation for interface Measure for more guidance on the
|
||||
// parameters of this function.
|
||||
func Float64(name, description, unit string) *Float64Measure {
|
||||
mi := registerMeasureHandle(name, description, unit)
|
||||
return &Float64Measure{mi}
|
||||
}
|
||||
|
||||
// Name returns the name of the measure.
|
||||
func (m *Float64Measure) Name() string {
|
||||
return m.desc.name
|
||||
}
|
||||
|
||||
// Description returns the description of the measure.
|
||||
func (m *Float64Measure) Description() string {
|
||||
return m.desc.description
|
||||
}
|
||||
|
||||
// Unit returns the unit of the measure.
|
||||
func (m *Float64Measure) Unit() string {
|
||||
return m.desc.unit
|
||||
}
|
||||
55
vendor/go.opencensus.io/stats/measure_int64.go
generated
vendored
Normal file
55
vendor/go.opencensus.io/stats/measure_int64.go
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package stats
|
||||
|
||||
// Int64Measure is a measure for int64 values.
|
||||
type Int64Measure struct {
|
||||
desc *measureDescriptor
|
||||
}
|
||||
|
||||
// M creates a new int64 measurement.
|
||||
// Use Record to record measurements.
|
||||
func (m *Int64Measure) M(v int64) Measurement {
|
||||
return Measurement{
|
||||
m: m,
|
||||
desc: m.desc,
|
||||
v: float64(v),
|
||||
}
|
||||
}
|
||||
|
||||
// Int64 creates a new measure for int64 values.
|
||||
//
|
||||
// See the documentation for interface Measure for more guidance on the
|
||||
// parameters of this function.
|
||||
func Int64(name, description, unit string) *Int64Measure {
|
||||
mi := registerMeasureHandle(name, description, unit)
|
||||
return &Int64Measure{mi}
|
||||
}
|
||||
|
||||
// Name returns the name of the measure.
|
||||
func (m *Int64Measure) Name() string {
|
||||
return m.desc.name
|
||||
}
|
||||
|
||||
// Description returns the description of the measure.
|
||||
func (m *Int64Measure) Description() string {
|
||||
return m.desc.description
|
||||
}
|
||||
|
||||
// Unit returns the unit of the measure.
|
||||
func (m *Int64Measure) Unit() string {
|
||||
return m.desc.unit
|
||||
}
|
||||
156
vendor/go.opencensus.io/stats/record.go
generated
vendored
Normal file
156
vendor/go.opencensus.io/stats/record.go
generated
vendored
Normal file
@@ -0,0 +1,156 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package stats
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opencensus.io/metric/metricdata"
|
||||
"go.opencensus.io/stats/internal"
|
||||
"go.opencensus.io/tag"
|
||||
)
|
||||
|
||||
func init() {
|
||||
internal.SubscriptionReporter = func(measure string) {
|
||||
mu.Lock()
|
||||
measures[measure].subscribe()
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// Recorder provides an interface for exporting measurement information from
|
||||
// the static Record method by using the WithRecorder option.
|
||||
type Recorder interface {
|
||||
// Record records a set of measurements associated with the given tags and attachments.
|
||||
// The second argument is a `[]Measurement`.
|
||||
Record(*tag.Map, interface{}, map[string]interface{})
|
||||
}
|
||||
|
||||
type recordOptions struct {
|
||||
attachments metricdata.Attachments
|
||||
mutators []tag.Mutator
|
||||
measurements []Measurement
|
||||
recorder Recorder
|
||||
}
|
||||
|
||||
// WithAttachments applies provided exemplar attachments.
|
||||
func WithAttachments(attachments metricdata.Attachments) Options {
|
||||
return func(ro *recordOptions) {
|
||||
ro.attachments = attachments
|
||||
}
|
||||
}
|
||||
|
||||
// WithTags applies provided tag mutators.
|
||||
func WithTags(mutators ...tag.Mutator) Options {
|
||||
return func(ro *recordOptions) {
|
||||
ro.mutators = mutators
|
||||
}
|
||||
}
|
||||
|
||||
// WithMeasurements applies provided measurements.
|
||||
func WithMeasurements(measurements ...Measurement) Options {
|
||||
return func(ro *recordOptions) {
|
||||
ro.measurements = measurements
|
||||
}
|
||||
}
|
||||
|
||||
// WithRecorder records the measurements to the specified `Recorder`, rather
|
||||
// than to the global metrics recorder.
|
||||
func WithRecorder(meter Recorder) Options {
|
||||
return func(ro *recordOptions) {
|
||||
ro.recorder = meter
|
||||
}
|
||||
}
|
||||
|
||||
// Options apply changes to recordOptions.
|
||||
type Options func(*recordOptions)
|
||||
|
||||
func createRecordOption(ros ...Options) *recordOptions {
|
||||
o := &recordOptions{}
|
||||
for _, ro := range ros {
|
||||
ro(o)
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
type measurementRecorder = func(tags *tag.Map, measurement []Measurement, attachments map[string]interface{})
|
||||
|
||||
// Record records one or multiple measurements with the same context at once.
|
||||
// If there are any tags in the context, measurements will be tagged with them.
|
||||
func Record(ctx context.Context, ms ...Measurement) {
|
||||
// Record behaves the same as RecordWithOptions, but because we do not have to handle generic functionality
|
||||
// (RecordOptions) we can reduce some allocations to speed up this hot path
|
||||
if len(ms) == 0 {
|
||||
return
|
||||
}
|
||||
recorder := internal.MeasurementRecorder.(measurementRecorder)
|
||||
record := false
|
||||
for _, m := range ms {
|
||||
if m.desc.subscribed() {
|
||||
record = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !record {
|
||||
return
|
||||
}
|
||||
recorder(tag.FromContext(ctx), ms, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// RecordWithTags records one or multiple measurements at once.
|
||||
//
|
||||
// Measurements will be tagged with the tags in the context mutated by the mutators.
|
||||
// RecordWithTags is useful if you want to record with tag mutations but don't want
|
||||
// to propagate the mutations in the context.
|
||||
func RecordWithTags(ctx context.Context, mutators []tag.Mutator, ms ...Measurement) error {
|
||||
return RecordWithOptions(ctx, WithTags(mutators...), WithMeasurements(ms...))
|
||||
}
|
||||
|
||||
// RecordWithOptions records measurements from the given options (if any) against context
|
||||
// and tags and attachments in the options (if any).
|
||||
// If there are any tags in the context, measurements will be tagged with them.
|
||||
func RecordWithOptions(ctx context.Context, ros ...Options) error {
|
||||
o := createRecordOption(ros...)
|
||||
if len(o.measurements) == 0 {
|
||||
return nil
|
||||
}
|
||||
recorder := internal.DefaultRecorder
|
||||
if o.recorder != nil {
|
||||
recorder = o.recorder.Record
|
||||
}
|
||||
if recorder == nil {
|
||||
return nil
|
||||
}
|
||||
record := false
|
||||
for _, m := range o.measurements {
|
||||
if m.desc.subscribed() {
|
||||
record = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !record {
|
||||
return nil
|
||||
}
|
||||
if len(o.mutators) > 0 {
|
||||
var err error
|
||||
if ctx, err = tag.New(ctx, o.mutators...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
recorder(tag.FromContext(ctx), o.measurements, o.attachments)
|
||||
return nil
|
||||
}
|
||||
26
vendor/go.opencensus.io/stats/units.go
generated
vendored
Normal file
26
vendor/go.opencensus.io/stats/units.go
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package stats
|
||||
|
||||
// Units are encoded according to the case-sensitive abbreviations from the
|
||||
// Unified Code for Units of Measure: http://unitsofmeasure.org/ucum.html
|
||||
const (
|
||||
UnitNone = "1" // Deprecated: Use UnitDimensionless.
|
||||
UnitDimensionless = "1"
|
||||
UnitBytes = "By"
|
||||
UnitMilliseconds = "ms"
|
||||
UnitSeconds = "s"
|
||||
)
|
||||
123
vendor/go.opencensus.io/stats/view/aggregation.go
generated
vendored
Normal file
123
vendor/go.opencensus.io/stats/view/aggregation.go
generated
vendored
Normal file
@@ -0,0 +1,123 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package view
|
||||
|
||||
import "time"
|
||||
|
||||
// AggType represents the type of aggregation function used on a View.
|
||||
type AggType int
|
||||
|
||||
// All available aggregation types.
|
||||
const (
|
||||
AggTypeNone AggType = iota // no aggregation; reserved for future use.
|
||||
AggTypeCount // the count aggregation, see Count.
|
||||
AggTypeSum // the sum aggregation, see Sum.
|
||||
AggTypeDistribution // the distribution aggregation, see Distribution.
|
||||
AggTypeLastValue // the last value aggregation, see LastValue.
|
||||
)
|
||||
|
||||
func (t AggType) String() string {
|
||||
return aggTypeName[t]
|
||||
}
|
||||
|
||||
var aggTypeName = map[AggType]string{
|
||||
AggTypeNone: "None",
|
||||
AggTypeCount: "Count",
|
||||
AggTypeSum: "Sum",
|
||||
AggTypeDistribution: "Distribution",
|
||||
AggTypeLastValue: "LastValue",
|
||||
}
|
||||
|
||||
// Aggregation represents a data aggregation method. Use one of the functions:
|
||||
// Count, Sum, or Distribution to construct an Aggregation.
|
||||
type Aggregation struct {
|
||||
Type AggType // Type is the AggType of this Aggregation.
|
||||
Buckets []float64 // Buckets are the bucket endpoints if this Aggregation represents a distribution, see Distribution.
|
||||
|
||||
newData func(time.Time) AggregationData
|
||||
}
|
||||
|
||||
var (
|
||||
aggCount = &Aggregation{
|
||||
Type: AggTypeCount,
|
||||
newData: func(t time.Time) AggregationData {
|
||||
return &CountData{Start: t}
|
||||
},
|
||||
}
|
||||
aggSum = &Aggregation{
|
||||
Type: AggTypeSum,
|
||||
newData: func(t time.Time) AggregationData {
|
||||
return &SumData{Start: t}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// Count indicates that data collected and aggregated
|
||||
// with this method will be turned into a count value.
|
||||
// For example, total number of accepted requests can be
|
||||
// aggregated by using Count.
|
||||
func Count() *Aggregation {
|
||||
return aggCount
|
||||
}
|
||||
|
||||
// Sum indicates that data collected and aggregated
|
||||
// with this method will be summed up.
|
||||
// For example, accumulated request bytes can be aggregated by using
|
||||
// Sum.
|
||||
func Sum() *Aggregation {
|
||||
return aggSum
|
||||
}
|
||||
|
||||
// Distribution indicates that the desired aggregation is
|
||||
// a histogram distribution.
|
||||
//
|
||||
// A distribution aggregation may contain a histogram of the values in the
|
||||
// population. The bucket boundaries for that histogram are described
|
||||
// by the bounds. This defines len(bounds)+1 buckets.
|
||||
//
|
||||
// If len(bounds) >= 2 then the boundaries for bucket index i are:
|
||||
//
|
||||
// [-infinity, bounds[i]) for i = 0
|
||||
// [bounds[i-1], bounds[i]) for 0 < i < length
|
||||
// [bounds[i-1], +infinity) for i = length
|
||||
//
|
||||
// If len(bounds) is 0 then there is no histogram associated with the
|
||||
// distribution. There will be a single bucket with boundaries
|
||||
// (-infinity, +infinity).
|
||||
//
|
||||
// If len(bounds) is 1 then there is no finite buckets, and that single
|
||||
// element is the common boundary of the overflow and underflow buckets.
|
||||
func Distribution(bounds ...float64) *Aggregation {
|
||||
agg := &Aggregation{
|
||||
Type: AggTypeDistribution,
|
||||
Buckets: bounds,
|
||||
}
|
||||
agg.newData = func(t time.Time) AggregationData {
|
||||
return newDistributionData(agg, t)
|
||||
}
|
||||
return agg
|
||||
}
|
||||
|
||||
// LastValue only reports the last value recorded using this
|
||||
// aggregation. All other measurements will be dropped.
|
||||
func LastValue() *Aggregation {
|
||||
return &Aggregation{
|
||||
Type: AggTypeLastValue,
|
||||
newData: func(_ time.Time) AggregationData {
|
||||
return &LastValueData{}
|
||||
},
|
||||
}
|
||||
}
|
||||
336
vendor/go.opencensus.io/stats/view/aggregation_data.go
generated
vendored
Normal file
336
vendor/go.opencensus.io/stats/view/aggregation_data.go
generated
vendored
Normal file
@@ -0,0 +1,336 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package view
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"go.opencensus.io/metric/metricdata"
|
||||
)
|
||||
|
||||
// AggregationData represents an aggregated value from a collection.
|
||||
// They are reported on the view data during exporting.
|
||||
// Mosts users won't directly access aggregration data.
|
||||
type AggregationData interface {
|
||||
isAggregationData() bool
|
||||
addSample(v float64, attachments map[string]interface{}, t time.Time)
|
||||
clone() AggregationData
|
||||
equal(other AggregationData) bool
|
||||
toPoint(t metricdata.Type, time time.Time) metricdata.Point
|
||||
StartTime() time.Time
|
||||
}
|
||||
|
||||
const epsilon = 1e-9
|
||||
|
||||
// CountData is the aggregated data for the Count aggregation.
|
||||
// A count aggregation processes data and counts the recordings.
|
||||
//
|
||||
// Most users won't directly access count data.
|
||||
type CountData struct {
|
||||
Start time.Time
|
||||
Value int64
|
||||
}
|
||||
|
||||
func (a *CountData) isAggregationData() bool { return true }
|
||||
|
||||
func (a *CountData) addSample(_ float64, _ map[string]interface{}, _ time.Time) {
|
||||
a.Value = a.Value + 1
|
||||
}
|
||||
|
||||
func (a *CountData) clone() AggregationData {
|
||||
return &CountData{Value: a.Value, Start: a.Start}
|
||||
}
|
||||
|
||||
func (a *CountData) equal(other AggregationData) bool {
|
||||
a2, ok := other.(*CountData)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return a.Start.Equal(a2.Start) && a.Value == a2.Value
|
||||
}
|
||||
|
||||
func (a *CountData) toPoint(metricType metricdata.Type, t time.Time) metricdata.Point {
|
||||
switch metricType {
|
||||
case metricdata.TypeCumulativeInt64:
|
||||
return metricdata.NewInt64Point(t, a.Value)
|
||||
default:
|
||||
panic("unsupported metricdata.Type")
|
||||
}
|
||||
}
|
||||
|
||||
// StartTime returns the start time of the data being aggregated by CountData.
|
||||
func (a *CountData) StartTime() time.Time {
|
||||
return a.Start
|
||||
}
|
||||
|
||||
// SumData is the aggregated data for the Sum aggregation.
|
||||
// A sum aggregation processes data and sums up the recordings.
|
||||
//
|
||||
// Most users won't directly access sum data.
|
||||
type SumData struct {
|
||||
Start time.Time
|
||||
Value float64
|
||||
}
|
||||
|
||||
func (a *SumData) isAggregationData() bool { return true }
|
||||
|
||||
func (a *SumData) addSample(v float64, _ map[string]interface{}, _ time.Time) {
|
||||
a.Value += v
|
||||
}
|
||||
|
||||
func (a *SumData) clone() AggregationData {
|
||||
return &SumData{Value: a.Value, Start: a.Start}
|
||||
}
|
||||
|
||||
func (a *SumData) equal(other AggregationData) bool {
|
||||
a2, ok := other.(*SumData)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return a.Start.Equal(a2.Start) && math.Pow(a.Value-a2.Value, 2) < epsilon
|
||||
}
|
||||
|
||||
func (a *SumData) toPoint(metricType metricdata.Type, t time.Time) metricdata.Point {
|
||||
switch metricType {
|
||||
case metricdata.TypeCumulativeInt64:
|
||||
return metricdata.NewInt64Point(t, int64(a.Value))
|
||||
case metricdata.TypeCumulativeFloat64:
|
||||
return metricdata.NewFloat64Point(t, a.Value)
|
||||
default:
|
||||
panic("unsupported metricdata.Type")
|
||||
}
|
||||
}
|
||||
|
||||
// StartTime returns the start time of the data being aggregated by SumData.
|
||||
func (a *SumData) StartTime() time.Time {
|
||||
return a.Start
|
||||
}
|
||||
|
||||
// DistributionData is the aggregated data for the
|
||||
// Distribution aggregation.
|
||||
//
|
||||
// Most users won't directly access distribution data.
|
||||
//
|
||||
// For a distribution with N bounds, the associated DistributionData will have
|
||||
// N+1 buckets.
|
||||
type DistributionData struct {
|
||||
Count int64 // number of data points aggregated
|
||||
Min float64 // minimum value in the distribution
|
||||
Max float64 // max value in the distribution
|
||||
Mean float64 // mean of the distribution
|
||||
SumOfSquaredDev float64 // sum of the squared deviation from the mean
|
||||
CountPerBucket []int64 // number of occurrences per bucket
|
||||
// ExemplarsPerBucket is slice the same length as CountPerBucket containing
|
||||
// an exemplar for the associated bucket, or nil.
|
||||
ExemplarsPerBucket []*metricdata.Exemplar
|
||||
bounds []float64 // histogram distribution of the values
|
||||
Start time.Time
|
||||
}
|
||||
|
||||
func newDistributionData(agg *Aggregation, t time.Time) *DistributionData {
|
||||
bucketCount := len(agg.Buckets) + 1
|
||||
return &DistributionData{
|
||||
CountPerBucket: make([]int64, bucketCount),
|
||||
ExemplarsPerBucket: make([]*metricdata.Exemplar, bucketCount),
|
||||
bounds: agg.Buckets,
|
||||
Min: math.MaxFloat64,
|
||||
Max: math.SmallestNonzeroFloat64,
|
||||
Start: t,
|
||||
}
|
||||
}
|
||||
|
||||
// Sum returns the sum of all samples collected.
|
||||
func (a *DistributionData) Sum() float64 { return a.Mean * float64(a.Count) }
|
||||
|
||||
func (a *DistributionData) variance() float64 {
|
||||
if a.Count <= 1 {
|
||||
return 0
|
||||
}
|
||||
return a.SumOfSquaredDev / float64(a.Count-1)
|
||||
}
|
||||
|
||||
func (a *DistributionData) isAggregationData() bool { return true }
|
||||
|
||||
// TODO(songy23): support exemplar attachments.
|
||||
func (a *DistributionData) addSample(v float64, attachments map[string]interface{}, t time.Time) {
|
||||
if v < a.Min {
|
||||
a.Min = v
|
||||
}
|
||||
if v > a.Max {
|
||||
a.Max = v
|
||||
}
|
||||
a.Count++
|
||||
a.addToBucket(v, attachments, t)
|
||||
|
||||
if a.Count == 1 {
|
||||
a.Mean = v
|
||||
return
|
||||
}
|
||||
|
||||
oldMean := a.Mean
|
||||
a.Mean = a.Mean + (v-a.Mean)/float64(a.Count)
|
||||
a.SumOfSquaredDev = a.SumOfSquaredDev + (v-oldMean)*(v-a.Mean)
|
||||
}
|
||||
|
||||
func (a *DistributionData) addToBucket(v float64, attachments map[string]interface{}, t time.Time) {
|
||||
var count *int64
|
||||
var i int
|
||||
var b float64
|
||||
for i, b = range a.bounds {
|
||||
if v < b {
|
||||
count = &a.CountPerBucket[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if count == nil { // Last bucket.
|
||||
i = len(a.bounds)
|
||||
count = &a.CountPerBucket[i]
|
||||
}
|
||||
*count++
|
||||
if exemplar := getExemplar(v, attachments, t); exemplar != nil {
|
||||
a.ExemplarsPerBucket[i] = exemplar
|
||||
}
|
||||
}
|
||||
|
||||
func getExemplar(v float64, attachments map[string]interface{}, t time.Time) *metricdata.Exemplar {
|
||||
if len(attachments) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &metricdata.Exemplar{
|
||||
Value: v,
|
||||
Timestamp: t,
|
||||
Attachments: attachments,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *DistributionData) clone() AggregationData {
|
||||
c := *a
|
||||
c.CountPerBucket = append([]int64(nil), a.CountPerBucket...)
|
||||
c.ExemplarsPerBucket = append([]*metricdata.Exemplar(nil), a.ExemplarsPerBucket...)
|
||||
return &c
|
||||
}
|
||||
|
||||
func (a *DistributionData) equal(other AggregationData) bool {
|
||||
a2, ok := other.(*DistributionData)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if a2 == nil {
|
||||
return false
|
||||
}
|
||||
if len(a.CountPerBucket) != len(a2.CountPerBucket) {
|
||||
return false
|
||||
}
|
||||
for i := range a.CountPerBucket {
|
||||
if a.CountPerBucket[i] != a2.CountPerBucket[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return a.Start.Equal(a2.Start) &&
|
||||
a.Count == a2.Count &&
|
||||
a.Min == a2.Min &&
|
||||
a.Max == a2.Max &&
|
||||
math.Pow(a.Mean-a2.Mean, 2) < epsilon && math.Pow(a.variance()-a2.variance(), 2) < epsilon
|
||||
}
|
||||
|
||||
func (a *DistributionData) toPoint(metricType metricdata.Type, t time.Time) metricdata.Point {
|
||||
switch metricType {
|
||||
case metricdata.TypeCumulativeDistribution:
|
||||
buckets := []metricdata.Bucket{}
|
||||
for i := 0; i < len(a.CountPerBucket); i++ {
|
||||
buckets = append(buckets, metricdata.Bucket{
|
||||
Count: a.CountPerBucket[i],
|
||||
Exemplar: a.ExemplarsPerBucket[i],
|
||||
})
|
||||
}
|
||||
bucketOptions := &metricdata.BucketOptions{Bounds: a.bounds}
|
||||
|
||||
val := &metricdata.Distribution{
|
||||
Count: a.Count,
|
||||
Sum: a.Sum(),
|
||||
SumOfSquaredDeviation: a.SumOfSquaredDev,
|
||||
BucketOptions: bucketOptions,
|
||||
Buckets: buckets,
|
||||
}
|
||||
return metricdata.NewDistributionPoint(t, val)
|
||||
|
||||
default:
|
||||
// TODO: [rghetia] when we have a use case for TypeGaugeDistribution.
|
||||
panic("unsupported metricdata.Type")
|
||||
}
|
||||
}
|
||||
|
||||
// StartTime returns the start time of the data being aggregated by DistributionData.
|
||||
func (a *DistributionData) StartTime() time.Time {
|
||||
return a.Start
|
||||
}
|
||||
|
||||
// LastValueData returns the last value recorded for LastValue aggregation.
|
||||
type LastValueData struct {
|
||||
Value float64
|
||||
}
|
||||
|
||||
func (l *LastValueData) isAggregationData() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (l *LastValueData) addSample(v float64, _ map[string]interface{}, _ time.Time) {
|
||||
l.Value = v
|
||||
}
|
||||
|
||||
func (l *LastValueData) clone() AggregationData {
|
||||
return &LastValueData{l.Value}
|
||||
}
|
||||
|
||||
func (l *LastValueData) equal(other AggregationData) bool {
|
||||
a2, ok := other.(*LastValueData)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return l.Value == a2.Value
|
||||
}
|
||||
|
||||
func (l *LastValueData) toPoint(metricType metricdata.Type, t time.Time) metricdata.Point {
|
||||
switch metricType {
|
||||
case metricdata.TypeGaugeInt64:
|
||||
return metricdata.NewInt64Point(t, int64(l.Value))
|
||||
case metricdata.TypeGaugeFloat64:
|
||||
return metricdata.NewFloat64Point(t, l.Value)
|
||||
default:
|
||||
panic("unsupported metricdata.Type")
|
||||
}
|
||||
}
|
||||
|
||||
// StartTime returns an empty time value as start time is not recorded when using last value
|
||||
// aggregation.
|
||||
func (l *LastValueData) StartTime() time.Time {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
// ClearStart clears the Start field from data if present. Useful for testing in cases where the
|
||||
// start time will be nondeterministic.
|
||||
func ClearStart(data AggregationData) {
|
||||
switch data := data.(type) {
|
||||
case *CountData:
|
||||
data.Start = time.Time{}
|
||||
case *SumData:
|
||||
data.Start = time.Time{}
|
||||
case *DistributionData:
|
||||
data.Start = time.Time{}
|
||||
}
|
||||
}
|
||||
93
vendor/go.opencensus.io/stats/view/collector.go
generated
vendored
Normal file
93
vendor/go.opencensus.io/stats/view/collector.go
generated
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package view
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"go.opencensus.io/internal/tagencoding"
|
||||
"go.opencensus.io/tag"
|
||||
)
|
||||
|
||||
type collector struct {
|
||||
// signatures holds the aggregations values for each unique tag signature
|
||||
// (values for all keys) to its aggregator.
|
||||
signatures map[string]AggregationData
|
||||
// Aggregation is the description of the aggregation to perform for this
|
||||
// view.
|
||||
a *Aggregation
|
||||
}
|
||||
|
||||
func (c *collector) addSample(s string, v float64, attachments map[string]interface{}, t time.Time) {
|
||||
aggregator, ok := c.signatures[s]
|
||||
if !ok {
|
||||
aggregator = c.a.newData(t)
|
||||
c.signatures[s] = aggregator
|
||||
}
|
||||
aggregator.addSample(v, attachments, t)
|
||||
}
|
||||
|
||||
// collectRows returns a snapshot of the collected Row values.
|
||||
func (c *collector) collectedRows(keys []tag.Key) []*Row {
|
||||
rows := make([]*Row, 0, len(c.signatures))
|
||||
for sig, aggregator := range c.signatures {
|
||||
tags := decodeTags([]byte(sig), keys)
|
||||
row := &Row{Tags: tags, Data: aggregator.clone()}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
func (c *collector) clearRows() {
|
||||
c.signatures = make(map[string]AggregationData)
|
||||
}
|
||||
|
||||
// encodeWithKeys encodes the map by using values
|
||||
// only associated with the keys provided.
|
||||
func encodeWithKeys(m *tag.Map, keys []tag.Key) []byte {
|
||||
// Compute the buffer length we will need ahead of time to avoid resizing later
|
||||
reqLen := 0
|
||||
for _, k := range keys {
|
||||
s, _ := m.Value(k)
|
||||
// We will store each key + its length
|
||||
reqLen += len(s) + 1
|
||||
}
|
||||
vb := &tagencoding.Values{
|
||||
Buffer: make([]byte, reqLen),
|
||||
}
|
||||
for _, k := range keys {
|
||||
v, _ := m.Value(k)
|
||||
vb.WriteValue([]byte(v))
|
||||
}
|
||||
return vb.Bytes()
|
||||
}
|
||||
|
||||
// decodeTags decodes tags from the buffer and
|
||||
// orders them by the keys.
|
||||
func decodeTags(buf []byte, keys []tag.Key) []tag.Tag {
|
||||
vb := &tagencoding.Values{Buffer: buf}
|
||||
var tags []tag.Tag
|
||||
for _, k := range keys {
|
||||
v := vb.ReadValue()
|
||||
if v != nil {
|
||||
tags = append(tags, tag.Tag{Key: k, Value: string(v)})
|
||||
}
|
||||
}
|
||||
vb.ReadIndex = 0
|
||||
sort.Slice(tags, func(i, j int) bool { return tags[i].Key.Name() < tags[j].Key.Name() })
|
||||
return tags
|
||||
}
|
||||
47
vendor/go.opencensus.io/stats/view/doc.go
generated
vendored
Normal file
47
vendor/go.opencensus.io/stats/view/doc.go
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
// Package view contains support for collecting and exposing aggregates over stats.
|
||||
//
|
||||
// In order to collect measurements, views need to be defined and registered.
|
||||
// A view allows recorded measurements to be filtered and aggregated.
|
||||
//
|
||||
// All recorded measurements can be grouped by a list of tags.
|
||||
//
|
||||
// OpenCensus provides several aggregation methods: Count, Distribution and Sum.
|
||||
//
|
||||
// Count only counts the number of measurement points recorded.
|
||||
// Distribution provides statistical summary of the aggregated data by counting
|
||||
// how many recorded measurements fall into each bucket.
|
||||
// Sum adds up the measurement values.
|
||||
// LastValue just keeps track of the most recently recorded measurement value.
|
||||
// All aggregations are cumulative.
|
||||
//
|
||||
// Views can be registered and unregistered at any time during program execution.
|
||||
//
|
||||
// Libraries can define views but it is recommended that in most cases registering
|
||||
// views be left up to applications.
|
||||
//
|
||||
// # Exporting
|
||||
//
|
||||
// Collected and aggregated data can be exported to a metric collection
|
||||
// backend by registering its exporter.
|
||||
//
|
||||
// Multiple exporters can be registered to upload the data to various
|
||||
// different back ends.
|
||||
package view // import "go.opencensus.io/stats/view"
|
||||
|
||||
// TODO(acetechnologist): Add a link to the language independent OpenCensus
|
||||
// spec when it is available.
|
||||
45
vendor/go.opencensus.io/stats/view/export.go
generated
vendored
Normal file
45
vendor/go.opencensus.io/stats/view/export.go
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package view
|
||||
|
||||
// Exporter exports the collected records as view data.
|
||||
//
|
||||
// The ExportView method should return quickly; if an
|
||||
// Exporter takes a significant amount of time to
|
||||
// process a Data, that work should be done on another goroutine.
|
||||
//
|
||||
// It is safe to assume that ExportView will not be called concurrently from
|
||||
// multiple goroutines.
|
||||
//
|
||||
// The Data should not be modified.
|
||||
type Exporter interface {
|
||||
ExportView(viewData *Data)
|
||||
}
|
||||
|
||||
// RegisterExporter registers an exporter.
|
||||
// Collected data will be reported via all the
|
||||
// registered exporters. Once you no longer
|
||||
// want data to be exported, invoke UnregisterExporter
|
||||
// with the previously registered exporter.
|
||||
//
|
||||
// Binaries can register exporters, libraries shouldn't register exporters.
|
||||
func RegisterExporter(e Exporter) {
|
||||
defaultWorker.RegisterExporter(e)
|
||||
}
|
||||
|
||||
// UnregisterExporter unregisters an exporter.
|
||||
func UnregisterExporter(e Exporter) {
|
||||
defaultWorker.UnregisterExporter(e)
|
||||
}
|
||||
221
vendor/go.opencensus.io/stats/view/view.go
generated
vendored
Normal file
221
vendor/go.opencensus.io/stats/view/view.go
generated
vendored
Normal file
@@ -0,0 +1,221 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package view
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go.opencensus.io/metric/metricdata"
|
||||
"go.opencensus.io/stats"
|
||||
"go.opencensus.io/tag"
|
||||
)
|
||||
|
||||
// View allows users to aggregate the recorded stats.Measurements.
|
||||
// Views need to be passed to the Register function before data will be
|
||||
// collected and sent to Exporters.
|
||||
type View struct {
|
||||
Name string // Name of View. Must be unique. If unset, will default to the name of the Measure.
|
||||
Description string // Description is a human-readable description for this view.
|
||||
|
||||
// TagKeys are the tag keys describing the grouping of this view.
|
||||
// A single Row will be produced for each combination of associated tag values.
|
||||
TagKeys []tag.Key
|
||||
|
||||
// Measure is a stats.Measure to aggregate in this view.
|
||||
Measure stats.Measure
|
||||
|
||||
// Aggregation is the aggregation function to apply to the set of Measurements.
|
||||
Aggregation *Aggregation
|
||||
}
|
||||
|
||||
// WithName returns a copy of the View with a new name. This is useful for
|
||||
// renaming views to cope with limitations placed on metric names by various
|
||||
// backends.
|
||||
func (v *View) WithName(name string) *View {
|
||||
vNew := *v
|
||||
vNew.Name = name
|
||||
return &vNew
|
||||
}
|
||||
|
||||
// same compares two views and returns true if they represent the same aggregation.
|
||||
func (v *View) same(other *View) bool {
|
||||
if v == other {
|
||||
return true
|
||||
}
|
||||
if v == nil {
|
||||
return false
|
||||
}
|
||||
return reflect.DeepEqual(v.Aggregation, other.Aggregation) &&
|
||||
v.Measure.Name() == other.Measure.Name()
|
||||
}
|
||||
|
||||
// ErrNegativeBucketBounds error returned if histogram contains negative bounds.
|
||||
//
|
||||
// Deprecated: this should not be public.
|
||||
var ErrNegativeBucketBounds = errors.New("negative bucket bounds not supported")
|
||||
|
||||
// canonicalize canonicalizes v by setting explicit
|
||||
// defaults for Name and Description and sorting the TagKeys
|
||||
func (v *View) canonicalize() error {
|
||||
if v.Measure == nil {
|
||||
return fmt.Errorf("cannot register view %q: measure not set", v.Name)
|
||||
}
|
||||
if v.Aggregation == nil {
|
||||
return fmt.Errorf("cannot register view %q: aggregation not set", v.Name)
|
||||
}
|
||||
if v.Name == "" {
|
||||
v.Name = v.Measure.Name()
|
||||
}
|
||||
if v.Description == "" {
|
||||
v.Description = v.Measure.Description()
|
||||
}
|
||||
if err := checkViewName(v.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Slice(v.TagKeys, func(i, j int) bool {
|
||||
return v.TagKeys[i].Name() < v.TagKeys[j].Name()
|
||||
})
|
||||
sort.Float64s(v.Aggregation.Buckets)
|
||||
for _, b := range v.Aggregation.Buckets {
|
||||
if b < 0 {
|
||||
return ErrNegativeBucketBounds
|
||||
}
|
||||
}
|
||||
// drop 0 bucket silently.
|
||||
v.Aggregation.Buckets = dropZeroBounds(v.Aggregation.Buckets...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func dropZeroBounds(bounds ...float64) []float64 {
|
||||
for i, bound := range bounds {
|
||||
if bound > 0 {
|
||||
return bounds[i:]
|
||||
}
|
||||
}
|
||||
return []float64{}
|
||||
}
|
||||
|
||||
// viewInternal is the internal representation of a View.
|
||||
type viewInternal struct {
|
||||
view *View // view is the canonicalized View definition associated with this view.
|
||||
subscribed uint32 // 1 if someone is subscribed and data need to be exported, use atomic to access
|
||||
collector *collector
|
||||
metricDescriptor *metricdata.Descriptor
|
||||
}
|
||||
|
||||
func newViewInternal(v *View) (*viewInternal, error) {
|
||||
return &viewInternal{
|
||||
view: v,
|
||||
collector: &collector{make(map[string]AggregationData), v.Aggregation},
|
||||
metricDescriptor: viewToMetricDescriptor(v),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *viewInternal) subscribe() {
|
||||
atomic.StoreUint32(&v.subscribed, 1)
|
||||
}
|
||||
|
||||
func (v *viewInternal) unsubscribe() {
|
||||
atomic.StoreUint32(&v.subscribed, 0)
|
||||
}
|
||||
|
||||
// isSubscribed returns true if the view is exporting
|
||||
// data by subscription.
|
||||
func (v *viewInternal) isSubscribed() bool {
|
||||
return atomic.LoadUint32(&v.subscribed) == 1
|
||||
}
|
||||
|
||||
func (v *viewInternal) clearRows() {
|
||||
v.collector.clearRows()
|
||||
}
|
||||
|
||||
func (v *viewInternal) collectedRows() []*Row {
|
||||
return v.collector.collectedRows(v.view.TagKeys)
|
||||
}
|
||||
|
||||
func (v *viewInternal) addSample(m *tag.Map, val float64, attachments map[string]interface{}, t time.Time) {
|
||||
if !v.isSubscribed() {
|
||||
return
|
||||
}
|
||||
sig := string(encodeWithKeys(m, v.view.TagKeys))
|
||||
v.collector.addSample(sig, val, attachments, t)
|
||||
}
|
||||
|
||||
// A Data is a set of rows about usage of the single measure associated
|
||||
// with the given view. Each row is specific to a unique set of tags.
|
||||
type Data struct {
|
||||
View *View
|
||||
Start, End time.Time
|
||||
Rows []*Row
|
||||
}
|
||||
|
||||
// Row is the collected value for a specific set of key value pairs a.k.a tags.
|
||||
type Row struct {
|
||||
Tags []tag.Tag
|
||||
Data AggregationData
|
||||
}
|
||||
|
||||
func (r *Row) String() string {
|
||||
var buffer bytes.Buffer
|
||||
buffer.WriteString("{ ")
|
||||
buffer.WriteString("{ ")
|
||||
for _, t := range r.Tags {
|
||||
buffer.WriteString(fmt.Sprintf("{%v %v}", t.Key.Name(), t.Value))
|
||||
}
|
||||
buffer.WriteString(" }")
|
||||
buffer.WriteString(fmt.Sprintf("%v", r.Data))
|
||||
buffer.WriteString(" }")
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// Equal returns true if both rows are equal. Tags are expected to be ordered
|
||||
// by the key name. Even if both rows have the same tags but the tags appear in
|
||||
// different orders it will return false.
|
||||
func (r *Row) Equal(other *Row) bool {
|
||||
if r == other {
|
||||
return true
|
||||
}
|
||||
return reflect.DeepEqual(r.Tags, other.Tags) && r.Data.equal(other.Data)
|
||||
}
|
||||
|
||||
const maxNameLength = 255
|
||||
|
||||
// Returns true if the given string contains only printable characters.
|
||||
func isPrintable(str string) bool {
|
||||
for _, r := range str {
|
||||
if !(r >= ' ' && r <= '~') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func checkViewName(name string) error {
|
||||
if len(name) > maxNameLength {
|
||||
return fmt.Errorf("view name cannot be larger than %v", maxNameLength)
|
||||
}
|
||||
if !isPrintable(name) {
|
||||
return fmt.Errorf("view name needs to be an ASCII string")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
147
vendor/go.opencensus.io/stats/view/view_to_metric.go
generated
vendored
Normal file
147
vendor/go.opencensus.io/stats/view/view_to_metric.go
generated
vendored
Normal file
@@ -0,0 +1,147 @@
|
||||
// Copyright 2019, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package view
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.opencensus.io/resource"
|
||||
|
||||
"go.opencensus.io/metric/metricdata"
|
||||
"go.opencensus.io/stats"
|
||||
)
|
||||
|
||||
func getUnit(unit string) metricdata.Unit {
|
||||
switch unit {
|
||||
case "1":
|
||||
return metricdata.UnitDimensionless
|
||||
case "ms":
|
||||
return metricdata.UnitMilliseconds
|
||||
case "By":
|
||||
return metricdata.UnitBytes
|
||||
}
|
||||
return metricdata.UnitDimensionless
|
||||
}
|
||||
|
||||
func getType(v *View) metricdata.Type {
|
||||
m := v.Measure
|
||||
agg := v.Aggregation
|
||||
|
||||
switch agg.Type {
|
||||
case AggTypeSum:
|
||||
switch m.(type) {
|
||||
case *stats.Int64Measure:
|
||||
return metricdata.TypeCumulativeInt64
|
||||
case *stats.Float64Measure:
|
||||
return metricdata.TypeCumulativeFloat64
|
||||
default:
|
||||
panic("unexpected measure type")
|
||||
}
|
||||
case AggTypeDistribution:
|
||||
return metricdata.TypeCumulativeDistribution
|
||||
case AggTypeLastValue:
|
||||
switch m.(type) {
|
||||
case *stats.Int64Measure:
|
||||
return metricdata.TypeGaugeInt64
|
||||
case *stats.Float64Measure:
|
||||
return metricdata.TypeGaugeFloat64
|
||||
default:
|
||||
panic("unexpected measure type")
|
||||
}
|
||||
case AggTypeCount:
|
||||
switch m.(type) {
|
||||
case *stats.Int64Measure:
|
||||
return metricdata.TypeCumulativeInt64
|
||||
case *stats.Float64Measure:
|
||||
return metricdata.TypeCumulativeInt64
|
||||
default:
|
||||
panic("unexpected measure type")
|
||||
}
|
||||
default:
|
||||
panic("unexpected aggregation type")
|
||||
}
|
||||
}
|
||||
|
||||
func getLabelKeys(v *View) []metricdata.LabelKey {
|
||||
labelKeys := []metricdata.LabelKey{}
|
||||
for _, k := range v.TagKeys {
|
||||
labelKeys = append(labelKeys, metricdata.LabelKey{Key: k.Name()})
|
||||
}
|
||||
return labelKeys
|
||||
}
|
||||
|
||||
func viewToMetricDescriptor(v *View) *metricdata.Descriptor {
|
||||
return &metricdata.Descriptor{
|
||||
Name: v.Name,
|
||||
Description: v.Description,
|
||||
Unit: convertUnit(v),
|
||||
Type: getType(v),
|
||||
LabelKeys: getLabelKeys(v),
|
||||
}
|
||||
}
|
||||
|
||||
func convertUnit(v *View) metricdata.Unit {
|
||||
switch v.Aggregation.Type {
|
||||
case AggTypeCount:
|
||||
return metricdata.UnitDimensionless
|
||||
default:
|
||||
return getUnit(v.Measure.Unit())
|
||||
}
|
||||
}
|
||||
|
||||
func toLabelValues(row *Row, expectedKeys []metricdata.LabelKey) []metricdata.LabelValue {
|
||||
labelValues := []metricdata.LabelValue{}
|
||||
tagMap := make(map[string]string)
|
||||
for _, tag := range row.Tags {
|
||||
tagMap[tag.Key.Name()] = tag.Value
|
||||
}
|
||||
|
||||
for _, key := range expectedKeys {
|
||||
if val, ok := tagMap[key.Key]; ok {
|
||||
labelValues = append(labelValues, metricdata.NewLabelValue(val))
|
||||
} else {
|
||||
labelValues = append(labelValues, metricdata.LabelValue{})
|
||||
}
|
||||
}
|
||||
return labelValues
|
||||
}
|
||||
|
||||
func rowToTimeseries(v *viewInternal, row *Row, now time.Time) *metricdata.TimeSeries {
|
||||
return &metricdata.TimeSeries{
|
||||
Points: []metricdata.Point{row.Data.toPoint(v.metricDescriptor.Type, now)},
|
||||
LabelValues: toLabelValues(row, v.metricDescriptor.LabelKeys),
|
||||
StartTime: row.Data.StartTime(),
|
||||
}
|
||||
}
|
||||
|
||||
func viewToMetric(v *viewInternal, r *resource.Resource, now time.Time) *metricdata.Metric {
|
||||
rows := v.collectedRows()
|
||||
if len(rows) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
ts := []*metricdata.TimeSeries{}
|
||||
for _, row := range rows {
|
||||
ts = append(ts, rowToTimeseries(v, row, now))
|
||||
}
|
||||
|
||||
m := &metricdata.Metric{
|
||||
Descriptor: *v.metricDescriptor,
|
||||
TimeSeries: ts,
|
||||
Resource: r,
|
||||
}
|
||||
return m
|
||||
}
|
||||
424
vendor/go.opencensus.io/stats/view/worker.go
generated
vendored
Normal file
424
vendor/go.opencensus.io/stats/view/worker.go
generated
vendored
Normal file
@@ -0,0 +1,424 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package view
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.opencensus.io/resource"
|
||||
|
||||
"go.opencensus.io/metric/metricdata"
|
||||
"go.opencensus.io/metric/metricproducer"
|
||||
"go.opencensus.io/stats"
|
||||
"go.opencensus.io/stats/internal"
|
||||
"go.opencensus.io/tag"
|
||||
)
|
||||
|
||||
func init() {
|
||||
defaultWorker = NewMeter().(*worker)
|
||||
go defaultWorker.start()
|
||||
internal.DefaultRecorder = record
|
||||
internal.MeasurementRecorder = recordMeasurement
|
||||
}
|
||||
|
||||
type measureRef struct {
|
||||
measure string
|
||||
views map[*viewInternal]struct{}
|
||||
}
|
||||
|
||||
type worker struct {
|
||||
measures map[string]*measureRef
|
||||
views map[string]*viewInternal
|
||||
viewStartTimes map[*viewInternal]time.Time
|
||||
|
||||
timer *time.Ticker
|
||||
c chan command
|
||||
quit, done chan bool
|
||||
mu sync.RWMutex
|
||||
r *resource.Resource
|
||||
|
||||
exportersMu sync.RWMutex
|
||||
exporters map[Exporter]struct{}
|
||||
}
|
||||
|
||||
// Meter defines an interface which allows a single process to maintain
|
||||
// multiple sets of metrics exports (intended for the advanced case where a
|
||||
// single process wants to report metrics about multiple objects, such as
|
||||
// multiple databases or HTTP services).
|
||||
//
|
||||
// Note that this is an advanced use case, and the static functions in this
|
||||
// module should cover the common use cases.
|
||||
type Meter interface {
|
||||
stats.Recorder
|
||||
// Find returns a registered view associated with this name.
|
||||
// If no registered view is found, nil is returned.
|
||||
Find(name string) *View
|
||||
// Register begins collecting data for the given views.
|
||||
// Once a view is registered, it reports data to the registered exporters.
|
||||
Register(views ...*View) error
|
||||
// Unregister the given views. Data will not longer be exported for these views
|
||||
// after Unregister returns.
|
||||
// It is not necessary to unregister from views you expect to collect for the
|
||||
// duration of your program execution.
|
||||
Unregister(views ...*View)
|
||||
// SetReportingPeriod sets the interval between reporting aggregated views in
|
||||
// the program. If duration is less than or equal to zero, it enables the
|
||||
// default behavior.
|
||||
//
|
||||
// Note: each exporter makes different promises about what the lowest supported
|
||||
// duration is. For example, the Stackdriver exporter recommends a value no
|
||||
// lower than 1 minute. Consult each exporter per your needs.
|
||||
SetReportingPeriod(time.Duration)
|
||||
|
||||
// RegisterExporter registers an exporter.
|
||||
// Collected data will be reported via all the
|
||||
// registered exporters. Once you no longer
|
||||
// want data to be exported, invoke UnregisterExporter
|
||||
// with the previously registered exporter.
|
||||
//
|
||||
// Binaries can register exporters, libraries shouldn't register exporters.
|
||||
RegisterExporter(Exporter)
|
||||
// UnregisterExporter unregisters an exporter.
|
||||
UnregisterExporter(Exporter)
|
||||
// SetResource may be used to set the Resource associated with this registry.
|
||||
// This is intended to be used in cases where a single process exports metrics
|
||||
// for multiple Resources, typically in a multi-tenant situation.
|
||||
SetResource(*resource.Resource)
|
||||
|
||||
// Start causes the Meter to start processing Record calls and aggregating
|
||||
// statistics as well as exporting data.
|
||||
Start()
|
||||
// Stop causes the Meter to stop processing calls and terminate data export.
|
||||
Stop()
|
||||
|
||||
// RetrieveData gets a snapshot of the data collected for the the view registered
|
||||
// with the given name. It is intended for testing only.
|
||||
RetrieveData(viewName string) ([]*Row, error)
|
||||
}
|
||||
|
||||
var _ Meter = (*worker)(nil)
|
||||
|
||||
var defaultWorker *worker
|
||||
|
||||
var defaultReportingDuration = 10 * time.Second
|
||||
|
||||
// Find returns a registered view associated with this name.
|
||||
// If no registered view is found, nil is returned.
|
||||
func Find(name string) (v *View) {
|
||||
return defaultWorker.Find(name)
|
||||
}
|
||||
|
||||
// Find returns a registered view associated with this name.
|
||||
// If no registered view is found, nil is returned.
|
||||
func (w *worker) Find(name string) (v *View) {
|
||||
req := &getViewByNameReq{
|
||||
name: name,
|
||||
c: make(chan *getViewByNameResp),
|
||||
}
|
||||
w.c <- req
|
||||
resp := <-req.c
|
||||
return resp.v
|
||||
}
|
||||
|
||||
// Register begins collecting data for the given views.
|
||||
// Once a view is registered, it reports data to the registered exporters.
|
||||
func Register(views ...*View) error {
|
||||
return defaultWorker.Register(views...)
|
||||
}
|
||||
|
||||
// Register begins collecting data for the given views.
|
||||
// Once a view is registered, it reports data to the registered exporters.
|
||||
func (w *worker) Register(views ...*View) error {
|
||||
req := ®isterViewReq{
|
||||
views: views,
|
||||
err: make(chan error),
|
||||
}
|
||||
w.c <- req
|
||||
return <-req.err
|
||||
}
|
||||
|
||||
// Unregister the given views. Data will not longer be exported for these views
|
||||
// after Unregister returns.
|
||||
// It is not necessary to unregister from views you expect to collect for the
|
||||
// duration of your program execution.
|
||||
func Unregister(views ...*View) {
|
||||
defaultWorker.Unregister(views...)
|
||||
}
|
||||
|
||||
// Unregister the given views. Data will not longer be exported for these views
|
||||
// after Unregister returns.
|
||||
// It is not necessary to unregister from views you expect to collect for the
|
||||
// duration of your program execution.
|
||||
func (w *worker) Unregister(views ...*View) {
|
||||
names := make([]string, len(views))
|
||||
for i := range views {
|
||||
names[i] = views[i].Name
|
||||
}
|
||||
req := &unregisterFromViewReq{
|
||||
views: names,
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
w.c <- req
|
||||
<-req.done
|
||||
}
|
||||
|
||||
// RetrieveData gets a snapshot of the data collected for the the view registered
|
||||
// with the given name. It is intended for testing only.
|
||||
func RetrieveData(viewName string) ([]*Row, error) {
|
||||
return defaultWorker.RetrieveData(viewName)
|
||||
}
|
||||
|
||||
// RetrieveData gets a snapshot of the data collected for the the view registered
|
||||
// with the given name. It is intended for testing only.
|
||||
func (w *worker) RetrieveData(viewName string) ([]*Row, error) {
|
||||
req := &retrieveDataReq{
|
||||
now: time.Now(),
|
||||
v: viewName,
|
||||
c: make(chan *retrieveDataResp),
|
||||
}
|
||||
w.c <- req
|
||||
resp := <-req.c
|
||||
return resp.rows, resp.err
|
||||
}
|
||||
|
||||
func record(tags *tag.Map, ms interface{}, attachments map[string]interface{}) {
|
||||
defaultWorker.Record(tags, ms, attachments)
|
||||
}
|
||||
|
||||
func recordMeasurement(tags *tag.Map, ms []stats.Measurement, attachments map[string]interface{}) {
|
||||
defaultWorker.recordMeasurement(tags, ms, attachments)
|
||||
}
|
||||
|
||||
// Record records a set of measurements ms associated with the given tags and attachments.
|
||||
func (w *worker) Record(tags *tag.Map, ms interface{}, attachments map[string]interface{}) {
|
||||
w.recordMeasurement(tags, ms.([]stats.Measurement), attachments)
|
||||
}
|
||||
|
||||
// recordMeasurement records a set of measurements ms associated with the given tags and attachments.
|
||||
// This is the same as Record but without an interface{} type to avoid allocations
|
||||
func (w *worker) recordMeasurement(tags *tag.Map, ms []stats.Measurement, attachments map[string]interface{}) {
|
||||
req := &recordReq{
|
||||
tm: tags,
|
||||
ms: ms,
|
||||
attachments: attachments,
|
||||
t: time.Now(),
|
||||
}
|
||||
w.c <- req
|
||||
}
|
||||
|
||||
// SetReportingPeriod sets the interval between reporting aggregated views in
|
||||
// the program. If duration is less than or equal to zero, it enables the
|
||||
// default behavior.
|
||||
//
|
||||
// Note: each exporter makes different promises about what the lowest supported
|
||||
// duration is. For example, the Stackdriver exporter recommends a value no
|
||||
// lower than 1 minute. Consult each exporter per your needs.
|
||||
func SetReportingPeriod(d time.Duration) {
|
||||
defaultWorker.SetReportingPeriod(d)
|
||||
}
|
||||
|
||||
// Stop stops the default worker.
|
||||
func Stop() {
|
||||
defaultWorker.Stop()
|
||||
}
|
||||
|
||||
// SetReportingPeriod sets the interval between reporting aggregated views in
|
||||
// the program. If duration is less than or equal to zero, it enables the
|
||||
// default behavior.
|
||||
//
|
||||
// Note: each exporter makes different promises about what the lowest supported
|
||||
// duration is. For example, the Stackdriver exporter recommends a value no
|
||||
// lower than 1 minute. Consult each exporter per your needs.
|
||||
func (w *worker) SetReportingPeriod(d time.Duration) {
|
||||
// TODO(acetechnologist): ensure that the duration d is more than a certain
|
||||
// value. e.g. 1s
|
||||
req := &setReportingPeriodReq{
|
||||
d: d,
|
||||
c: make(chan bool),
|
||||
}
|
||||
w.c <- req
|
||||
<-req.c // don't return until the timer is set to the new duration.
|
||||
}
|
||||
|
||||
// NewMeter constructs a Meter instance. You should only need to use this if
|
||||
// you need to separate out Measurement recordings and View aggregations within
|
||||
// a single process.
|
||||
func NewMeter() Meter {
|
||||
return &worker{
|
||||
measures: make(map[string]*measureRef),
|
||||
views: make(map[string]*viewInternal),
|
||||
viewStartTimes: make(map[*viewInternal]time.Time),
|
||||
timer: time.NewTicker(defaultReportingDuration),
|
||||
c: make(chan command, 1024),
|
||||
quit: make(chan bool),
|
||||
done: make(chan bool),
|
||||
|
||||
exporters: make(map[Exporter]struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// SetResource associates all data collected by this Meter with the specified
|
||||
// resource. This resource is reported when using metricexport.ReadAndExport;
|
||||
// it is not provided when used with ExportView/RegisterExporter, because that
|
||||
// interface does not provide a means for reporting the Resource.
|
||||
func (w *worker) SetResource(r *resource.Resource) {
|
||||
w.r = r
|
||||
}
|
||||
|
||||
func (w *worker) Start() {
|
||||
go w.start()
|
||||
}
|
||||
|
||||
func (w *worker) start() {
|
||||
prodMgr := metricproducer.GlobalManager()
|
||||
prodMgr.AddProducer(w)
|
||||
|
||||
for {
|
||||
select {
|
||||
case cmd := <-w.c:
|
||||
cmd.handleCommand(w)
|
||||
case <-w.timer.C:
|
||||
w.reportUsage()
|
||||
case <-w.quit:
|
||||
w.timer.Stop()
|
||||
close(w.c)
|
||||
close(w.done)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *worker) Stop() {
|
||||
prodMgr := metricproducer.GlobalManager()
|
||||
prodMgr.DeleteProducer(w)
|
||||
select {
|
||||
case <-w.quit:
|
||||
default:
|
||||
close(w.quit)
|
||||
}
|
||||
<-w.done
|
||||
}
|
||||
|
||||
func (w *worker) getMeasureRef(name string) *measureRef {
|
||||
if mr, ok := w.measures[name]; ok {
|
||||
return mr
|
||||
}
|
||||
mr := &measureRef{
|
||||
measure: name,
|
||||
views: make(map[*viewInternal]struct{}),
|
||||
}
|
||||
w.measures[name] = mr
|
||||
return mr
|
||||
}
|
||||
|
||||
func (w *worker) tryRegisterView(v *View) (*viewInternal, error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
vi, err := newViewInternal(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if x, ok := w.views[vi.view.Name]; ok {
|
||||
if !x.view.same(vi.view) {
|
||||
return nil, fmt.Errorf("cannot register view %q; a different view with the same name is already registered", v.Name)
|
||||
}
|
||||
|
||||
// the view is already registered so there is nothing to do and the
|
||||
// command is considered successful.
|
||||
return x, nil
|
||||
}
|
||||
w.views[vi.view.Name] = vi
|
||||
w.viewStartTimes[vi] = time.Now()
|
||||
ref := w.getMeasureRef(vi.view.Measure.Name())
|
||||
ref.views[vi] = struct{}{}
|
||||
return vi, nil
|
||||
}
|
||||
|
||||
func (w *worker) unregisterView(v *viewInternal) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
delete(w.views, v.view.Name)
|
||||
delete(w.viewStartTimes, v)
|
||||
if measure := w.measures[v.view.Measure.Name()]; measure != nil {
|
||||
delete(measure.views, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *worker) reportView(v *viewInternal) {
|
||||
if !v.isSubscribed() {
|
||||
return
|
||||
}
|
||||
rows := v.collectedRows()
|
||||
viewData := &Data{
|
||||
View: v.view,
|
||||
Start: w.viewStartTimes[v],
|
||||
End: time.Now(),
|
||||
Rows: rows,
|
||||
}
|
||||
w.exportersMu.Lock()
|
||||
defer w.exportersMu.Unlock()
|
||||
for e := range w.exporters {
|
||||
e.ExportView(viewData)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *worker) reportUsage() {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
for _, v := range w.views {
|
||||
w.reportView(v)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *worker) toMetric(v *viewInternal, now time.Time) *metricdata.Metric {
|
||||
if !v.isSubscribed() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return viewToMetric(v, w.r, now)
|
||||
}
|
||||
|
||||
// Read reads all view data and returns them as metrics.
|
||||
// It is typically invoked by metric reader to export stats in metric format.
|
||||
func (w *worker) Read() []*metricdata.Metric {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
now := time.Now()
|
||||
metrics := make([]*metricdata.Metric, 0, len(w.views))
|
||||
for _, v := range w.views {
|
||||
metric := w.toMetric(v, now)
|
||||
if metric != nil {
|
||||
metrics = append(metrics, metric)
|
||||
}
|
||||
}
|
||||
return metrics
|
||||
}
|
||||
|
||||
func (w *worker) RegisterExporter(e Exporter) {
|
||||
w.exportersMu.Lock()
|
||||
defer w.exportersMu.Unlock()
|
||||
|
||||
w.exporters[e] = struct{}{}
|
||||
}
|
||||
|
||||
func (w *worker) UnregisterExporter(e Exporter) {
|
||||
w.exportersMu.Lock()
|
||||
defer w.exportersMu.Unlock()
|
||||
|
||||
delete(w.exporters, e)
|
||||
}
|
||||
186
vendor/go.opencensus.io/stats/view/worker_commands.go
generated
vendored
Normal file
186
vendor/go.opencensus.io/stats/view/worker_commands.go
generated
vendored
Normal file
@@ -0,0 +1,186 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package view
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.opencensus.io/stats"
|
||||
"go.opencensus.io/stats/internal"
|
||||
"go.opencensus.io/tag"
|
||||
)
|
||||
|
||||
type command interface {
|
||||
handleCommand(w *worker)
|
||||
}
|
||||
|
||||
// getViewByNameReq is the command to get a view given its name.
|
||||
type getViewByNameReq struct {
|
||||
name string
|
||||
c chan *getViewByNameResp
|
||||
}
|
||||
|
||||
type getViewByNameResp struct {
|
||||
v *View
|
||||
}
|
||||
|
||||
func (cmd *getViewByNameReq) handleCommand(w *worker) {
|
||||
v := w.views[cmd.name]
|
||||
if v == nil {
|
||||
cmd.c <- &getViewByNameResp{nil}
|
||||
return
|
||||
}
|
||||
cmd.c <- &getViewByNameResp{v.view}
|
||||
}
|
||||
|
||||
// registerViewReq is the command to register a view.
|
||||
type registerViewReq struct {
|
||||
views []*View
|
||||
err chan error
|
||||
}
|
||||
|
||||
func (cmd *registerViewReq) handleCommand(w *worker) {
|
||||
for _, v := range cmd.views {
|
||||
if err := v.canonicalize(); err != nil {
|
||||
cmd.err <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
var errstr []string
|
||||
for _, view := range cmd.views {
|
||||
vi, err := w.tryRegisterView(view)
|
||||
if err != nil {
|
||||
errstr = append(errstr, fmt.Sprintf("%s: %v", view.Name, err))
|
||||
continue
|
||||
}
|
||||
internal.SubscriptionReporter(view.Measure.Name())
|
||||
vi.subscribe()
|
||||
}
|
||||
if len(errstr) > 0 {
|
||||
cmd.err <- errors.New(strings.Join(errstr, "\n"))
|
||||
} else {
|
||||
cmd.err <- nil
|
||||
}
|
||||
}
|
||||
|
||||
// unregisterFromViewReq is the command to unregister to a view. Has no
|
||||
// impact on the data collection for client that are pulling data from the
|
||||
// library.
|
||||
type unregisterFromViewReq struct {
|
||||
views []string
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func (cmd *unregisterFromViewReq) handleCommand(w *worker) {
|
||||
for _, name := range cmd.views {
|
||||
vi, ok := w.views[name]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// Report pending data for this view before removing it.
|
||||
w.reportView(vi)
|
||||
|
||||
vi.unsubscribe()
|
||||
if !vi.isSubscribed() {
|
||||
// this was the last subscription and view is not collecting anymore.
|
||||
// The collected data can be cleared.
|
||||
vi.clearRows()
|
||||
}
|
||||
w.unregisterView(vi)
|
||||
}
|
||||
cmd.done <- struct{}{}
|
||||
}
|
||||
|
||||
// retrieveDataReq is the command to retrieve data for a view.
|
||||
type retrieveDataReq struct {
|
||||
now time.Time
|
||||
v string
|
||||
c chan *retrieveDataResp
|
||||
}
|
||||
|
||||
type retrieveDataResp struct {
|
||||
rows []*Row
|
||||
err error
|
||||
}
|
||||
|
||||
func (cmd *retrieveDataReq) handleCommand(w *worker) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
vi, ok := w.views[cmd.v]
|
||||
if !ok {
|
||||
cmd.c <- &retrieveDataResp{
|
||||
nil,
|
||||
fmt.Errorf("cannot retrieve data; view %q is not registered", cmd.v),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !vi.isSubscribed() {
|
||||
cmd.c <- &retrieveDataResp{
|
||||
nil,
|
||||
fmt.Errorf("cannot retrieve data; view %q has no subscriptions or collection is not forcibly started", cmd.v),
|
||||
}
|
||||
return
|
||||
}
|
||||
cmd.c <- &retrieveDataResp{
|
||||
vi.collectedRows(),
|
||||
nil,
|
||||
}
|
||||
}
|
||||
|
||||
// recordReq is the command to record data related to multiple measures
|
||||
// at once.
|
||||
type recordReq struct {
|
||||
tm *tag.Map
|
||||
ms []stats.Measurement
|
||||
attachments map[string]interface{}
|
||||
t time.Time
|
||||
}
|
||||
|
||||
func (cmd *recordReq) handleCommand(w *worker) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
for _, m := range cmd.ms {
|
||||
if (m == stats.Measurement{}) { // not registered
|
||||
continue
|
||||
}
|
||||
ref := w.getMeasureRef(m.Measure().Name())
|
||||
for v := range ref.views {
|
||||
v.addSample(cmd.tm, m.Value(), cmd.attachments, cmd.t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// setReportingPeriodReq is the command to modify the duration between
|
||||
// reporting the collected data to the registered clients.
|
||||
type setReportingPeriodReq struct {
|
||||
d time.Duration
|
||||
c chan bool
|
||||
}
|
||||
|
||||
func (cmd *setReportingPeriodReq) handleCommand(w *worker) {
|
||||
w.timer.Stop()
|
||||
if cmd.d <= 0 {
|
||||
w.timer = time.NewTicker(defaultReportingDuration)
|
||||
} else {
|
||||
w.timer = time.NewTicker(cmd.d)
|
||||
}
|
||||
cmd.c <- true
|
||||
}
|
||||
43
vendor/go.opencensus.io/tag/context.go
generated
vendored
Normal file
43
vendor/go.opencensus.io/tag/context.go
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package tag
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// FromContext returns the tag map stored in the context.
|
||||
func FromContext(ctx context.Context) *Map {
|
||||
// The returned tag map shouldn't be mutated.
|
||||
ts := ctx.Value(mapCtxKey)
|
||||
if ts == nil {
|
||||
return nil
|
||||
}
|
||||
return ts.(*Map)
|
||||
}
|
||||
|
||||
// NewContext creates a new context with the given tag map.
|
||||
// To propagate a tag map to downstream methods and downstream RPCs, add a tag map
|
||||
// to the current context. NewContext will return a copy of the current context,
|
||||
// and put the tag map into the returned one.
|
||||
// If there is already a tag map in the current context, it will be replaced with m.
|
||||
func NewContext(ctx context.Context, m *Map) context.Context {
|
||||
return context.WithValue(ctx, mapCtxKey, m)
|
||||
}
|
||||
|
||||
type ctxKey struct{}
|
||||
|
||||
var mapCtxKey = ctxKey{}
|
||||
26
vendor/go.opencensus.io/tag/doc.go
generated
vendored
Normal file
26
vendor/go.opencensus.io/tag/doc.go
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/*
|
||||
Package tag contains OpenCensus tags.
|
||||
|
||||
Tags are key-value pairs. Tags provide additional cardinality to
|
||||
the OpenCensus instrumentation data.
|
||||
|
||||
Tags can be propagated on the wire and in the same
|
||||
process via context.Context. Encode and Decode should be
|
||||
used to represent tags into their binary propagation form.
|
||||
*/
|
||||
package tag // import "go.opencensus.io/tag"
|
||||
44
vendor/go.opencensus.io/tag/key.go
generated
vendored
Normal file
44
vendor/go.opencensus.io/tag/key.go
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package tag
|
||||
|
||||
// Key represents a tag key.
|
||||
type Key struct {
|
||||
name string
|
||||
}
|
||||
|
||||
// NewKey creates or retrieves a string key identified by name.
|
||||
// Calling NewKey more than once with the same name returns the same key.
|
||||
func NewKey(name string) (Key, error) {
|
||||
if !checkKeyName(name) {
|
||||
return Key{}, errInvalidKeyName
|
||||
}
|
||||
return Key{name: name}, nil
|
||||
}
|
||||
|
||||
// MustNewKey returns a key with the given name, and panics if name is an invalid key name.
|
||||
func MustNewKey(name string) Key {
|
||||
k, err := NewKey(name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return k
|
||||
}
|
||||
|
||||
// Name returns the name of the key.
|
||||
func (k Key) Name() string {
|
||||
return k.name
|
||||
}
|
||||
229
vendor/go.opencensus.io/tag/map.go
generated
vendored
Normal file
229
vendor/go.opencensus.io/tag/map.go
generated
vendored
Normal file
@@ -0,0 +1,229 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package tag
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Tag is a key value pair that can be propagated on wire.
|
||||
type Tag struct {
|
||||
Key Key
|
||||
Value string
|
||||
}
|
||||
|
||||
type tagContent struct {
|
||||
value string
|
||||
m metadatas
|
||||
}
|
||||
|
||||
// Map is a map of tags. Use New to create a context containing
|
||||
// a new Map.
|
||||
type Map struct {
|
||||
m map[Key]tagContent
|
||||
}
|
||||
|
||||
// Value returns the value for the key if a value for the key exists.
|
||||
func (m *Map) Value(k Key) (string, bool) {
|
||||
if m == nil {
|
||||
return "", false
|
||||
}
|
||||
v, ok := m.m[k]
|
||||
return v.value, ok
|
||||
}
|
||||
|
||||
func (m *Map) String() string {
|
||||
if m == nil {
|
||||
return "nil"
|
||||
}
|
||||
keys := make([]Key, 0, len(m.m))
|
||||
for k := range m.m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Slice(keys, func(i, j int) bool { return keys[i].Name() < keys[j].Name() })
|
||||
|
||||
var buffer bytes.Buffer
|
||||
buffer.WriteString("{ ")
|
||||
for _, k := range keys {
|
||||
buffer.WriteString(fmt.Sprintf("{%v %v}", k.name, m.m[k]))
|
||||
}
|
||||
buffer.WriteString(" }")
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
func (m *Map) insert(k Key, v string, md metadatas) {
|
||||
if _, ok := m.m[k]; ok {
|
||||
return
|
||||
}
|
||||
m.m[k] = tagContent{value: v, m: md}
|
||||
}
|
||||
|
||||
func (m *Map) update(k Key, v string, md metadatas) {
|
||||
if _, ok := m.m[k]; ok {
|
||||
m.m[k] = tagContent{value: v, m: md}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Map) upsert(k Key, v string, md metadatas) {
|
||||
m.m[k] = tagContent{value: v, m: md}
|
||||
}
|
||||
|
||||
func (m *Map) delete(k Key) {
|
||||
delete(m.m, k)
|
||||
}
|
||||
|
||||
func newMap() *Map {
|
||||
return &Map{m: make(map[Key]tagContent)}
|
||||
}
|
||||
|
||||
// Mutator modifies a tag map.
|
||||
type Mutator interface {
|
||||
Mutate(t *Map) (*Map, error)
|
||||
}
|
||||
|
||||
// Insert returns a mutator that inserts a
|
||||
// value associated with k. If k already exists in the tag map,
|
||||
// mutator doesn't update the value.
|
||||
// Metadata applies metadata to the tag. It is optional.
|
||||
// Metadatas are applied in the order in which it is provided.
|
||||
// If more than one metadata updates the same attribute then
|
||||
// the update from the last metadata prevails.
|
||||
func Insert(k Key, v string, mds ...Metadata) Mutator {
|
||||
return &mutator{
|
||||
fn: func(m *Map) (*Map, error) {
|
||||
if !checkValue(v) {
|
||||
return nil, errInvalidValue
|
||||
}
|
||||
m.insert(k, v, createMetadatas(mds...))
|
||||
return m, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Update returns a mutator that updates the
|
||||
// value of the tag associated with k with v. If k doesn't
|
||||
// exists in the tag map, the mutator doesn't insert the value.
|
||||
// Metadata applies metadata to the tag. It is optional.
|
||||
// Metadatas are applied in the order in which it is provided.
|
||||
// If more than one metadata updates the same attribute then
|
||||
// the update from the last metadata prevails.
|
||||
func Update(k Key, v string, mds ...Metadata) Mutator {
|
||||
return &mutator{
|
||||
fn: func(m *Map) (*Map, error) {
|
||||
if !checkValue(v) {
|
||||
return nil, errInvalidValue
|
||||
}
|
||||
m.update(k, v, createMetadatas(mds...))
|
||||
return m, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Upsert returns a mutator that upserts the
|
||||
// value of the tag associated with k with v. It inserts the
|
||||
// value if k doesn't exist already. It mutates the value
|
||||
// if k already exists.
|
||||
// Metadata applies metadata to the tag. It is optional.
|
||||
// Metadatas are applied in the order in which it is provided.
|
||||
// If more than one metadata updates the same attribute then
|
||||
// the update from the last metadata prevails.
|
||||
func Upsert(k Key, v string, mds ...Metadata) Mutator {
|
||||
return &mutator{
|
||||
fn: func(m *Map) (*Map, error) {
|
||||
if !checkValue(v) {
|
||||
return nil, errInvalidValue
|
||||
}
|
||||
m.upsert(k, v, createMetadatas(mds...))
|
||||
return m, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func createMetadatas(mds ...Metadata) metadatas {
|
||||
var metas metadatas
|
||||
if len(mds) > 0 {
|
||||
for _, md := range mds {
|
||||
if md != nil {
|
||||
md(&metas)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
WithTTL(TTLUnlimitedPropagation)(&metas)
|
||||
}
|
||||
return metas
|
||||
|
||||
}
|
||||
|
||||
// Delete returns a mutator that deletes
|
||||
// the value associated with k.
|
||||
func Delete(k Key) Mutator {
|
||||
return &mutator{
|
||||
fn: func(m *Map) (*Map, error) {
|
||||
m.delete(k)
|
||||
return m, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// New returns a new context that contains a tag map
|
||||
// originated from the incoming context and modified
|
||||
// with the provided mutators.
|
||||
func New(ctx context.Context, mutator ...Mutator) (context.Context, error) {
|
||||
m := newMap()
|
||||
orig := FromContext(ctx)
|
||||
if orig != nil {
|
||||
for k, v := range orig.m {
|
||||
if !checkKeyName(k.Name()) {
|
||||
return ctx, fmt.Errorf("key:%q: %v", k, errInvalidKeyName)
|
||||
}
|
||||
if !checkValue(v.value) {
|
||||
return ctx, fmt.Errorf("key:%q value:%q: %v", k.Name(), v, errInvalidValue)
|
||||
}
|
||||
m.insert(k, v.value, v.m)
|
||||
}
|
||||
}
|
||||
var err error
|
||||
for _, mod := range mutator {
|
||||
m, err = mod.Mutate(m)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
return NewContext(ctx, m), nil
|
||||
}
|
||||
|
||||
// Do is similar to pprof.Do: a convenience for installing the tags
|
||||
// from the context as Go profiler labels. This allows you to
|
||||
// correlated runtime profiling with stats.
|
||||
//
|
||||
// It converts the key/values from the given map to Go profiler labels
|
||||
// and calls pprof.Do.
|
||||
//
|
||||
// Do is going to do nothing if your Go version is below 1.9.
|
||||
func Do(ctx context.Context, f func(ctx context.Context)) {
|
||||
do(ctx, f)
|
||||
}
|
||||
|
||||
type mutator struct {
|
||||
fn func(t *Map) (*Map, error)
|
||||
}
|
||||
|
||||
func (m *mutator) Mutate(t *Map) (*Map, error) {
|
||||
return m.fn(t)
|
||||
}
|
||||
239
vendor/go.opencensus.io/tag/map_codec.go
generated
vendored
Normal file
239
vendor/go.opencensus.io/tag/map_codec.go
generated
vendored
Normal file
@@ -0,0 +1,239 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package tag
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// KeyType defines the types of keys allowed. Currently only keyTypeString is
|
||||
// supported.
|
||||
type keyType byte
|
||||
|
||||
const (
|
||||
keyTypeString keyType = iota
|
||||
keyTypeInt64
|
||||
keyTypeTrue
|
||||
keyTypeFalse
|
||||
|
||||
tagsVersionID = byte(0)
|
||||
)
|
||||
|
||||
type encoderGRPC struct {
|
||||
buf []byte
|
||||
writeIdx, readIdx int
|
||||
}
|
||||
|
||||
// writeKeyString writes the fieldID '0' followed by the key string and value
|
||||
// string.
|
||||
func (eg *encoderGRPC) writeTagString(k, v string) {
|
||||
eg.writeByte(byte(keyTypeString))
|
||||
eg.writeStringWithVarintLen(k)
|
||||
eg.writeStringWithVarintLen(v)
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) writeTagUint64(k string, i uint64) {
|
||||
eg.writeByte(byte(keyTypeInt64))
|
||||
eg.writeStringWithVarintLen(k)
|
||||
eg.writeUint64(i)
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) writeTagTrue(k string) {
|
||||
eg.writeByte(byte(keyTypeTrue))
|
||||
eg.writeStringWithVarintLen(k)
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) writeTagFalse(k string) {
|
||||
eg.writeByte(byte(keyTypeFalse))
|
||||
eg.writeStringWithVarintLen(k)
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) writeBytesWithVarintLen(bytes []byte) {
|
||||
length := len(bytes)
|
||||
|
||||
eg.growIfRequired(binary.MaxVarintLen64 + length)
|
||||
eg.writeIdx += binary.PutUvarint(eg.buf[eg.writeIdx:], uint64(length))
|
||||
copy(eg.buf[eg.writeIdx:], bytes)
|
||||
eg.writeIdx += length
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) writeStringWithVarintLen(s string) {
|
||||
length := len(s)
|
||||
|
||||
eg.growIfRequired(binary.MaxVarintLen64 + length)
|
||||
eg.writeIdx += binary.PutUvarint(eg.buf[eg.writeIdx:], uint64(length))
|
||||
copy(eg.buf[eg.writeIdx:], s)
|
||||
eg.writeIdx += length
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) writeByte(v byte) {
|
||||
eg.growIfRequired(1)
|
||||
eg.buf[eg.writeIdx] = v
|
||||
eg.writeIdx++
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) writeUint32(i uint32) {
|
||||
eg.growIfRequired(4)
|
||||
binary.LittleEndian.PutUint32(eg.buf[eg.writeIdx:], i)
|
||||
eg.writeIdx += 4
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) writeUint64(i uint64) {
|
||||
eg.growIfRequired(8)
|
||||
binary.LittleEndian.PutUint64(eg.buf[eg.writeIdx:], i)
|
||||
eg.writeIdx += 8
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) readByte() byte {
|
||||
b := eg.buf[eg.readIdx]
|
||||
eg.readIdx++
|
||||
return b
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) readUint32() uint32 {
|
||||
i := binary.LittleEndian.Uint32(eg.buf[eg.readIdx:])
|
||||
eg.readIdx += 4
|
||||
return i
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) readUint64() uint64 {
|
||||
i := binary.LittleEndian.Uint64(eg.buf[eg.readIdx:])
|
||||
eg.readIdx += 8
|
||||
return i
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) readBytesWithVarintLen() ([]byte, error) {
|
||||
if eg.readEnded() {
|
||||
return nil, fmt.Errorf("unexpected end while readBytesWithVarintLen '%x' starting at idx '%v'", eg.buf, eg.readIdx)
|
||||
}
|
||||
length, valueStart := binary.Uvarint(eg.buf[eg.readIdx:])
|
||||
if valueStart <= 0 {
|
||||
return nil, fmt.Errorf("unexpected end while readBytesWithVarintLen '%x' starting at idx '%v'", eg.buf, eg.readIdx)
|
||||
}
|
||||
|
||||
valueStart += eg.readIdx
|
||||
valueEnd := valueStart + int(length)
|
||||
if valueEnd > len(eg.buf) {
|
||||
return nil, fmt.Errorf("malformed encoding: length:%v, upper:%v, maxLength:%v", length, valueEnd, len(eg.buf))
|
||||
}
|
||||
|
||||
eg.readIdx = valueEnd
|
||||
return eg.buf[valueStart:valueEnd], nil
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) readStringWithVarintLen() (string, error) {
|
||||
bytes, err := eg.readBytesWithVarintLen()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) growIfRequired(expected int) {
|
||||
if len(eg.buf)-eg.writeIdx < expected {
|
||||
tmp := make([]byte, 2*(len(eg.buf)+1)+expected)
|
||||
copy(tmp, eg.buf)
|
||||
eg.buf = tmp
|
||||
}
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) readEnded() bool {
|
||||
return eg.readIdx >= len(eg.buf)
|
||||
}
|
||||
|
||||
func (eg *encoderGRPC) bytes() []byte {
|
||||
return eg.buf[:eg.writeIdx]
|
||||
}
|
||||
|
||||
// Encode encodes the tag map into a []byte. It is useful to propagate
|
||||
// the tag maps on wire in binary format.
|
||||
func Encode(m *Map) []byte {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
eg := &encoderGRPC{
|
||||
buf: make([]byte, len(m.m)),
|
||||
}
|
||||
eg.writeByte(tagsVersionID)
|
||||
for k, v := range m.m {
|
||||
if v.m.ttl.ttl == valueTTLUnlimitedPropagation {
|
||||
eg.writeByte(byte(keyTypeString))
|
||||
eg.writeStringWithVarintLen(k.name)
|
||||
eg.writeBytesWithVarintLen([]byte(v.value))
|
||||
}
|
||||
}
|
||||
return eg.bytes()
|
||||
}
|
||||
|
||||
// Decode decodes the given []byte into a tag map.
|
||||
func Decode(bytes []byte) (*Map, error) {
|
||||
ts := newMap()
|
||||
err := DecodeEach(bytes, ts.upsert)
|
||||
if err != nil {
|
||||
// no partial failures
|
||||
return nil, err
|
||||
}
|
||||
return ts, nil
|
||||
}
|
||||
|
||||
// DecodeEach decodes the given serialized tag map, calling handler for each
|
||||
// tag key and value decoded.
|
||||
func DecodeEach(bytes []byte, fn func(key Key, val string, md metadatas)) error {
|
||||
eg := &encoderGRPC{
|
||||
buf: bytes,
|
||||
}
|
||||
if len(eg.buf) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
version := eg.readByte()
|
||||
if version > tagsVersionID {
|
||||
return fmt.Errorf("cannot decode: unsupported version: %q; supports only up to: %q", version, tagsVersionID)
|
||||
}
|
||||
|
||||
for !eg.readEnded() {
|
||||
typ := keyType(eg.readByte())
|
||||
|
||||
if typ != keyTypeString {
|
||||
return fmt.Errorf("cannot decode: invalid key type: %q", typ)
|
||||
}
|
||||
|
||||
k, err := eg.readBytesWithVarintLen()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
v, err := eg.readBytesWithVarintLen()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
key, err := NewKey(string(k))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
val := string(v)
|
||||
if !checkValue(val) {
|
||||
return errInvalidValue
|
||||
}
|
||||
fn(key, val, createMetadatas(WithTTL(TTLUnlimitedPropagation)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
52
vendor/go.opencensus.io/tag/metadata.go
generated
vendored
Normal file
52
vendor/go.opencensus.io/tag/metadata.go
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright 2019, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package tag
|
||||
|
||||
const (
|
||||
// valueTTLNoPropagation prevents tag from propagating.
|
||||
valueTTLNoPropagation = 0
|
||||
|
||||
// valueTTLUnlimitedPropagation allows tag to propagate without any limits on number of hops.
|
||||
valueTTLUnlimitedPropagation = -1
|
||||
)
|
||||
|
||||
// TTL is metadata that specifies number of hops a tag can propagate.
|
||||
// Details about TTL metadata is specified at https://github.com/census-instrumentation/opencensus-specs/blob/master/tags/TagMap.md#tagmetadata
|
||||
type TTL struct {
|
||||
ttl int
|
||||
}
|
||||
|
||||
var (
|
||||
// TTLUnlimitedPropagation is TTL metadata that allows tag to propagate without any limits on number of hops.
|
||||
TTLUnlimitedPropagation = TTL{ttl: valueTTLUnlimitedPropagation}
|
||||
|
||||
// TTLNoPropagation is TTL metadata that prevents tag from propagating.
|
||||
TTLNoPropagation = TTL{ttl: valueTTLNoPropagation}
|
||||
)
|
||||
|
||||
type metadatas struct {
|
||||
ttl TTL
|
||||
}
|
||||
|
||||
// Metadata applies metadatas specified by the function.
|
||||
type Metadata func(*metadatas)
|
||||
|
||||
// WithTTL applies metadata with provided ttl.
|
||||
func WithTTL(ttl TTL) Metadata {
|
||||
return func(m *metadatas) {
|
||||
m.ttl = ttl
|
||||
}
|
||||
}
|
||||
32
vendor/go.opencensus.io/tag/profile_19.go
generated
vendored
Normal file
32
vendor/go.opencensus.io/tag/profile_19.go
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build go1.9
|
||||
// +build go1.9
|
||||
|
||||
package tag
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime/pprof"
|
||||
)
|
||||
|
||||
func do(ctx context.Context, f func(ctx context.Context)) {
|
||||
m := FromContext(ctx)
|
||||
keyvals := make([]string, 0, 2*len(m.m))
|
||||
for k, v := range m.m {
|
||||
keyvals = append(keyvals, k.Name(), v.value)
|
||||
}
|
||||
pprof.Do(ctx, pprof.Labels(keyvals...), f)
|
||||
}
|
||||
24
vendor/go.opencensus.io/tag/profile_not19.go
generated
vendored
Normal file
24
vendor/go.opencensus.io/tag/profile_not19.go
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright 2018, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build !go1.9
|
||||
// +build !go1.9
|
||||
|
||||
package tag
|
||||
|
||||
import "context"
|
||||
|
||||
func do(ctx context.Context, f func(ctx context.Context)) {
|
||||
f(ctx)
|
||||
}
|
||||
56
vendor/go.opencensus.io/tag/validate.go
generated
vendored
Normal file
56
vendor/go.opencensus.io/tag/validate.go
generated
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright 2017, OpenCensus Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package tag
|
||||
|
||||
import "errors"
|
||||
|
||||
const (
|
||||
maxKeyLength = 255
|
||||
|
||||
// valid are restricted to US-ASCII subset (range 0x20 (' ') to 0x7e ('~')).
|
||||
validKeyValueMin = 32
|
||||
validKeyValueMax = 126
|
||||
)
|
||||
|
||||
var (
|
||||
errInvalidKeyName = errors.New("invalid key name: only ASCII characters accepted; max length must be 255 characters")
|
||||
errInvalidValue = errors.New("invalid value: only ASCII characters accepted; max length must be 255 characters")
|
||||
)
|
||||
|
||||
func checkKeyName(name string) bool {
|
||||
if len(name) == 0 {
|
||||
return false
|
||||
}
|
||||
if len(name) > maxKeyLength {
|
||||
return false
|
||||
}
|
||||
return isASCII(name)
|
||||
}
|
||||
|
||||
func isASCII(s string) bool {
|
||||
for _, c := range s {
|
||||
if (c < validKeyValueMin) || (c > validKeyValueMax) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func checkValue(v string) bool {
|
||||
if len(v) > maxKeyLength {
|
||||
return false
|
||||
}
|
||||
return isASCII(v)
|
||||
}
|
||||
Reference in New Issue
Block a user