Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.semconv.K8sAttributes;
import io.opentelemetry.semconv.incubating.HostIncubatingAttributes;

public final class HostIdResourceUtil {
private HostIdResourceUtil() {}

public static Attributes createAttribute() {
AttributesBuilder builder = Attributes.builder();
HostId hostId = ServerHostInfoReader.INSTANCE.getHostId();
builder.put(AttributeKey.stringArrayKey("mac.addresses"), hostId.getMacAddresses());
public static Attributes createUamsAttributes() {
return createUamsAttributes(ServerHostInfoReader.INSTANCE.getHostId());
}

builder.put(HostIncubatingAttributes.HOST_ID, hostId.getHerokuDynoId());
builder.put(AttributeKey.stringKey("sw.uams.client.id"), hostId.getUamsClientId());
static Attributes createUamsAttributes(HostId hostId) {
return Attributes.builder()
.put(AttributeKey.stringKey("sw.uams.client.id"), hostId.getUamsClientId())
.build();
}

public static Attributes createK8sAttributes() {
return createK8sAttributes(ServerHostInfoReader.INSTANCE.getHostId());
}

static Attributes createK8sAttributes(HostId hostId) {
AttributesBuilder builder = Attributes.builder();
HostId.K8sMetadata k8sMetadata = hostId.getK8sMetadata();

if (k8sMetadata != null) {
Expand All @@ -44,4 +52,8 @@ public static Attributes createAttribute() {

return builder.build();
}

public static Attributes createAttribute() {
return createUamsAttributes().toBuilder().putAll(createK8sAttributes()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,11 @@
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.resources.Resource;
import lombok.Getter;
import lombok.Setter;

@AutoService(ComponentProvider.class)
public class ResourceComponentProvider implements ComponentProvider {
public class ApmResourceComponentProvider implements ComponentProvider {

public static final String COMPONENT_NAME = "swo/resource";

// Accumulated statically: create() merges into this field in place, and
// HostIdResourceComponentProvider reads it via getResource().merge(...). Assumes create() is
// invoked once; merging identical attributes is idempotent, but any new writer of this field
// must be aware the result is order-dependent.
@Getter @Setter private static Resource resource = Resource.getDefault();
public static final String COMPONENT_NAME = "swo_apm";

@Override
public Class<Resource> getType() {
Expand All @@ -54,7 +46,6 @@ public Resource create(DeclarativeConfigProperties declarativeConfigProperties)
Attributes resourceAttributes =
Attributes.of(moduleKey, "apm", versionKey, BuildConfig.SOLARWINDS_AGENT_VERSION);

resource = resource.merge(Resource.create(resourceAttributes));
return resource;
return Resource.create(resourceAttributes);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* © SolarWinds Worldwide, LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.solarwinds.opentelemetry.extensions.config.provider;

import com.google.auto.service.AutoService;
import com.solarwinds.opentelemetry.extensions.config.HostIdResourceUtil;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.K8sAttributes;

@AutoService(ComponentProvider.class)
public class K8sResourceComponentProvider implements ComponentProvider {

public static final String COMPONENT_NAME = "swo_k8s";

@Override
public Class<Resource> getType() {
return Resource.class;
}

@Override
public String getName() {
return COMPONENT_NAME;
}

@Override
public Resource create(DeclarativeConfigProperties config) {
AttributesBuilder builder = HostIdResourceUtil.createK8sAttributes().toBuilder();
putIfConfigured(builder, K8sAttributes.K8S_NAMESPACE_NAME, config.getString("namespace"));
putIfConfigured(builder, K8sAttributes.K8S_POD_UID, config.getString("pod_uid"));
putIfConfigured(builder, K8sAttributes.K8S_POD_NAME, config.getString("pod_name"));
return Resource.create(builder.build());
}

private static void putIfConfigured(
AttributesBuilder builder, AttributeKey<String> key, String value) {
if (value != null && !value.isEmpty()) {
builder.put(key, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import io.opentelemetry.sdk.resources.Resource;

@AutoService(ComponentProvider.class)
public class HostIdResourceComponentProvider implements ComponentProvider {
public class UamsResourceComponentProvider implements ComponentProvider {

public static final String COMPONENT_NAME = "swo/hostIdResource";
public static final String COMPONENT_NAME = "swo_uams";

@Override
public Class<Resource> getType() {
Expand All @@ -40,9 +40,7 @@ public String getName() {

@Override
public Resource create(DeclarativeConfigProperties declarativeConfigProperties) {
Attributes attribute = HostIdResourceUtil.createAttribute();
ResourceComponentProvider.setResource(
ResourceComponentProvider.getResource().merge(Resource.create(attribute)));
Attributes attribute = HostIdResourceUtil.createUamsAttributes();
return Resource.create(attribute);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,55 @@

package com.solarwinds.opentelemetry.extensions.config;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.solarwinds.joboe.core.HostId;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import java.util.List;
import io.opentelemetry.semconv.K8sAttributes;
import org.junit.jupiter.api.Test;

class HostIdResourceUtilTest {
@Test
void testCreateAttributes() {
Attributes attribute = HostIdResourceUtil.createAttribute();
List<String> macAddresses = attribute.get(AttributeKey.stringArrayKey("mac.addresses"));
assertNotNull(macAddresses);
void uamsAttributesDoNotIncludeDroppedKeys() {
Attributes attributes = HostIdResourceUtil.createUamsAttributes();
assertNull(attributes.get(AttributeKey.stringArrayKey("mac.addresses")));
assertNull(attributes.get(AttributeKey.stringKey("host.id")));
}

@Test
void k8sAttributesOnlyContainK8sKeys() {
HostIdResourceUtil.createK8sAttributes()
.forEach((key, value) -> assertTrue(key.getKey().startsWith("k8s.")));
}

@Test
void uamsAttributesContainClientId() {
HostId hostId = HostId.builder().uamsClientId("client-id").herokuDynoId("dyno").build();
Attributes attributes = HostIdResourceUtil.createUamsAttributes(hostId);

assertEquals("client-id", attributes.get(AttributeKey.stringKey("sw.uams.client.id")));
assertEquals(1, attributes.size());
}

@Test
void k8sAttributesAreMappedFromMetadata() {
HostId hostId =
HostId.builder()
.k8sMetadata(
HostId.K8sMetadata.builder().namespace("ns").podName("pod").podUid("uid").build())
.build();
Attributes attributes = HostIdResourceUtil.createK8sAttributes(hostId);

assertEquals("ns", attributes.get(K8sAttributes.K8S_NAMESPACE_NAME));
assertEquals("pod", attributes.get(K8sAttributes.K8S_POD_NAME));
assertEquals("uid", attributes.get(K8sAttributes.K8S_POD_UID));
}

@Test
void k8sAttributesEmptyWithoutMetadata() {
assertEquals(0, HostIdResourceUtil.createK8sAttributes(HostId.builder().build()).size());
}
}
Loading
Loading