Skip to content

Commit cc42ae8

Browse files
authored
Resource detector implemented (#2605)
1 parent 25344a8 commit cc42ae8

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright Splunk Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.splunk.opentelemetry;
18+
19+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
20+
21+
import com.google.auto.service.AutoService;
22+
import io.opentelemetry.api.common.AttributeKey;
23+
import io.opentelemetry.api.common.Attributes;
24+
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
25+
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
26+
import io.opentelemetry.sdk.resources.Resource;
27+
28+
@AutoService(ComponentProvider.class)
29+
public class AppDynamicsVersionResourceDetector implements ComponentProvider {
30+
static final AttributeKey<String> APPD_AGENT_VER_KEY = stringKey("appdynamics.agent.version");
31+
32+
@Override
33+
public Class<Resource> getType() {
34+
return Resource.class;
35+
}
36+
37+
@Override
38+
public String getName() {
39+
return "appd_agent_version";
40+
}
41+
42+
@Override
43+
public Resource create(DeclarativeConfigProperties config) {
44+
// When AppD Agent starts Splunk Agent then it passes its version to Splunk Agent as a system
45+
// property
46+
String version = System.getProperty(APPD_AGENT_VER_KEY.getKey());
47+
if (version == null) {
48+
return Resource.empty();
49+
}
50+
51+
Attributes attributes = Attributes.of(APPD_AGENT_VER_KEY, version);
52+
return Resource.create(attributes);
53+
}
54+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright Splunk Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.splunk.opentelemetry;
18+
19+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
20+
21+
import io.opentelemetry.sdk.resources.Resource;
22+
import org.junit.jupiter.api.Test;
23+
24+
class AppDynamicsVersionResourceDetectorTest {
25+
@Test
26+
void shouldDetectAppDynamicsVersion() {
27+
try {
28+
// given
29+
String appDVersion = "version X";
30+
System.setProperty(
31+
AppDynamicsVersionResourceDetector.APPD_AGENT_VER_KEY.getKey(), appDVersion);
32+
AppDynamicsVersionResourceDetector detector = new AppDynamicsVersionResourceDetector();
33+
34+
// when
35+
Resource resource = detector.create(null);
36+
37+
// then
38+
assertThat(
39+
resource.getAttributes().get(AppDynamicsVersionResourceDetector.APPD_AGENT_VER_KEY))
40+
.isEqualTo(appDVersion);
41+
} finally {
42+
System.clearProperty(AppDynamicsVersionResourceDetector.APPD_AGENT_VER_KEY.getKey());
43+
}
44+
}
45+
46+
@Test
47+
void shouldHandleMissingAppDynamicsVersion() {
48+
// given
49+
AppDynamicsVersionResourceDetector detector = new AppDynamicsVersionResourceDetector();
50+
51+
// when
52+
Resource resource = detector.create(null);
53+
54+
// then
55+
assertThat(resource.getAttributes().isEmpty()).isTrue();
56+
}
57+
}

0 commit comments

Comments
 (0)