This repository was archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
Add Support for Opencensus to OpenTelemetry migration #2059
Merged
zoercai
merged 8 commits into
census-instrumentation:master
from
zoercai:opencensus-otel-migration-final
Oct 20, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2934f4b
Add support for custom context storage
a293f8a
Add reflection for OpenTelemetry trace component
zoercai e25717d
Review changes & Fix checkstyle
zoercai 4dce999
Fix test checkstyle
zoercai 55cfe19
Fix verify google java format
zoercai 5e8df76
Fix more verify google java format
zoercai 0b78da6
Allow migration library context manager to be loaded via reflection a…
zoercai cfd0ab2
Review changes
zoercai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,24 @@ | ||
| /* | ||
| * Copyright 2016-17, OpenCensus Authors | ||
| * | ||
| * 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 io.opencensus.trace; | ||
|
|
||
| public interface ContextHandle { | ||
|
|
||
| ContextHandle attach(); | ||
|
|
||
| void detach(ContextHandle contextHandle); | ||
| } |
This file contains hidden or 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,26 @@ | ||
| /* | ||
| * Copyright 2016-17, OpenCensus Authors | ||
| * | ||
| * 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 io.opencensus.trace; | ||
|
|
||
| public interface ContextManager { | ||
|
|
||
| ContextHandle currentContext(); | ||
|
|
||
| ContextHandle withValue(ContextHandle contextHandle, @javax.annotation.Nullable Span span); | ||
|
|
||
| Span getValue(ContextHandle contextHandle); | ||
| } |
This file contains hidden or 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java
This file contains hidden or 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,45 @@ | ||
| /* | ||
| * Copyright 2016-17, OpenCensus Authors | ||
| * | ||
| * 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 io.opencensus.trace.unsafe; | ||
|
|
||
| import io.grpc.Context; | ||
| import io.opencensus.trace.ContextHandle; | ||
|
|
||
| /** {@code ContextHandle} implementation using {@see io.grpc.Context}. */ | ||
| class ContextHandleImpl implements ContextHandle { | ||
|
|
||
| private final Context context; | ||
|
|
||
| public ContextHandleImpl(Context context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| Context getContext() { | ||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public ContextHandle attach() { | ||
| return new ContextHandleImpl(context.attach()); | ||
| } | ||
|
|
||
| @Override | ||
| public void detach(ContextHandle contextHandle) { | ||
| ContextHandleImpl impl = (ContextHandleImpl) contextHandle; | ||
| context.detach(impl.context); | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java
This file contains hidden or 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,79 @@ | ||
| /* | ||
| * Copyright 2016-17, OpenCensus Authors | ||
| * | ||
| * 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 io.opencensus.trace.unsafe; | ||
|
|
||
| import io.opencensus.internal.Provider; | ||
| import io.opencensus.trace.ContextHandle; | ||
| import io.opencensus.trace.ContextManager; | ||
| import io.opencensus.trace.Span; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| public class ContextHandleUtils { | ||
|
|
||
| // No instance of this class. | ||
| private ContextHandleUtils() {} | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(ContextHandleUtils.class.getName()); | ||
| private static final ContextManager CONTEXT_MANAGER = | ||
| loadContextManager(ContextManager.class.getClassLoader()); | ||
|
|
||
| private static ContextManager loadContextManager(@Nullable ClassLoader classLoader) { | ||
| try { | ||
| return Provider.createInstance( | ||
| Class.forName( | ||
| "io.opentelemetry.opencensusshim.OpenTelemetryContextManager", | ||
| /*initialize=*/ true, | ||
| classLoader), | ||
| ContextManager.class); | ||
| } catch (ClassNotFoundException e) { | ||
| LOGGER.log( | ||
| Level.FINE, | ||
| "Couldn't load full implementation for OpenTelemetry context manager, now loading " | ||
| + "original implementation.", | ||
| e); | ||
| } | ||
| return new ContextManagerImpl(); | ||
| } | ||
|
|
||
| public static ContextHandle currentContext() { | ||
| return CONTEXT_MANAGER.currentContext(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@code ContextHandle} with the given value set. | ||
| * | ||
| * @param context the parent {@code ContextHandle}. | ||
| * @param span the value to be set. | ||
| * @return a new context with the given value set. | ||
| */ | ||
| public static ContextHandle withValue( | ||
| ContextHandle context, @javax.annotation.Nullable Span span) { | ||
| return CONTEXT_MANAGER.withValue(context, span); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the value from the specified {@code ContextHandle}. | ||
| * | ||
| * @param context the specified {@code ContextHandle}. | ||
| * @return the value from the specified {@code ContextHandle}. | ||
| */ | ||
| public static Span getValue(ContextHandle context) { | ||
| return CONTEXT_MANAGER.getValue(context); | ||
| } | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java
This file contains hidden or 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,52 @@ | ||
| /* | ||
| * Copyright 2016-17, OpenCensus Authors | ||
| * | ||
| * 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 io.opencensus.trace.unsafe; | ||
|
|
||
| import io.grpc.Context; | ||
| import io.opencensus.trace.ContextHandle; | ||
| import io.opencensus.trace.ContextManager; | ||
| import io.opencensus.trace.Span; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** Default {@code ContextManager} implementation using io.grpc.Context */ | ||
| public class ContextManagerImpl implements ContextManager { | ||
|
|
||
| @Override | ||
| public ContextHandle currentContext() { | ||
| return wrapContext(Context.current()); | ||
| } | ||
|
|
||
| @Override | ||
| public ContextHandle withValue(ContextHandle contextHandle, @Nullable Span span) { | ||
| return wrapContext(ContextUtils.withValue(unwrapContext(contextHandle), span)); | ||
| } | ||
|
|
||
| @Override | ||
| public Span getValue(ContextHandle contextHandle) { | ||
| return ContextUtils.getValue(unwrapContext(contextHandle)); | ||
| } | ||
|
|
||
| private static ContextHandle wrapContext(Context context) { | ||
| return new ContextHandleImpl(context); | ||
| } | ||
|
|
||
| private static Context unwrapContext(ContextHandle contextHandle) { | ||
| return ((ContextHandleImpl) contextHandle).getContext(); | ||
| } | ||
|
|
||
| protected ContextManagerImpl() {} | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.