@@ -82,6 +82,29 @@ def test_populate_part_a_fields(self):
82
82
self .assertEqual (tags .get ("ai.device.oemName" ), "testDeviceMake" )
83
83
self .assertEqual (tags .get ("ai.application.ver" ), "testApplicationVer" )
84
84
85
+ # Default service.name fields should be used when kubernetes values are not present
86
+ def test_populate_part_a_fields_unknown_service (self ):
87
+ resource = Resource (
88
+ {
89
+ "service.name" : "unknown_servicefoobar" ,
90
+ "service.namespace" : "testServiceNamespace" ,
91
+ "service.instance.id" : "testServiceInstanceId" ,
92
+ "device.id" : "testDeviceId" ,
93
+ "device.model.name" : "testDeviceModel" ,
94
+ "device.manufacturer" : "testDeviceMake" ,
95
+ "service.version" : "testApplicationVer" ,
96
+ }
97
+ )
98
+ tags = _utils ._populate_part_a_fields (resource )
99
+ self .assertIsNotNone (tags )
100
+ self .assertEqual (tags .get ("ai.cloud.role" ), "testServiceNamespace.unknown_servicefoobar" )
101
+ self .assertEqual (tags .get ("ai.cloud.roleInstance" ), "testServiceInstanceId" )
102
+ self .assertEqual (tags .get ("ai.internal.nodeName" ), "testServiceInstanceId" )
103
+ self .assertEqual (tags .get ("ai.device.id" ), "testDeviceId" )
104
+ self .assertEqual (tags .get ("ai.device.model" ), "testDeviceModel" )
105
+ self .assertEqual (tags .get ("ai.device.oemName" ), "testDeviceMake" )
106
+ self .assertEqual (tags .get ("ai.application.ver" ), "testApplicationVer" )
107
+
85
108
def test_populate_part_a_fields_default (self ):
86
109
resource = Resource ({"service.name" : "testServiceName" })
87
110
tags = _utils ._populate_part_a_fields (resource )
@@ -90,6 +113,159 @@ def test_populate_part_a_fields_default(self):
90
113
self .assertEqual (tags .get ("ai.cloud.roleInstance" ), platform .node ())
91
114
self .assertEqual (tags .get ("ai.internal.nodeName" ), tags .get ("ai.cloud.roleInstance" ))
92
115
116
+ def test_populate_part_a_fields_aks (self ):
117
+ resource = Resource (
118
+ {
119
+ "k8s.deployment.name" : "testDeploymentName" ,
120
+ "k8s.replicaset.name" : "testReplicaSetName" ,
121
+ "k8s.statefulset.name" : "testStatefulSetName" ,
122
+ "k8s.job.name" : "testJobName" ,
123
+ "k8s.cronJob.name" : "testCronJobName" ,
124
+ "k8s.daemonset.name" : "testDaemonSetName" ,
125
+ "k8s.pod.name" : "testPodName" ,
126
+ }
127
+ )
128
+ tags = _utils ._populate_part_a_fields (resource )
129
+ self .assertIsNotNone (tags )
130
+ self .assertEqual (tags .get ("ai.cloud.role" ), "testDeploymentName" )
131
+ self .assertEqual (tags .get ("ai.cloud.roleInstance" ), "testPodName" )
132
+ self .assertEqual (tags .get ("ai.internal.nodeName" ), tags .get ("ai.cloud.roleInstance" ))
133
+
134
+ def test_populate_part_a_fields_aks_replica (self ):
135
+ resource = Resource (
136
+ {
137
+ "k8s.replicaset.name" : "testReplicaSetName" ,
138
+ "k8s.statefulset.name" : "testStatefulSetName" ,
139
+ "k8s.job.name" : "testJobName" ,
140
+ "k8s.cronjob.name" : "testCronJobName" ,
141
+ "k8s.daemonset.name" : "testDaemonSetName" ,
142
+ "k8s.pod.name" : "testPodName" ,
143
+ }
144
+ )
145
+ tags = _utils ._populate_part_a_fields (resource )
146
+ self .assertIsNotNone (tags )
147
+ self .assertEqual (tags .get ("ai.cloud.role" ), "testReplicaSetName" )
148
+ self .assertEqual (tags .get ("ai.cloud.roleInstance" ), "testPodName" )
149
+ self .assertEqual (tags .get ("ai.internal.nodeName" ), tags .get ("ai.cloud.roleInstance" ))
150
+
151
+ def test_populate_part_a_fields_aks_stateful (self ):
152
+ resource = Resource (
153
+ {
154
+ "k8s.statefulset.name" : "testStatefulSetName" ,
155
+ "k8s.job.name" : "testJobName" ,
156
+ "k8s.cronjob.name" : "testCronJobName" ,
157
+ "k8s.daemonset.name" : "testDaemonSetName" ,
158
+ "k8s.pod.name" : "testPodName" ,
159
+ }
160
+ )
161
+ tags = _utils ._populate_part_a_fields (resource )
162
+ self .assertIsNotNone (tags )
163
+ self .assertEqual (tags .get ("ai.cloud.role" ), "testStatefulSetName" )
164
+ self .assertEqual (tags .get ("ai.cloud.roleInstance" ), "testPodName" )
165
+ self .assertEqual (tags .get ("ai.internal.nodeName" ), tags .get ("ai.cloud.roleInstance" ))
166
+
167
+ def test_populate_part_a_fields_aks_job (self ):
168
+ resource = Resource (
169
+ {
170
+ "k8s.job.name" : "testJobName" ,
171
+ "k8s.cronjob.name" : "testCronJobName" ,
172
+ "k8s.daemonset.name" : "testDaemonSetName" ,
173
+ "k8s.pod.name" : "testPodName" ,
174
+ }
175
+ )
176
+ tags = _utils ._populate_part_a_fields (resource )
177
+ self .assertIsNotNone (tags )
178
+ self .assertEqual (tags .get ("ai.cloud.role" ), "testJobName" )
179
+ self .assertEqual (tags .get ("ai.cloud.roleInstance" ), "testPodName" )
180
+ self .assertEqual (tags .get ("ai.internal.nodeName" ), tags .get ("ai.cloud.roleInstance" ))
181
+
182
+ def test_populate_part_a_fields_aks_cronjob (self ):
183
+ resource = Resource (
184
+ {
185
+ "k8s.cronjob.name" : "testCronJobName" ,
186
+ "k8s.daemonset.name" : "testDaemonSetName" ,
187
+ "k8s.pod.name" : "testPodName" ,
188
+ }
189
+ )
190
+ tags = _utils ._populate_part_a_fields (resource )
191
+ self .assertIsNotNone (tags )
192
+ self .assertEqual (tags .get ("ai.cloud.role" ), "testCronJobName" )
193
+ self .assertEqual (tags .get ("ai.cloud.roleInstance" ), "testPodName" )
194
+ self .assertEqual (tags .get ("ai.internal.nodeName" ), tags .get ("ai.cloud.roleInstance" ))
195
+
196
+ def test_populate_part_a_fields_aks_daemon (self ):
197
+ resource = Resource (
198
+ {
199
+ "k8s.daemonset.name" : "testDaemonSetName" ,
200
+ "k8s.pod.name" : "testPodName" ,
201
+ }
202
+ )
203
+ tags = _utils ._populate_part_a_fields (resource )
204
+ self .assertIsNotNone (tags )
205
+ self .assertEqual (tags .get ("ai.cloud.role" ), "testDaemonSetName" )
206
+ self .assertEqual (tags .get ("ai.cloud.roleInstance" ), "testPodName" )
207
+ self .assertEqual (tags .get ("ai.internal.nodeName" ), tags .get ("ai.cloud.roleInstance" ))
208
+
209
+ # Test that undefined fields are ignored.
210
+ def test_populate_part_a_fields_aks_undefined (self ):
211
+ resource = Resource (
212
+ {
213
+ "k8s.deployment.name" : "" ,
214
+ "k8s.replicaset.name" : None ,
215
+ "k8s.statefulset.name" : "" ,
216
+ "k8s.job.name" : None ,
217
+ "k8s.cronJob.name" : "" ,
218
+ "k8s.daemonset.name" : "testDaemonSetName" ,
219
+ "k8s.pod.name" : "testPodName" ,
220
+ }
221
+ )
222
+ tags = _utils ._populate_part_a_fields (resource )
223
+ self .assertIsNotNone (tags )
224
+ self .assertEqual (tags .get ("ai.cloud.role" ), "testDaemonSetName" )
225
+ self .assertEqual (tags .get ("ai.cloud.roleInstance" ), "testPodName" )
226
+ self .assertEqual (tags .get ("ai.internal.nodeName" ), tags .get ("ai.cloud.roleInstance" ))
227
+
228
+
229
+ def test_populate_part_a_fields_aks_with_service (self ):
230
+ resource = Resource (
231
+ {
232
+ "service.name" : "testServiceName" ,
233
+ "service.instance.id" : "testServiceInstanceId" ,
234
+ "k8s.deployment.name" : "testDeploymentName" ,
235
+ "k8s.replicaset.name" : "testReplicaSetName" ,
236
+ "k8s.statefulset.name" : "testStatefulSetName" ,
237
+ "k8s.job.name" : "testJobName" ,
238
+ "k8s.cronjob.name" : "testCronJobName" ,
239
+ "k8s.daemonset.name" : "testDaemonSetName" ,
240
+ "k8s.pod.name" : "testPodName" ,
241
+ }
242
+ )
243
+ tags = _utils ._populate_part_a_fields (resource )
244
+ self .assertIsNotNone (tags )
245
+ self .assertEqual (tags .get ("ai.cloud.role" ), "testServiceName" )
246
+ self .assertEqual (tags .get ("ai.cloud.roleInstance" ), "testServiceInstanceId" )
247
+ self .assertEqual (tags .get ("ai.internal.nodeName" ), tags .get ("ai.cloud.roleInstance" ))
248
+
249
+ # Default service.name fields should be ignored when kubernetes values are present
250
+ def test_populate_part_a_fields_aks_with_unknown_service (self ):
251
+ resource = Resource (
252
+ {
253
+ "service.name" : "unknown_servicefoobar" ,
254
+ "k8s.deployment.name" : "testDeploymentName" ,
255
+ "k8s.replicaset.name" : "testReplicaSetName" ,
256
+ "k8s.statefulset.name" : "testStatefulSetName" ,
257
+ "k8s.job.name" : "testJobName" ,
258
+ "k8s.cronjob.name" : "testCronJobName" ,
259
+ "k8s.daemonset.name" : "testDaemonSetName" ,
260
+ "k8s.pod.name" : "testPodName" ,
261
+ }
262
+ )
263
+ tags = _utils ._populate_part_a_fields (resource )
264
+ self .assertIsNotNone (tags )
265
+ self .assertEqual (tags .get ("ai.cloud.role" ), "testDeploymentName" )
266
+ self .assertEqual (tags .get ("ai.cloud.roleInstance" ), "testPodName" )
267
+ self .assertEqual (tags .get ("ai.internal.nodeName" ), tags .get ("ai.cloud.roleInstance" ))
268
+
93
269
@patch ("azure.monitor.opentelemetry.exporter._utils.ns_to_iso_str" , return_value = TEST_TIME )
94
270
@patch ("azure.monitor.opentelemetry.exporter._utils.azure_monitor_context" , TEST_AZURE_MONITOR_CONTEXT )
95
271
def test_create_telemetry_item (self , mock_ns_to_iso_str ):
0 commit comments