-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert camel instrumentation to instrumenter api (open-telemetry#4973)
- Loading branch information
Showing
14 changed files
with
380 additions
and
297 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
117 changes: 117 additions & 0 deletions
117
...ain/java/io/opentelemetry/javaagent/instrumentation/apachecamel/ActiveContextManager.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,117 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
// Includes work from: | ||
/* | ||
* Apache Camel Opentracing Component | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional information regarding | ||
* copyright ownership. The ASF licenses this file to You 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 | ||
* | ||
* <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p>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.opentelemetry.javaagent.instrumentation.apachecamel; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.apachecamel.CamelSingletons.instrumenter; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import javax.annotation.Nullable; | ||
import org.apache.camel.Exchange; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** Utility class for managing active contexts as a stack associated with an exchange. */ | ||
class ActiveContextManager { | ||
|
||
private static final String ACTIVE_CONTEXT_PROPERTY = "OpenTelemetry.activeContext"; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ActiveContextManager.class); | ||
|
||
private ActiveContextManager() {} | ||
|
||
/** | ||
* This method activates the supplied context for the supplied exchange. If an existing context is | ||
* found for the exchange it will be pushed onto a stack. | ||
* | ||
* @param context The exchange | ||
* @param request The context | ||
*/ | ||
public static void activate(Context context, CamelRequest request) { | ||
Exchange exchange = request.getExchange(); | ||
ContextWithScope parent = exchange.getProperty(ACTIVE_CONTEXT_PROPERTY, ContextWithScope.class); | ||
ContextWithScope contextWithScope = ContextWithScope.activate(parent, context, request); | ||
exchange.setProperty(ACTIVE_CONTEXT_PROPERTY, contextWithScope); | ||
logger.debug("Activated a span: {}", contextWithScope); | ||
} | ||
|
||
/** | ||
* This method deactivates an existing active context associated with the supplied exchange. Once | ||
* deactivated, if a parent span is found associated with the stack for the exchange, it will be | ||
* restored as the current span for the exchange. | ||
* | ||
* @param exchange The exchange | ||
*/ | ||
public static Context deactivate(Exchange exchange) { | ||
ContextWithScope contextWithScope = | ||
exchange.getProperty(ACTIVE_CONTEXT_PROPERTY, ContextWithScope.class); | ||
|
||
if (contextWithScope != null) { | ||
contextWithScope.deactivate(exchange.getException()); | ||
exchange.setProperty(ACTIVE_CONTEXT_PROPERTY, contextWithScope.getParent()); | ||
logger.debug("Deactivated span: {}", contextWithScope); | ||
return contextWithScope.context; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static class ContextWithScope { | ||
@Nullable private final ContextWithScope parent; | ||
@Nullable private final Context context; | ||
private final CamelRequest request; | ||
@Nullable private final Scope scope; | ||
|
||
public ContextWithScope( | ||
ContextWithScope parent, Context context, CamelRequest request, Scope scope) { | ||
this.parent = parent; | ||
this.context = context; | ||
this.request = request; | ||
this.scope = scope; | ||
} | ||
|
||
public static ContextWithScope activate( | ||
ContextWithScope parent, Context context, CamelRequest request) { | ||
Scope scope = context != null ? context.makeCurrent() : null; | ||
return new ContextWithScope(parent, context, request, scope); | ||
} | ||
|
||
public ContextWithScope getParent() { | ||
return parent; | ||
} | ||
|
||
public void deactivate(Exception exception) { | ||
if (scope == null) { | ||
return; | ||
} | ||
scope.close(); | ||
instrumenter().end(context, request, null, exception); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ContextWithScope [context=" + context + ", scope=" + scope + "]"; | ||
} | ||
} | ||
} |
128 changes: 0 additions & 128 deletions
128
...c/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/ActiveSpanManager.java
This file was deleted.
Oops, something went wrong.
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
34 changes: 34 additions & 0 deletions
34
...nt/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/CamelRequest.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,34 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.apachecamel; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import org.apache.camel.Endpoint; | ||
import org.apache.camel.Exchange; | ||
|
||
@AutoValue | ||
abstract class CamelRequest { | ||
|
||
public static CamelRequest create( | ||
SpanDecorator spanDecorator, | ||
Exchange exchange, | ||
Endpoint endpoint, | ||
CamelDirection camelDirection, | ||
SpanKind spanKind) { | ||
return new AutoValue_CamelRequest(spanDecorator, exchange, endpoint, camelDirection, spanKind); | ||
} | ||
|
||
public abstract SpanDecorator getSpanDecorator(); | ||
|
||
public abstract Exchange getExchange(); | ||
|
||
public abstract Endpoint getEndpoint(); | ||
|
||
public abstract CamelDirection getCamelDirection(); | ||
|
||
public abstract SpanKind getSpanKind(); | ||
} |
Oops, something went wrong.