Debian Codegraph @

schema/v1.1/parser_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package schema
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10
11 ast10 "go.opentelemetry.io/otel/schema/v1.0/ast"
12 types10 "go.opentelemetry.io/otel/schema/v1.0/types"
13 ast11 "go.opentelemetry.io/otel/schema/v1.1/ast"
14 types11 "go.opentelemetry.io/otel/schema/v1.1/types"
15 )
16
17 func TestParseSchemaFile(t *testing.T) {
18 ts, err := ParseFile("testdata/valid-example.yaml")
19 assert.NoError(t, err)
20 assert.NotNil(t, ts)
21 assert.Equal(
22 t, &ast11.Schema{
23 FileFormat: "1.1.0",
24 SchemaURL: "https://opentelemetry.io/schemas/1.1.0",
25 Versions: map[types11.TelemetryVersion]ast11.VersionDef{
26 "1.0.0": {},
27
28 "1.1.0": {
29 All: ast10.Attributes{
30 Changes: []ast10.AttributeChange{
31 {
32 RenameAttributes: &ast10.RenameAttributes{
33 AttributeMap: ast10.AttributeMap{
34 "k8s.cluster.name": "kubernetes.cluster.name",
35 "k8s.namespace.name": "kubernetes.namespace.name",
36 "k8s.node.name": "kubernetes.node.name",
37 "k8s.node.uid": "kubernetes.node.uid",
38 "k8s.pod.name": "kubernetes.pod.name",
39 "k8s.pod.uid": "kubernetes.pod.uid",
40 "k8s.container.name": "kubernetes.container.name",
41 "k8s.replicaset.name": "kubernetes.replicaset.name",
42 "k8s.replicaset.uid": "kubernetes.replicaset.uid",
43 "k8s.cronjob.name": "kubernetes.cronjob.name",
44 "k8s.cronjob.uid": "kubernetes.cronjob.uid",
45 "k8s.job.name": "kubernetes.job.name",
46 "k8s.job.uid": "kubernetes.job.uid",
47 "k8s.statefulset.name": "kubernetes.statefulset.name",
48 "k8s.statefulset.uid": "kubernetes.statefulset.uid",
49 "k8s.daemonset.name": "kubernetes.daemonset.name",
50 "k8s.daemonset.uid": "kubernetes.daemonset.uid",
51 "k8s.deployment.name": "kubernetes.deployment.name",
52 "k8s.deployment.uid": "kubernetes.deployment.uid",
53 "service.namespace": "service.namespace.name",
54 },
55 },
56 },
57 },
58 },
59
60 Resources: ast10.Attributes{
61 Changes: []ast10.AttributeChange{
62 {
63 RenameAttributes: &ast10.RenameAttributes{
64 AttributeMap: ast10.AttributeMap{
65 "telemetry.auto.version": "telemetry.auto_instr.version",
66 },
67 },
68 },
69 },
70 },
71
72 Spans: ast10.Spans{
73 Changes: []ast10.SpansChange{
74 {
75 RenameAttributes: &ast10.AttributeMapForSpans{
76 AttributeMap: ast10.AttributeMap{
77 "peer.service": "peer.service.name",
78 },
79 ApplyToSpans: []types10.SpanName{"HTTP GET"},
80 },
81 },
82 },
83 },
84
85 SpanEvents: ast10.SpanEvents{
86 Changes: []ast10.SpanEventsChange{
87 {
88 RenameEvents: &ast10.RenameSpanEvents{
89 EventNameMap: map[string]string{
90 "exception.stacktrace": "exception.stack_trace",
91 },
92 },
93 },
94 {
95 RenameAttributes: &ast10.RenameSpanEventAttributes{
96 ApplyToEvents: []types10.EventName{"exception.stack_trace"},
97 AttributeMap: ast10.AttributeMap{
98 "peer.service": "peer.service.name",
99 },
100 },
101 },
102 },
103 },
104
105 Logs: ast10.Logs{
106 Changes: []ast10.LogsChange{
107 {
108 RenameAttributes: &ast10.RenameAttributes{
109 AttributeMap: map[string]string{
110 "process.executable_name": "process.executable.name",
111 },
112 },
113 },
114 },
115 },
116
117 Metrics: ast11.Metrics{
118 Changes: []ast11.MetricsChange{
119 {
120 RenameAttributes: &ast10.AttributeMapForMetrics{
121 AttributeMap: map[string]string{
122 "http.status_code": "http.response_status_code",
123 },
124 },
125 },
126 {
127 RenameMetrics: map[types10.MetricName]types10.MetricName{
128 "container.cpu.usage.total": "cpu.usage.total",
129 "container.memory.usage.max": "memory.usage.max",
130 },
131 },
132 {
133 RenameAttributes: &ast10.AttributeMapForMetrics{
134 ApplyToMetrics: []types10.MetricName{
135 "system.cpu.utilization",
136 "system.memory.usage",
137 "system.memory.utilization",
138 "system.paging.usage",
139 },
140 AttributeMap: map[string]string{
141 "status": "state",
142 },
143 },
144 },
145 {
146 Split: &ast11.SplitMetric{
147 ApplyToMetric: "system.paging.operations",
148 ByAttribute: "direction",
149 MetricsFromAttributes: map[types10.MetricName]types11.AttributeValue{
150 "system.paging.operations.in": "in",
151 "system.paging.operations.out": "out",
152 },
153 },
154 },
155 },
156 },
157 },
158 },
159 }, ts,
160 )
161 }
162
163 func TestFailParseFileUnsupportedFileFormat(t *testing.T) {
164 ts, err := ParseFile("testdata/unsupported-file-format.yaml")
165 assert.ErrorContains(t, err, "unsupported schema file format minor version number")
166 assert.Nil(t, ts)
167 }
168
169 func TestFailParseFileUnknownField(t *testing.T) {
170 ts, err := ParseFile("testdata/unknown-field.yaml")
171 assert.ErrorContains(t, err, "field Resources not found in type ast.VersionDef")
172 assert.Nil(t, ts)
173 }