-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
1 deletion.
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
139 changes: 139 additions & 0 deletions
139
...com/microsoft/applicationinsights/agent/internal/init/TimestampingLogRecordProcessor.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,139 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.applicationinsights.agent.internal.init; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.logs.Severity; | ||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.common.InstrumentationScopeInfo; | ||
import io.opentelemetry.sdk.logs.LogRecordProcessor; | ||
import io.opentelemetry.sdk.logs.ReadWriteLogRecord; | ||
import io.opentelemetry.sdk.logs.data.Body; | ||
import io.opentelemetry.sdk.logs.data.LogRecordData; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.time.Instant; | ||
import javax.annotation.Nullable; | ||
|
||
// this is just needed temporarily until | ||
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/8761 | ||
class TimestampingLogRecordProcessor implements LogRecordProcessor { | ||
|
||
private final LogRecordProcessor delegate; | ||
|
||
public TimestampingLogRecordProcessor(LogRecordProcessor delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void onEmit(Context context, ReadWriteLogRecord logRecord) { | ||
delegate.onEmit(context, new TimestampingReadWriteLogRecord(logRecord, Instant.now())); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
return delegate.shutdown(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode forceFlush() { | ||
return delegate.forceFlush(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
delegate.close(); | ||
} | ||
|
||
private static class TimestampingReadWriteLogRecord implements ReadWriteLogRecord { | ||
|
||
private final ReadWriteLogRecord delegate; | ||
private final Instant timestamp; | ||
|
||
private TimestampingReadWriteLogRecord(ReadWriteLogRecord delegate, Instant timestamp) { | ||
this.delegate = delegate; | ||
this.timestamp = timestamp; | ||
} | ||
|
||
@Override | ||
public <T> ReadWriteLogRecord setAttribute(AttributeKey<T> key, T value) { | ||
return delegate.setAttribute(key, value); | ||
} | ||
|
||
@Override | ||
public LogRecordData toLogRecordData() { | ||
return new TimestampedLogRecordData(delegate.toLogRecordData(), timestamp); | ||
} | ||
} | ||
|
||
private static class TimestampedLogRecordData implements LogRecordData { | ||
|
||
private final LogRecordData delegate; | ||
private final Instant timestamp; | ||
|
||
private TimestampedLogRecordData(LogRecordData delegate, Instant timestamp) { | ||
this.delegate = delegate; | ||
this.timestamp = timestamp; | ||
} | ||
|
||
@Override | ||
public Resource getResource() { | ||
return delegate.getResource(); | ||
} | ||
|
||
@Override | ||
public InstrumentationScopeInfo getInstrumentationScopeInfo() { | ||
return delegate.getInstrumentationScopeInfo(); | ||
} | ||
|
||
@Override | ||
public long getTimestampEpochNanos() { | ||
long timestampEpochNanos = delegate.getTimestampEpochNanos(); | ||
if (timestampEpochNanos == 0) { | ||
return SECONDS.toNanos(timestamp.getEpochSecond()) + timestamp.getNano(); | ||
} | ||
return timestampEpochNanos; | ||
} | ||
|
||
@Override | ||
public long getObservedTimestampEpochNanos() { | ||
return delegate.getObservedTimestampEpochNanos(); | ||
} | ||
|
||
@Override | ||
public SpanContext getSpanContext() { | ||
return delegate.getSpanContext(); | ||
} | ||
|
||
@Override | ||
public Severity getSeverity() { | ||
return delegate.getSeverity(); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public String getSeverityText() { | ||
return delegate.getSeverityText(); | ||
} | ||
|
||
@Override | ||
public Body getBody() { | ||
return delegate.getBody(); | ||
} | ||
|
||
@Override | ||
public Attributes getAttributes() { | ||
return delegate.getAttributes(); | ||
} | ||
|
||
@Override | ||
public int getTotalAttributeCount() { | ||
return delegate.getTotalAttributeCount(); | ||
} | ||
} | ||
} |
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