1// Copyright The OpenTelemetry Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package internaldata
16
17import (
18	"time"
19
20	occommon "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
21	agentmetricspb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/metrics/v1"
22	ocmetrics "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
23	ocresource "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
24	"google.golang.org/protobuf/types/known/timestamppb"
25	"google.golang.org/protobuf/types/known/wrapperspb"
26
27	"go.opentelemetry.io/collector/internal/occonventions"
28	"go.opentelemetry.io/collector/internal/testdata"
29	"go.opentelemetry.io/collector/model/pdata"
30	"go.opentelemetry.io/collector/translator/conventions"
31)
32
33func generateOCTestDataNoMetrics() *agentmetricspb.ExportMetricsServiceRequest {
34	return &agentmetricspb.ExportMetricsServiceRequest{
35		Node: &occommon.Node{},
36		Resource: &ocresource.Resource{
37			Labels: map[string]string{"resource-attr": "resource-attr-val-1"},
38		},
39	}
40}
41
42func generateOCTestDataNoPoints() *agentmetricspb.ExportMetricsServiceRequest {
43	return &agentmetricspb.ExportMetricsServiceRequest{
44		Node: &occommon.Node{},
45		Resource: &ocresource.Resource{
46			Labels: map[string]string{"resource-attr": "resource-attr-val-1"},
47		},
48		Metrics: []*ocmetrics.Metric{
49			{
50				MetricDescriptor: &ocmetrics.MetricDescriptor{
51					Name:        testdata.TestGaugeDoubleMetricName,
52					Description: "",
53					Unit:        "1",
54					Type:        ocmetrics.MetricDescriptor_GAUGE_DOUBLE,
55				},
56			},
57			{
58				MetricDescriptor: &ocmetrics.MetricDescriptor{
59					Name:        testdata.TestGaugeIntMetricName,
60					Description: "",
61					Unit:        "1",
62					Type:        ocmetrics.MetricDescriptor_GAUGE_INT64,
63				},
64			},
65			{
66				MetricDescriptor: &ocmetrics.MetricDescriptor{
67					Name:        testdata.TestSumDoubleMetricName,
68					Description: "",
69					Unit:        "1",
70					Type:        ocmetrics.MetricDescriptor_CUMULATIVE_DOUBLE,
71				},
72			},
73			{
74				MetricDescriptor: &ocmetrics.MetricDescriptor{
75					Name:        testdata.TestSumIntMetricName,
76					Description: "",
77					Unit:        "1",
78					Type:        ocmetrics.MetricDescriptor_CUMULATIVE_INT64,
79				},
80			},
81			{
82				MetricDescriptor: &ocmetrics.MetricDescriptor{
83					Name:        testdata.TestDoubleHistogramMetricName,
84					Description: "",
85					Unit:        "1",
86					Type:        ocmetrics.MetricDescriptor_CUMULATIVE_DISTRIBUTION,
87				},
88			},
89			{
90				MetricDescriptor: &ocmetrics.MetricDescriptor{
91					Name:        testdata.TestDoubleSummaryMetricName,
92					Description: "",
93					Unit:        "1",
94					Type:        ocmetrics.MetricDescriptor_SUMMARY,
95				},
96			},
97		},
98	}
99}
100
101func generateOCTestDataNoLabels() *agentmetricspb.ExportMetricsServiceRequest {
102	m := generateOCTestMetricCumulativeInt()
103	m.MetricDescriptor.LabelKeys = nil
104	m.Timeseries[0].LabelValues = nil
105	m.Timeseries[1].LabelValues = nil
106	return &agentmetricspb.ExportMetricsServiceRequest{
107		Node: &occommon.Node{},
108		Resource: &ocresource.Resource{
109			Labels: map[string]string{"resource-attr": "resource-attr-val-1"},
110		},
111		Metrics: []*ocmetrics.Metric{m},
112	}
113}
114
115func generateOCTestDataMetricsOneMetric() *agentmetricspb.ExportMetricsServiceRequest {
116	return &agentmetricspb.ExportMetricsServiceRequest{
117		Node: &occommon.Node{},
118		Resource: &ocresource.Resource{
119			Labels: map[string]string{"resource-attr": "resource-attr-val-1"},
120		},
121		Metrics: []*ocmetrics.Metric{generateOCTestMetricCumulativeInt()},
122	}
123}
124
125func generateOCTestDataMetricsOneMetricOneNil() *agentmetricspb.ExportMetricsServiceRequest {
126	return &agentmetricspb.ExportMetricsServiceRequest{
127		Node: &occommon.Node{},
128		Resource: &ocresource.Resource{
129			Labels: map[string]string{"resource-attr": "resource-attr-val-1"},
130		},
131		Metrics: []*ocmetrics.Metric{generateOCTestMetricCumulativeInt(), nil},
132	}
133}
134
135func generateOCTestDataMetricsOneMetricOneNilTimeseries() *agentmetricspb.ExportMetricsServiceRequest {
136	m := generateOCTestMetricCumulativeInt()
137	m.Timeseries = append(m.Timeseries, nil)
138	return &agentmetricspb.ExportMetricsServiceRequest{
139		Node: &occommon.Node{},
140		Resource: &ocresource.Resource{
141			Labels: map[string]string{"resource-attr": "resource-attr-val-1"},
142		},
143		Metrics: []*ocmetrics.Metric{m},
144	}
145}
146
147func generateOCTestDataMetricsOneMetricOneNilPoint() *agentmetricspb.ExportMetricsServiceRequest {
148	m := generateOCTestMetricCumulativeInt()
149	m.Timeseries[0].Points = append(m.Timeseries[0].Points, nil)
150	return &agentmetricspb.ExportMetricsServiceRequest{
151		Node: &occommon.Node{},
152		Resource: &ocresource.Resource{
153			Labels: map[string]string{"resource-attr": "resource-attr-val-1"},
154		},
155		Metrics: []*ocmetrics.Metric{m},
156	}
157}
158
159func generateOCTestMetricGaugeInt() *ocmetrics.Metric {
160	return &ocmetrics.Metric{
161		MetricDescriptor: &ocmetrics.MetricDescriptor{
162			Name:        testdata.TestGaugeIntMetricName,
163			Description: "",
164			Unit:        "1",
165			Type:        ocmetrics.MetricDescriptor_GAUGE_INT64,
166			LabelKeys: []*ocmetrics.LabelKey{
167				{Key: testdata.TestLabelKey1},
168				{Key: testdata.TestLabelKey2},
169			},
170		},
171		Timeseries: []*ocmetrics.TimeSeries{
172			{
173				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
174				LabelValues: []*ocmetrics.LabelValue{
175					{
176						// key1
177						Value:    testdata.TestLabelValue1,
178						HasValue: true,
179					},
180					{
181						// key2
182						HasValue: false,
183					},
184				},
185				Points: []*ocmetrics.Point{
186					{
187						Timestamp: timestamppb.New(testdata.TestMetricTime),
188						Value: &ocmetrics.Point_Int64Value{
189							Int64Value: 123,
190						},
191					},
192				},
193			},
194			{
195				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
196				LabelValues: []*ocmetrics.LabelValue{
197					{
198						// key1
199						HasValue: false,
200					},
201					{
202						// key2
203						Value:    testdata.TestLabelValue2,
204						HasValue: true,
205					},
206				},
207				Points: []*ocmetrics.Point{
208					{
209						Timestamp: timestamppb.New(testdata.TestMetricTime),
210						Value: &ocmetrics.Point_Int64Value{
211							Int64Value: 456,
212						},
213					},
214				},
215			},
216		},
217	}
218}
219
220func generateOCTestMetricGaugeDouble() *ocmetrics.Metric {
221	return &ocmetrics.Metric{
222		MetricDescriptor: &ocmetrics.MetricDescriptor{
223			Name: testdata.TestGaugeDoubleMetricName,
224			Unit: "1",
225			Type: ocmetrics.MetricDescriptor_GAUGE_DOUBLE,
226			LabelKeys: []*ocmetrics.LabelKey{
227				{Key: testdata.TestLabelKey1},
228				{Key: testdata.TestLabelKey2},
229				{Key: testdata.TestLabelKey3},
230			},
231		},
232		Timeseries: []*ocmetrics.TimeSeries{
233			{
234				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
235				LabelValues: []*ocmetrics.LabelValue{
236					{
237						// key1
238						Value:    testdata.TestLabelValue1,
239						HasValue: true,
240					},
241					{
242						// key2
243						Value:    testdata.TestLabelValue2,
244						HasValue: true,
245					},
246					{
247						// key3
248						HasValue: false,
249					},
250				},
251				Points: []*ocmetrics.Point{
252					{
253						Timestamp: timestamppb.New(testdata.TestMetricTime),
254						Value: &ocmetrics.Point_DoubleValue{
255							DoubleValue: 1.23,
256						},
257					},
258				},
259			},
260			{
261				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
262				LabelValues: []*ocmetrics.LabelValue{
263					{
264						// key1
265						Value:    testdata.TestLabelValue1,
266						HasValue: true,
267					},
268					{
269						// key2
270						HasValue: false,
271					},
272					{
273						// key3
274						Value:    testdata.TestLabelValue3,
275						HasValue: true,
276					},
277				},
278				Points: []*ocmetrics.Point{
279					{
280						Timestamp: timestamppb.New(testdata.TestMetricTime),
281						Value: &ocmetrics.Point_DoubleValue{
282							DoubleValue: 4.56,
283						},
284					},
285				},
286			},
287		},
288	}
289}
290
291func generateOCTestMetricCumulativeInt() *ocmetrics.Metric {
292	return &ocmetrics.Metric{
293		MetricDescriptor: &ocmetrics.MetricDescriptor{
294			Name:        testdata.TestSumIntMetricName,
295			Description: "",
296			Unit:        "1",
297			Type:        ocmetrics.MetricDescriptor_CUMULATIVE_INT64,
298			LabelKeys: []*ocmetrics.LabelKey{
299				{Key: testdata.TestLabelKey1},
300				{Key: testdata.TestLabelKey2},
301			},
302		},
303		Timeseries: []*ocmetrics.TimeSeries{
304			{
305				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
306				LabelValues: []*ocmetrics.LabelValue{
307					{
308						// key1
309						Value:    testdata.TestLabelValue1,
310						HasValue: true,
311					},
312					{
313						// key2
314						HasValue: false,
315					},
316				},
317				Points: []*ocmetrics.Point{
318					{
319						Timestamp: timestamppb.New(testdata.TestMetricTime),
320						Value: &ocmetrics.Point_Int64Value{
321							Int64Value: 123,
322						},
323					},
324				},
325			},
326			{
327				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
328				LabelValues: []*ocmetrics.LabelValue{
329					{
330						// key1
331						HasValue: false,
332					},
333					{
334						// key2
335						Value:    testdata.TestLabelValue2,
336						HasValue: true,
337					},
338				},
339				Points: []*ocmetrics.Point{
340					{
341						Timestamp: timestamppb.New(testdata.TestMetricTime),
342						Value: &ocmetrics.Point_Int64Value{
343							Int64Value: 456,
344						},
345					},
346				},
347			},
348		},
349	}
350}
351
352func generateOCTestMetricCumulativeDouble() *ocmetrics.Metric {
353	return &ocmetrics.Metric{
354		MetricDescriptor: &ocmetrics.MetricDescriptor{
355			Name: testdata.TestSumDoubleMetricName,
356			Unit: "1",
357			Type: ocmetrics.MetricDescriptor_CUMULATIVE_DOUBLE,
358			LabelKeys: []*ocmetrics.LabelKey{
359				{Key: testdata.TestLabelKey1},
360				{Key: testdata.TestLabelKey2},
361				{Key: testdata.TestLabelKey3},
362			},
363		},
364		Timeseries: []*ocmetrics.TimeSeries{
365			{
366				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
367				LabelValues: []*ocmetrics.LabelValue{
368					{
369						// key1
370						Value:    testdata.TestLabelValue1,
371						HasValue: true,
372					},
373					{
374						// key2
375						Value:    testdata.TestLabelValue2,
376						HasValue: true,
377					},
378					{
379						// key3
380						HasValue: false,
381					},
382				},
383				Points: []*ocmetrics.Point{
384					{
385						Timestamp: timestamppb.New(testdata.TestMetricTime),
386						Value: &ocmetrics.Point_DoubleValue{
387							DoubleValue: 1.23,
388						},
389					},
390				},
391			},
392			{
393				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
394				LabelValues: []*ocmetrics.LabelValue{
395					{
396						// key1
397						Value:    testdata.TestLabelValue1,
398						HasValue: true,
399					},
400					{
401						// key2
402						HasValue: false,
403					},
404					{
405						// key3
406						Value:    testdata.TestLabelValue3,
407						HasValue: true,
408					},
409				},
410				Points: []*ocmetrics.Point{
411					{
412						Timestamp: timestamppb.New(testdata.TestMetricTime),
413						Value: &ocmetrics.Point_DoubleValue{
414							DoubleValue: 4.56,
415						},
416					},
417				},
418			},
419		},
420	}
421}
422
423func generateOCTestMetricDoubleHistogram() *ocmetrics.Metric {
424	return &ocmetrics.Metric{
425		MetricDescriptor: &ocmetrics.MetricDescriptor{
426			Name:        testdata.TestDoubleHistogramMetricName,
427			Description: "",
428			Unit:        "1",
429			Type:        ocmetrics.MetricDescriptor_CUMULATIVE_DISTRIBUTION,
430			LabelKeys: []*ocmetrics.LabelKey{
431				{Key: testdata.TestLabelKey1},
432				{Key: testdata.TestLabelKey2},
433				{Key: testdata.TestLabelKey3},
434			},
435		},
436		Timeseries: []*ocmetrics.TimeSeries{
437			{
438				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
439				LabelValues: []*ocmetrics.LabelValue{
440					{
441						// key1
442						Value:    testdata.TestLabelValue1,
443						HasValue: true,
444					},
445					{
446						// key2
447						HasValue: false,
448					},
449					{
450						// key3
451						Value:    testdata.TestLabelValue3,
452						HasValue: true,
453					},
454				},
455				Points: []*ocmetrics.Point{
456					{
457						Timestamp: timestamppb.New(testdata.TestMetricTime),
458						Value: &ocmetrics.Point_DistributionValue{
459							DistributionValue: &ocmetrics.DistributionValue{
460								Count: 1,
461								Sum:   15,
462							},
463						},
464					},
465				},
466			},
467			{
468				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
469				LabelValues: []*ocmetrics.LabelValue{
470					{
471						// key1
472						HasValue: false,
473					},
474					{
475						// key2
476						Value:    testdata.TestLabelValue2,
477						HasValue: true,
478					},
479					{
480						// key3
481						HasValue: false,
482					},
483				},
484				Points: []*ocmetrics.Point{
485					{
486						Timestamp: timestamppb.New(testdata.TestMetricTime),
487						Value: &ocmetrics.Point_DistributionValue{
488							DistributionValue: &ocmetrics.DistributionValue{
489								Count: 1,
490								Sum:   15,
491								BucketOptions: &ocmetrics.DistributionValue_BucketOptions{
492									Type: &ocmetrics.DistributionValue_BucketOptions_Explicit_{
493										Explicit: &ocmetrics.DistributionValue_BucketOptions_Explicit{
494											Bounds: []float64{1},
495										},
496									},
497								},
498								Buckets: []*ocmetrics.DistributionValue_Bucket{
499									{
500										Count: 0,
501									},
502									{
503										Count: 1,
504										Exemplar: &ocmetrics.DistributionValue_Exemplar{
505											Timestamp:   timestamppb.New(testdata.TestMetricExemplarTime),
506											Value:       15,
507											Attachments: map[string]string{testdata.TestAttachmentKey: testdata.TestAttachmentValue},
508										},
509									},
510								},
511							},
512						},
513					},
514				},
515			},
516		},
517	}
518}
519
520func generateOCTestMetricDoubleSummary() *ocmetrics.Metric {
521	return &ocmetrics.Metric{
522		MetricDescriptor: &ocmetrics.MetricDescriptor{
523			Name:        testdata.TestDoubleSummaryMetricName,
524			Description: "",
525			Unit:        "1",
526			Type:        ocmetrics.MetricDescriptor_SUMMARY,
527			LabelKeys: []*ocmetrics.LabelKey{
528				{Key: testdata.TestLabelKey1},
529				{Key: testdata.TestLabelKey2},
530				{Key: testdata.TestLabelKey3},
531			},
532		},
533		Timeseries: []*ocmetrics.TimeSeries{
534			{
535				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
536				LabelValues: []*ocmetrics.LabelValue{
537					{
538						// key1
539						Value:    testdata.TestLabelValue1,
540						HasValue: true,
541					},
542					{
543						// key2
544						HasValue: false,
545					},
546					{
547						// key3
548						Value:    testdata.TestLabelValue3,
549						HasValue: true,
550					},
551				},
552				Points: []*ocmetrics.Point{
553					{
554						Timestamp: timestamppb.New(testdata.TestMetricTime),
555						Value: &ocmetrics.Point_SummaryValue{
556							SummaryValue: &ocmetrics.SummaryValue{
557								Count: &wrapperspb.Int64Value{
558									Value: 1,
559								},
560								Sum: &wrapperspb.DoubleValue{
561									Value: 15,
562								},
563								Snapshot: &ocmetrics.SummaryValue_Snapshot{
564									PercentileValues: nil,
565								},
566							},
567						},
568					},
569				},
570			},
571			{
572				StartTimestamp: timestamppb.New(testdata.TestMetricStartTime),
573				LabelValues: []*ocmetrics.LabelValue{
574					{
575						// key1
576						HasValue: false,
577					},
578					{
579						// key2
580						Value:    testdata.TestLabelValue2,
581						HasValue: true,
582					},
583					{
584						// key3
585						HasValue: false,
586					},
587				},
588				Points: []*ocmetrics.Point{
589					{
590						Timestamp: timestamppb.New(testdata.TestMetricTime),
591						Value: &ocmetrics.Point_SummaryValue{
592							SummaryValue: &ocmetrics.SummaryValue{
593								Count: &wrapperspb.Int64Value{
594									Value: 1,
595								},
596								Sum: &wrapperspb.DoubleValue{
597									Value: 15,
598								},
599								Snapshot: &ocmetrics.SummaryValue_Snapshot{
600									PercentileValues: []*ocmetrics.SummaryValue_Snapshot_ValueAtPercentile{
601										{
602											Percentile: 1,
603											Value:      15,
604										},
605									},
606								},
607							},
608						},
609					},
610				},
611			},
612		},
613	}
614}
615
616func generateResourceWithOcNodeAndResource() pdata.Resource {
617	resource := pdata.NewResource()
618	resource.Attributes().InitFromMap(map[string]pdata.AttributeValue{
619		occonventions.AttributeProcessStartTime:   pdata.NewAttributeValueString("2020-02-11T20:26:00Z"),
620		conventions.AttributeHostName:             pdata.NewAttributeValueString("host1"),
621		conventions.AttributeProcessID:            pdata.NewAttributeValueInt(123),
622		conventions.AttributeTelemetrySDKVersion:  pdata.NewAttributeValueString("v2.0.1"),
623		occonventions.AttributeExporterVersion:    pdata.NewAttributeValueString("v1.2.0"),
624		conventions.AttributeTelemetrySDKLanguage: pdata.NewAttributeValueString("cpp"),
625		occonventions.AttributeResourceType:       pdata.NewAttributeValueString("good-resource"),
626		"node-str-attr":                           pdata.NewAttributeValueString("node-str-attr-val"),
627		"resource-str-attr":                       pdata.NewAttributeValueString("resource-str-attr-val"),
628		"resource-int-attr":                       pdata.NewAttributeValueInt(123),
629	})
630	return resource
631}
632
633func generateOcNode() *occommon.Node {
634	ts := timestamppb.New(time.Date(2020, 2, 11, 20, 26, 0, 0, time.UTC))
635
636	return &occommon.Node{
637		Identifier: &occommon.ProcessIdentifier{
638			HostName:       "host1",
639			Pid:            123,
640			StartTimestamp: ts,
641		},
642		LibraryInfo: &occommon.LibraryInfo{
643			Language:           occommon.LibraryInfo_CPP,
644			ExporterVersion:    "v1.2.0",
645			CoreLibraryVersion: "v2.0.1",
646		},
647		Attributes: map[string]string{
648			"node-str-attr": "node-str-attr-val",
649		},
650	}
651}
652
653func generateOcResource() *ocresource.Resource {
654	return &ocresource.Resource{
655		Type: "good-resource",
656		Labels: map[string]string{
657			"resource-str-attr": "resource-str-attr-val",
658			"resource-int-attr": "123",
659		},
660	}
661}
662