-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resource.Builder for convenient work with Resource class (#3065)
* Resource.Builder for convenient work with Resource class * Remove unused import * Adding javadoc comment * Spotless fix * Code review fixes * Adding Javadoc * Call Resource.create instead of AutoValue
- Loading branch information
1 parent
2f2af19
commit 1a9c909
Showing
6 changed files
with
270 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.resources; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
|
||
/** | ||
* A builder of {@link Resource} that allows to add key-value pairs and copy attributes from other | ||
* {@link Attributes} or {@link Resource}. | ||
*/ | ||
public class ResourceBuilder { | ||
|
||
private final AttributesBuilder attributesBuilder = Attributes.builder(); | ||
|
||
/** | ||
* Puts a String attribute into this. | ||
* | ||
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate | ||
* your keys, if possible. | ||
* | ||
* @return this Builder | ||
*/ | ||
public ResourceBuilder put(String key, String value) { | ||
if (key != null && value != null) { | ||
attributesBuilder.put(key, value); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Puts a long attribute into this. | ||
* | ||
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate | ||
* your keys, if possible. | ||
* | ||
* @return this Builder | ||
*/ | ||
public ResourceBuilder put(String key, long value) { | ||
if (key != null) { | ||
attributesBuilder.put(key, value); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Puts a double attribute into this. | ||
* | ||
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate | ||
* your keys, if possible. | ||
* | ||
* @return this Builder | ||
*/ | ||
public ResourceBuilder put(String key, double value) { | ||
if (key != null) { | ||
attributesBuilder.put(key, value); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Puts a boolean attribute into this. | ||
* | ||
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate | ||
* your keys, if possible. | ||
* | ||
* @return this Builder | ||
*/ | ||
public ResourceBuilder put(String key, boolean value) { | ||
if (key != null) { | ||
attributesBuilder.put(key, value); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Puts a String array attribute into this. | ||
* | ||
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate | ||
* your keys, if possible. | ||
* | ||
* @return this Builder | ||
*/ | ||
public ResourceBuilder put(String key, String... values) { | ||
if (key != null && values != null) { | ||
attributesBuilder.put(key, values); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Puts a Long array attribute into this. | ||
* | ||
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate | ||
* your keys, if possible. | ||
* | ||
* @return this Builder | ||
*/ | ||
public ResourceBuilder put(String key, long... values) { | ||
if (key != null && values != null) { | ||
attributesBuilder.put(key, values); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Puts a Double array attribute into this. | ||
* | ||
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate | ||
* your keys, if possible. | ||
* | ||
* @return this Builder | ||
*/ | ||
public ResourceBuilder put(String key, double... values) { | ||
if (key != null && values != null) { | ||
attributesBuilder.put(key, values); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Puts a Boolean array attribute into this. | ||
* | ||
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate | ||
* your keys, if possible. | ||
* | ||
* @return this Builder | ||
*/ | ||
public ResourceBuilder put(String key, boolean... values) { | ||
if (key != null && values != null) { | ||
attributesBuilder.put(key, values); | ||
} | ||
return this; | ||
} | ||
|
||
/** Puts a {@link AttributeKey} with associated value into this. */ | ||
public <T> ResourceBuilder put(AttributeKey<T> key, T value) { | ||
if (key != null && key.getKey() != null && key.getKey().length() > 0 && value != null) { | ||
attributesBuilder.put(key, value); | ||
} | ||
return this; | ||
} | ||
|
||
/** Puts a {@link AttributeKey} with associated value into this. */ | ||
public ResourceBuilder put(AttributeKey<Long> key, int value) { | ||
if (key != null && key.getKey() != null) { | ||
attributesBuilder.put(key, value); | ||
} | ||
return this; | ||
} | ||
|
||
/** Puts all {@link Attributes} into this. */ | ||
public ResourceBuilder putAll(Attributes attributes) { | ||
if (attributes != null) { | ||
attributesBuilder.putAll(attributes); | ||
} | ||
return this; | ||
} | ||
|
||
/** Puts all attributes from {@link Resource} into this. */ | ||
public ResourceBuilder putAll(Resource resource) { | ||
if (resource != null) { | ||
attributesBuilder.putAll(resource.getAttributes()); | ||
} | ||
return this; | ||
} | ||
|
||
/** Create the {@link Resource} from this. */ | ||
public Resource build() { | ||
return Resource.create(attributesBuilder.build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters