Skip to content

Commit 4ff56a6

Browse files
authored
[Logs 13] Attach server.address to Logs (#4394)
* Add Log feature to Java SDK * Rate limit for log items * Add options for logs * Add batch processor for logs * Use a separate ExecutorService for log batching * Reduce locking when log event is created * Add system tests for Logs * Separate enum for SentryLogLevel * Remove logsSampleRate option * Move logs options out of experimental namespace * Add severity_number to SentryLogItem * Logs review feedback * mark captureBatchedLogEvents internal * remove hint for logs * Attach server.address to logs * Allow null for log event attribute value
1 parent ff3aa9c commit 4ff56a6

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

sentry/api/sentry.api

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,11 @@ public final class io/sentry/Hint {
589589
public static fun withAttachments (Ljava/util/List;)Lio/sentry/Hint;
590590
}
591591

592+
public final class io/sentry/HostnameCache {
593+
public fun getHostname ()Ljava/lang/String;
594+
public static fun getInstance ()Lio/sentry/HostnameCache;
595+
}
596+
592597
public final class io/sentry/HttpStatusCodeRange {
593598
public static final field DEFAULT_MAX I
594599
public static final field DEFAULT_MIN I

sentry/src/main/java/io/sentry/HostnameCache.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry;
22

3+
import io.sentry.util.AutoClosableReentrantLock;
34
import io.sentry.util.Objects;
45
import java.net.InetAddress;
56
import java.util.concurrent.Callable;
@@ -11,6 +12,7 @@
1112
import java.util.concurrent.TimeUnit;
1213
import java.util.concurrent.TimeoutException;
1314
import java.util.concurrent.atomic.AtomicBoolean;
15+
import org.jetbrains.annotations.ApiStatus;
1416
import org.jetbrains.annotations.NotNull;
1517
import org.jetbrains.annotations.Nullable;
1618

@@ -25,13 +27,16 @@
2527
* <p>HostnameCache is a singleton and its instance should be obtained through {@link
2628
* HostnameCache#getInstance()}.
2729
*/
28-
final class HostnameCache {
30+
@ApiStatus.Internal
31+
public final class HostnameCache {
2932
private static final long HOSTNAME_CACHE_DURATION = TimeUnit.HOURS.toMillis(5);
3033

3134
/** Time before the get hostname operation times out (in ms). */
3235
private static final long GET_HOSTNAME_TIMEOUT = TimeUnit.SECONDS.toMillis(1);
3336

34-
@Nullable private static HostnameCache INSTANCE;
37+
private static volatile @Nullable HostnameCache INSTANCE;
38+
private static final @NotNull AutoClosableReentrantLock staticLock =
39+
new AutoClosableReentrantLock();
3540

3641
/** Time for which the cache is kept. */
3742
private final long cacheDuration;
@@ -47,10 +52,15 @@ final class HostnameCache {
4752
private final @NotNull ExecutorService executorService =
4853
Executors.newSingleThreadExecutor(new HostnameCacheThreadFactory());
4954

50-
static @NotNull HostnameCache getInstance() {
55+
public static @NotNull HostnameCache getInstance() {
5156
if (INSTANCE == null) {
52-
INSTANCE = new HostnameCache();
57+
try (final @NotNull ISentryLifecycleToken ignored = staticLock.acquire()) {
58+
if (INSTANCE == null) {
59+
INSTANCE = new HostnameCache();
60+
}
61+
}
5362
}
63+
5464
return INSTANCE;
5565
}
5666

@@ -93,7 +103,7 @@ boolean isClosed() {
93103
* @return the hostname of the current machine.
94104
*/
95105
@Nullable
96-
String getHostname() {
106+
public String getHostname() {
97107
if (expirationTimestamp < System.currentTimeMillis()
98108
&& updateRunning.compareAndSet(false, true)) {
99109
updateCache();

sentry/src/main/java/io/sentry/MainEventProcessor.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import io.sentry.protocol.SentryException;
88
import io.sentry.protocol.SentryTransaction;
99
import io.sentry.protocol.User;
10-
import io.sentry.util.AutoClosableReentrantLock;
1110
import io.sentry.util.HintUtils;
1211
import io.sentry.util.Objects;
1312
import java.io.Closeable;
@@ -28,8 +27,6 @@ public final class MainEventProcessor implements EventProcessor, Closeable {
2827
private final @NotNull SentryThreadFactory sentryThreadFactory;
2928
private final @NotNull SentryExceptionFactory sentryExceptionFactory;
3029
private volatile @Nullable HostnameCache hostnameCache = null;
31-
private final @NotNull AutoClosableReentrantLock hostnameCacheLock =
32-
new AutoClosableReentrantLock();
3330

3431
public MainEventProcessor(final @NotNull SentryOptions options) {
3532
this.options = Objects.requireNonNull(options, "The SentryOptions is required.");
@@ -183,11 +180,7 @@ private void setServerName(final @NotNull SentryBaseEvent event) {
183180

184181
private void ensureHostnameCache() {
185182
if (hostnameCache == null) {
186-
try (final @NotNull ISentryLifecycleToken ignored = hostnameCacheLock.acquire()) {
187-
if (hostnameCache == null) {
188-
hostnameCache = HostnameCache.getInstance();
189-
}
190-
}
183+
hostnameCache = HostnameCache.getInstance();
191184
}
192185
}
193186

sentry/src/main/java/io/sentry/logger/LoggerApi.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry.logger;
22

3+
import io.sentry.HostnameCache;
34
import io.sentry.IScope;
45
import io.sentry.ISpan;
56
import io.sentry.PropagationContext;
@@ -13,6 +14,7 @@
1314
import io.sentry.SpanId;
1415
import io.sentry.protocol.SdkVersion;
1516
import io.sentry.protocol.SentryId;
17+
import io.sentry.util.Platform;
1618
import io.sentry.util.TracingUtils;
1719
import java.util.HashMap;
1820
import org.jetbrains.annotations.ApiStatus;
@@ -163,9 +165,29 @@ private void captureLog(
163165

164166
attributes.put(
165167
"sentry.trace.parent_span_id", new SentryLogEventAttributeValue("string", spanId));
168+
169+
if (Platform.isJvm()) {
170+
setServerName(attributes);
171+
}
172+
166173
return attributes;
167174
}
168175

176+
private void setServerName(
177+
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes) {
178+
final @NotNull SentryOptions options = scopes.getOptions();
179+
final @Nullable String optionsServerName = options.getServerName();
180+
if (optionsServerName != null) {
181+
attributes.put(
182+
"server.address", new SentryLogEventAttributeValue("string", optionsServerName));
183+
} else if (options.isAttachServerName()) {
184+
final @Nullable String hostname = HostnameCache.getInstance().getHostname();
185+
if (hostname != null) {
186+
attributes.put("server.address", new SentryLogEventAttributeValue("string", hostname));
187+
}
188+
}
189+
}
190+
169191
private @NotNull String getType(final @Nullable Object arg) {
170192
if (arg instanceof Boolean) {
171193
return "boolean";

0 commit comments

Comments
 (0)