Skip to content

Commit 3574cea

Browse files
anirudh-04bvahdat
authored andcommitted
CAMEL-20937 add azure-storage-datalake span decorator
1 parent 71aa1ca commit 3574cea

File tree

4 files changed

+271
-0
lines changed

4 files changed

+271
-0
lines changed

components/camel-observation/src/main/resources/META-INF/services/org.apache.camel.tracing.SpanDecorator

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
org.apache.camel.tracing.decorators.AhcSpanDecorator
1919
org.apache.camel.tracing.decorators.AmqpSpanDecorator
2020
org.apache.camel.tracing.decorators.AzureServiceBusSpanDecorator
21+
org.apache.camel.tracing.decorators.AzureStorageDataLakeSpanDecorator
2122
org.apache.camel.tracing.decorators.CometdSpanDecorator
2223
org.apache.camel.tracing.decorators.CometdsSpanDecorator
2324
org.apache.camel.tracing.decorators.CqlSpanDecorator

components/camel-opentelemetry/src/main/resources/META-INF/services/org.apache.camel.tracing.SpanDecorator

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
org.apache.camel.tracing.decorators.AhcSpanDecorator
1919
org.apache.camel.tracing.decorators.AmqpSpanDecorator
2020
org.apache.camel.tracing.decorators.AzureServiceBusSpanDecorator
21+
org.apache.camel.tracing.decorators.AzureStorageDataLakeSpanDecorator
2122
org.apache.camel.tracing.decorators.CometdSpanDecorator
2223
org.apache.camel.tracing.decorators.CometdsSpanDecorator
2324
org.apache.camel.tracing.decorators.CqlSpanDecorator
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.tracing.decorators;
18+
19+
import java.time.Duration;
20+
import java.time.OffsetDateTime;
21+
import java.util.Map;
22+
23+
import org.apache.camel.Endpoint;
24+
import org.apache.camel.Exchange;
25+
import org.apache.camel.tracing.SpanAdapter;
26+
import org.apache.camel.tracing.TagConstants;
27+
28+
public class AzureStorageDataLakeSpanDecorator extends AbstractSpanDecorator {
29+
30+
static final String STORAGE_DATALAKE_DIRECTORY_NAME = "directoryName";
31+
static final String STORAGE_DATALAKE_FILE_NAME = "fileName";
32+
static final String STORAGE_DATALAKE_PATH = "path";
33+
static final String STORAGE_DATALAKE_TIMEOUT = "timeout";
34+
static final String STORAGE_DATALAKE_CONTENT_TYPE = "contentType";
35+
static final String STORAGE_DATALAKE_METADATA = "metadata";
36+
static final String STORAGE_DATALAKE_LAST_MODIFIED = "lastModified";
37+
static final String STORAGE_DATALAKE_POSITION = "position";
38+
static final String STORAGE_DATALAKE_EXPRESSION = "expression";
39+
40+
/**
41+
* Constants copied from {@link org.apache.camel.component.azure.storage.datalake.DataLakeConstants}
42+
*/
43+
static final String OPERATION = "CamelAzureStorageDataLakeOperation";
44+
static final String FILESYSTEM_NAME = "CamelAzureStorageDataLakeFileSystemName";
45+
static final String DIRECTORY_NAME = "CamelAzureStorageDataLakeDirectoryName";
46+
static final String FILE_NAME = "CamelAzureStorageDataLakeFileName";
47+
static final String PATH = "CamelAzureStorageDataLakePath";
48+
static final String TIMEOUT = "CamelAzureStorageDataLakeTimeout";
49+
static final String CONTENT_TYPE = "CamelAzureStorageDataLakeContentType";
50+
static final String METADATA = "CamelAzureStorageDataLakeMetadata";
51+
static final String LAST_MODIFIED = "CamelAzureStorageDataLakeLastModified";
52+
static final String POSITION = "CamelAzureStorageDataLakePosition";
53+
static final String EXPRESSION = "CamelAzureStorageDataLakeExpression";
54+
55+
@Override
56+
public String getComponent() {
57+
return "azure-storage-datalake";
58+
}
59+
60+
@Override
61+
public String getComponentClassName() {
62+
return "org.apache.camel.component.azure.storage.datalake.DataLakeComponent";
63+
}
64+
65+
@Override
66+
public String getOperationName(Exchange exchange, Endpoint endpoint) {
67+
String operation = exchange.getIn().getHeader(OPERATION, String.class);
68+
if (operation == null) {
69+
Map<String, String> queryParameters = toQueryParameters(endpoint.getEndpointUri());
70+
return queryParameters.containsKey("operation")
71+
? queryParameters.get("operation")
72+
: super.getOperationName(exchange, endpoint);
73+
}
74+
return operation;
75+
}
76+
77+
@Override
78+
public void pre(SpanAdapter span, Exchange exchange, Endpoint endpoint) {
79+
super.pre(span, exchange, endpoint);
80+
span.setTag(TagConstants.DB_SYSTEM, getComponent());
81+
82+
String fileSystemName = exchange.getIn().getHeader(FILESYSTEM_NAME, String.class);
83+
if (fileSystemName != null) {
84+
span.setTag(TagConstants.DB_NAME, fileSystemName);
85+
}
86+
87+
String directoryName = exchange.getIn().getHeader(DIRECTORY_NAME, String.class);
88+
if (directoryName != null) {
89+
span.setTag(STORAGE_DATALAKE_DIRECTORY_NAME, directoryName);
90+
}
91+
92+
String fileName = exchange.getIn().getHeader(FILE_NAME, String.class);
93+
if (fileName != null) {
94+
span.setTag(STORAGE_DATALAKE_FILE_NAME, fileName);
95+
}
96+
97+
String path = exchange.getIn().getHeader(PATH, String.class);
98+
if (path != null) {
99+
span.setTag(STORAGE_DATALAKE_PATH, path);
100+
}
101+
102+
Duration timeout = exchange.getIn().getHeader(TIMEOUT, Duration.class);
103+
if (timeout != null) {
104+
span.setTag(STORAGE_DATALAKE_TIMEOUT, timeout.toString());
105+
}
106+
107+
String contentType = exchange.getIn().getHeader(CONTENT_TYPE, String.class);
108+
if (contentType != null) {
109+
span.setTag(STORAGE_DATALAKE_CONTENT_TYPE, contentType);
110+
}
111+
112+
Map metadata = exchange.getIn().getHeader(METADATA, Map.class);
113+
if (metadata != null) {
114+
span.setTag(STORAGE_DATALAKE_METADATA, metadata.toString());
115+
}
116+
117+
OffsetDateTime lastModified = exchange.getIn().getHeader(LAST_MODIFIED, OffsetDateTime.class);
118+
if (lastModified != null) {
119+
span.setTag(STORAGE_DATALAKE_LAST_MODIFIED, lastModified.toString());
120+
}
121+
122+
Long position = exchange.getIn().getHeader(POSITION, Long.class);
123+
if (position != null) {
124+
span.setTag(STORAGE_DATALAKE_POSITION, position);
125+
}
126+
127+
String expression = exchange.getIn().getHeader(EXPRESSION, String.class);
128+
if (expression != null) {
129+
span.setTag(STORAGE_DATALAKE_EXPRESSION, expression);
130+
}
131+
}
132+
133+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.tracing.decorators;
18+
19+
import java.time.Duration;
20+
import java.time.OffsetDateTime;
21+
import java.util.Map;
22+
23+
import org.apache.camel.Endpoint;
24+
import org.apache.camel.Exchange;
25+
import org.apache.camel.Message;
26+
import org.apache.camel.tracing.MockSpanAdapter;
27+
import org.apache.camel.tracing.TagConstants;
28+
import org.junit.jupiter.api.Test;
29+
import org.mockito.Mockito;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
33+
public class AzureStorageDataLakeSpanDecoratorTest {
34+
35+
@Test
36+
public void testGetOperationNameFromHeader() {
37+
String operation = "upload";
38+
Exchange exchange = Mockito.mock(Exchange.class);
39+
Message message = Mockito.mock(Message.class);
40+
41+
Mockito.when(exchange.getIn()).thenReturn(message);
42+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.OPERATION, String.class)).thenReturn(operation);
43+
44+
AbstractSpanDecorator decorator = new AzureStorageDataLakeSpanDecorator();
45+
46+
assertEquals(operation, decorator.getOperationName(exchange, null));
47+
}
48+
49+
@Test
50+
public void testGetOperationNameFromHeaderWithEnum() {
51+
operationEnum operation = operationEnum.upload;
52+
53+
Exchange exchange = Mockito.mock(Exchange.class);
54+
Message message = Mockito.mock(Message.class);
55+
Mockito.when(exchange.getIn()).thenReturn(message);
56+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.OPERATION, String.class))
57+
.thenReturn(operation.toString());
58+
59+
AbstractSpanDecorator decorator = new AzureStorageDataLakeSpanDecorator();
60+
61+
assertEquals(operation.toString(), decorator.getOperationName(exchange, null));
62+
}
63+
64+
@Test
65+
public void testGetOperationNameFromQueryParameter() {
66+
Endpoint endpoint = Mockito.mock(Endpoint.class);
67+
Exchange exchange = Mockito.mock(Exchange.class);
68+
Message message = Mockito.mock(Message.class);
69+
70+
Mockito.when(endpoint.getEndpointUri()).thenReturn("azure-storage-datalake:myAccount/myFileSystem?operation=upload");
71+
Mockito.when(exchange.getIn()).thenReturn(message);
72+
73+
AbstractSpanDecorator decorator = new AzureStorageDataLakeSpanDecorator();
74+
75+
assertEquals("upload", decorator.getOperationName(exchange, endpoint));
76+
}
77+
78+
@Test
79+
public void testPre() {
80+
String fileSystemName = "myFileSystem";
81+
String directoryName = "myDirectory";
82+
String fileName = "myFile";
83+
String path = "myPath";
84+
String expression = "myExpression";
85+
String contentType = "myContentType";
86+
Duration timeout = Duration.ofDays(7);
87+
Map<String, String> metadata = Map.of("key1", "value1", "key2", "value2");
88+
OffsetDateTime lastModified = OffsetDateTime.now();
89+
Long position = 21L;
90+
91+
Endpoint endpoint = Mockito.mock(Endpoint.class);
92+
Exchange exchange = Mockito.mock(Exchange.class);
93+
Message message = Mockito.mock(Message.class);
94+
95+
Mockito.when(endpoint.getEndpointUri()).thenReturn("azure-storage-datalake:account/myFileSystem");
96+
Mockito.when(exchange.getIn()).thenReturn(message);
97+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.FILESYSTEM_NAME, String.class))
98+
.thenReturn(fileSystemName);
99+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.DIRECTORY_NAME, String.class))
100+
.thenReturn(directoryName);
101+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.FILE_NAME, String.class)).thenReturn(fileName);
102+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.PATH, String.class)).thenReturn(path);
103+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.EXPRESSION, String.class)).thenReturn(expression);
104+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.CONTENT_TYPE, String.class)).thenReturn(contentType);
105+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.TIMEOUT, Duration.class)).thenReturn(timeout);
106+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.METADATA, Map.class))
107+
.thenReturn(metadata);
108+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.LAST_MODIFIED, OffsetDateTime.class))
109+
.thenReturn(lastModified);
110+
Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.POSITION, Long.class)).thenReturn(position);
111+
112+
AbstractSpanDecorator decorator = new AzureStorageDataLakeSpanDecorator();
113+
114+
MockSpanAdapter span = new MockSpanAdapter();
115+
116+
decorator.pre(span, exchange, endpoint);
117+
118+
assertEquals("azure-storage-datalake", span.tags().get(TagConstants.DB_SYSTEM));
119+
assertEquals(fileSystemName, span.tags().get(TagConstants.DB_NAME));
120+
assertEquals(directoryName, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_DIRECTORY_NAME));
121+
assertEquals(fileName, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_FILE_NAME));
122+
assertEquals(path, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_PATH));
123+
assertEquals(expression, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_EXPRESSION));
124+
assertEquals(contentType, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_CONTENT_TYPE));
125+
assertEquals(timeout.toString(), span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_TIMEOUT));
126+
assertEquals(metadata.toString(), span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_METADATA));
127+
assertEquals(lastModified.toString(),
128+
span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_LAST_MODIFIED));
129+
assertEquals(position, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_POSITION));
130+
}
131+
132+
enum operationEnum {
133+
upload
134+
}
135+
136+
}

0 commit comments

Comments
 (0)