This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Add option to provide resource based on metric descriptor. #231
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -31,6 +31,7 @@ import ( | |
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres" | ||
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" | ||
|
||
"contrib.go.opencensus.io/exporter/stackdriver/monitoredresource" | ||
"go.opencensus.io/metric/metricdata" | ||
"go.opencensus.io/resource" | ||
"go.opencensus.io/trace" | ||
|
@@ -559,3 +560,238 @@ func TestMetricsToMonitoringMetrics_fromProtoPoint(t *testing.T) { | |
} | ||
} | ||
} | ||
|
||
func TestGetMonitorResource(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Should the name be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed. |
||
startTimestamp := ×tamp.Timestamp{ | ||
Seconds: 1543160298, | ||
Nanos: 100000090, | ||
} | ||
startTime, _ := ptypes.Timestamp(startTimestamp) | ||
endTimestamp := ×tamp.Timestamp{ | ||
Seconds: 1543160298, | ||
Nanos: 100000997, | ||
} | ||
endTime, _ := ptypes.Timestamp(endTimestamp) | ||
|
||
tests := []struct { | ||
in *metricdata.Metric | ||
want []*monitoringpb.CreateTimeSeriesRequest | ||
wantErr string | ||
}{ | ||
{ | ||
in: &metricdata.Metric{ | ||
Descriptor: metricdata.Descriptor{ | ||
Name: "custom_resource_one", | ||
Description: "This is a test", | ||
Unit: metricdata.UnitBytes, | ||
Type: metricdata.TypeCumulativeInt64, | ||
}, | ||
Resource: nil, | ||
TimeSeries: []*metricdata.TimeSeries{ | ||
{ | ||
StartTime: startTime, | ||
Points: []metricdata.Point{ | ||
{ | ||
Time: endTime, | ||
Value: int64(5), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: []*monitoringpb.CreateTimeSeriesRequest{ | ||
{ | ||
Name: "projects/foo", | ||
TimeSeries: []*monitoringpb.TimeSeries{ | ||
{ | ||
Metric: &googlemetricpb.Metric{ | ||
Type: "custom.googleapis.com/opencensus/custom_resource_one", | ||
Labels: nil, | ||
}, | ||
Resource: &monitoredrespb.MonitoredResource{ | ||
Type: "one", | ||
Labels: map[string]string{ | ||
"k11": "v11", | ||
"k12": "v12", | ||
}, | ||
}, | ||
Points: []*monitoringpb.Point{ | ||
{ | ||
Interval: &monitoringpb.TimeInterval{ | ||
StartTime: startTimestamp, | ||
EndTime: endTimestamp, | ||
}, | ||
Value: &monitoringpb.TypedValue{ | ||
Value: &monitoringpb.TypedValue_Int64Value{ | ||
Int64Value: 5, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
in: &metricdata.Metric{ | ||
Descriptor: metricdata.Descriptor{ | ||
Name: "custom_resource_two", | ||
Description: "This is a test", | ||
Unit: metricdata.UnitBytes, | ||
Type: metricdata.TypeCumulativeInt64, | ||
}, | ||
Resource: nil, | ||
TimeSeries: []*metricdata.TimeSeries{ | ||
{ | ||
StartTime: startTime, | ||
Points: []metricdata.Point{ | ||
{ | ||
Time: endTime, | ||
Value: int64(5), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: []*monitoringpb.CreateTimeSeriesRequest{ | ||
{ | ||
Name: "projects/foo", | ||
TimeSeries: []*monitoringpb.TimeSeries{ | ||
{ | ||
Metric: &googlemetricpb.Metric{ | ||
Type: "custom.googleapis.com/opencensus/custom_resource_two", | ||
Labels: nil, | ||
}, | ||
Resource: &monitoredrespb.MonitoredResource{ | ||
Type: "two", | ||
Labels: map[string]string{ | ||
"k21": "v21", | ||
"k22": "v22", | ||
}, | ||
}, | ||
Points: []*monitoringpb.Point{ | ||
{ | ||
Interval: &monitoringpb.TimeInterval{ | ||
StartTime: startTimestamp, | ||
EndTime: endTimestamp, | ||
}, | ||
Value: &monitoringpb.TypedValue{ | ||
Value: &monitoringpb.TypedValue_Int64Value{ | ||
Int64Value: 5, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
in: &metricdata.Metric{ | ||
Descriptor: metricdata.Descriptor{ | ||
Name: "custom_resource_other", | ||
Description: "This is a test", | ||
Unit: metricdata.UnitBytes, | ||
Type: metricdata.TypeCumulativeInt64, | ||
}, | ||
Resource: nil, | ||
TimeSeries: []*metricdata.TimeSeries{ | ||
{ | ||
StartTime: startTime, | ||
Points: []metricdata.Point{ | ||
{ | ||
Time: endTime, | ||
Value: int64(5), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: []*monitoringpb.CreateTimeSeriesRequest{ | ||
{ | ||
Name: "projects/foo", | ||
TimeSeries: []*monitoringpb.TimeSeries{ | ||
{ | ||
Metric: &googlemetricpb.Metric{ | ||
Type: "custom.googleapis.com/opencensus/custom_resource_other", | ||
Labels: nil, | ||
}, | ||
Resource: &monitoredrespb.MonitoredResource{ | ||
Type: "global", | ||
}, | ||
Points: []*monitoringpb.Point{ | ||
{ | ||
Interval: &monitoringpb.TimeInterval{ | ||
StartTime: startTimestamp, | ||
EndTime: endTimestamp, | ||
}, | ||
Value: &monitoringpb.TypedValue{ | ||
Value: &monitoringpb.TypedValue_Int64Value{ | ||
Int64Value: 5, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
var se = &statsExporter{ | ||
o: Options{ | ||
ProjectID: "foo", | ||
ResourceByDescriptor: testGetMonitoringResource, | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
tsl, err := se.metricToMpbTs(context.Background(), tt.in) | ||
if tt.wantErr != "" { | ||
if err == nil || !strings.Contains(err.Error(), tt.wantErr) { | ||
t.Errorf("#%d: unmatched error. Got\n\t%v\nWant\n\t%v", i, err, tt.wantErr) | ||
} | ||
continue | ||
} | ||
if err != nil { | ||
t.Errorf("#%d: unexpected error: %v", i, err) | ||
continue | ||
} | ||
|
||
got := se.combineTimeSeriesToCreateTimeSeriesRequest(tsl) | ||
// Our saving grace is serialization equality since some | ||
// unexported fields could be present in the various values. | ||
if diff := cmpTSReqs(got, tt.want); diff != "" { | ||
t.Fatalf("Test %d failed. Unexpected CreateTimeSeriesRequests -got +want: %s", i, diff) | ||
} | ||
} | ||
} | ||
|
||
type customResource struct { | ||
rt string | ||
rm map[string]string | ||
} | ||
|
||
var _ monitoredresource.Interface = customResource{} | ||
|
||
func (cr customResource) MonitoredResource() (resType string, labels map[string]string) { | ||
return cr.rt, cr.rm | ||
} | ||
|
||
var crOne = customResource{rt: "one", rm: map[string]string{"k11": "v11", "k12": "v12"}} | ||
var crTwo = customResource{rt: "two", rm: map[string]string{"k21": "v21", "k22": "v22"}} | ||
var crEmpty = customResource{rt: ""} | ||
|
||
func testGetMonitoringResource(md *metricdata.Descriptor) monitoredresource.Interface { | ||
switch md.Name { | ||
case "custom_resource_one": | ||
return crOne | ||
case "custom_resource_two": | ||
return crTwo | ||
default: | ||
return crEmpty | ||
} | ||
} |
This file contains 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
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.
What
metric.Descriptor
will be if I register the following: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.
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.
Could we determine resource type by name? Name is more like an ID instead of the description. WDYT?
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.
actually Descriptor.Name = View.Name.
knative does determine resource type from name as per this.
It seems you also need tags, can you confirm?
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.
Ah got it.
The tags are used to set the values of monitored resource labels. For example,
project_id
.