This repository was archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
metrics: OpenCensus-Go stats+view to Metrics converter #32
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
// 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 ocagent_test | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net" | ||
"reflect" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/protobuf/ptypes/timestamp" | ||
"google.golang.org/grpc" | ||
|
||
"contrib.go.opencensus.io/exporter/ocagent" | ||
"go.opencensus.io/stats" | ||
"go.opencensus.io/stats/view" | ||
|
||
agentmetricspb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/metrics/v1" | ||
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" | ||
) | ||
|
||
type metricsAgent struct { | ||
mu sync.RWMutex | ||
metrics []*agentmetricspb.ExportMetricsServiceRequest | ||
} | ||
|
||
func TestExportMetrics_conversionFromViewData(t *testing.T) { | ||
ln, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
t.Fatalf("Failed to get an available TCP address: %v", err) | ||
} | ||
defer ln.Close() | ||
|
||
_, agentPortStr, _ := net.SplitHostPort(ln.Addr().String()) | ||
ma := new(metricsAgent) | ||
srv := grpc.NewServer() | ||
agentmetricspb.RegisterMetricsServiceServer(srv, ma) | ||
defer srv.Stop() | ||
go func() { | ||
_ = srv.Serve(ln) | ||
}() | ||
|
||
reconnectionPeriod := 2 * time.Millisecond | ||
ocexp, err := ocagent.NewExporter(ocagent.WithInsecure(), | ||
ocagent.WithAddress(":"+agentPortStr), | ||
ocagent.WithReconnectionPeriod(reconnectionPeriod)) | ||
if err != nil { | ||
t.Fatalf("Failed to create the ocagent exporter: %v", err) | ||
} | ||
<-time.After(5 * reconnectionPeriod) | ||
ocexp.Flush() | ||
|
||
startTime := time.Date(2018, 11, 25, 15, 38, 18, 997, time.UTC) | ||
endTime := startTime.Add(100 * time.Millisecond) | ||
|
||
mLatencyMs := stats.Float64("latency", "The latency for various methods", "ms") | ||
|
||
ocexp.ExportView(&view.Data{ | ||
Start: startTime, | ||
End: endTime, | ||
View: &view.View{ | ||
Name: "ocagent.io/latency", | ||
Description: "The latency of the various methods", | ||
Aggregation: view.Count(), | ||
Measure: mLatencyMs, | ||
}, | ||
Rows: []*view.Row{ | ||
{ | ||
Data: &view.CountData{Value: 4}, | ||
}, | ||
}, | ||
}) | ||
|
||
for i := 0; i < 5; i++ { | ||
ocexp.Flush() | ||
} | ||
|
||
<-time.After(100 * time.Millisecond) | ||
|
||
var received []*agentmetricspb.ExportMetricsServiceRequest | ||
ma.forEachRequest(func(req *agentmetricspb.ExportMetricsServiceRequest) { | ||
received = append(received, req) | ||
}) | ||
|
||
// Now compare them with what we expect | ||
want := []*agentmetricspb.ExportMetricsServiceRequest{ | ||
{ | ||
Node: ocagent.NodeWithStartTime(""), // The first message identifying this application. | ||
Metrics: nil, | ||
}, | ||
{ | ||
Metrics: []*metricspb.Metric{ | ||
{ | ||
Descriptor_: &metricspb.Metric_MetricDescriptor{ | ||
MetricDescriptor: &metricspb.MetricDescriptor{ | ||
Name: "ocagent.io/latency", | ||
Description: "The latency of the various methods", | ||
Unit: "ms", // Derived from the measure | ||
Type: metricspb.MetricDescriptor_CUMULATIVE_INT64, | ||
LabelKeys: nil, | ||
}, | ||
}, | ||
Timeseries: []*metricspb.TimeSeries{ | ||
{ | ||
StartTimestamp: ×tamp.Timestamp{ | ||
Seconds: 1543160298, | ||
Nanos: 997, | ||
}, | ||
LabelValues: nil, | ||
Points: []*metricspb.Point{ | ||
{ | ||
Timestamp: ×tamp.Timestamp{ | ||
Seconds: 1543160298, | ||
Nanos: 100000997, | ||
}, | ||
Value: &metricspb.Point_Int64Value{Int64Value: 4}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
if !reflect.DeepEqual(received, want) { | ||
gj, _ := json.MarshalIndent(received, "", " ") | ||
wj, _ := json.MarshalIndent(want, "", " ") | ||
if string(gj) != string(wj) { | ||
t.Errorf("Got:\n%s\nWant:\n%s", gj, wj) | ||
} | ||
} | ||
} | ||
|
||
func (ma *metricsAgent) Export(mes agentmetricspb.MetricsService_ExportServer) error { | ||
// Expecting the first message to contain the Node information | ||
firstMetric, err := mes.Recv() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if firstMetric == nil || firstMetric.Node == nil { | ||
return errors.New("Expecting a non-nil Node in the first message") | ||
} | ||
|
||
ma.addMetric(firstMetric) | ||
|
||
for { | ||
msg, err := mes.Recv() | ||
if err != nil { | ||
return err | ||
} | ||
ma.addMetric(msg) | ||
} | ||
} | ||
|
||
func (ma *metricsAgent) addMetric(metric *agentmetricspb.ExportMetricsServiceRequest) { | ||
ma.mu.Lock() | ||
ma.metrics = append(ma.metrics, metric) | ||
ma.mu.Unlock() | ||
} | ||
|
||
func (ma *metricsAgent) forEachRequest(fn func(*agentmetricspb.ExportMetricsServiceRequest)) { | ||
ma.mu.RLock() | ||
defer ma.mu.RUnlock() | ||
|
||
for _, req := range ma.metrics { | ||
fn(req) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say I prefer the old method name :) Not only start_time is derived from env vars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha. The reason why I changed it is because all the other variables can be retrieved alright from the environment but we don't expose startTime so it is the most important one.