forked from apache/rocketmq
-
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.
- Loading branch information
Showing
12 changed files
with
1,107 additions
and
0 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
95 changes: 95 additions & 0 deletions
95
...rc/main/java/org/apache/rocketmq/client/trace/hook/ConsumeMessageOpenTracingHookImpl.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,95 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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 org.apache.rocketmq.client.trace.hook; | ||
|
||
import io.opentracing.Span; | ||
import io.opentracing.SpanContext; | ||
import io.opentracing.Tracer; | ||
import io.opentracing.propagation.Format; | ||
import io.opentracing.propagation.TextMapAdapter; | ||
import io.opentracing.tag.Tags; | ||
import org.apache.rocketmq.client.hook.ConsumeMessageContext; | ||
import org.apache.rocketmq.client.hook.ConsumeMessageHook; | ||
import org.apache.rocketmq.client.trace.TraceConstants; | ||
import org.apache.rocketmq.common.message.MessageConst; | ||
import org.apache.rocketmq.common.message.MessageExt; | ||
import org.apache.rocketmq.common.protocol.NamespaceUtil; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class ConsumeMessageOpenTracingHookImpl implements ConsumeMessageHook { | ||
|
||
private Tracer tracer; | ||
|
||
public ConsumeMessageOpenTracingHookImpl(Tracer tracer) { | ||
this.tracer = tracer; | ||
} | ||
|
||
@Override | ||
public String hookName() { | ||
return "ConsumeMessageOpenTracingHook"; | ||
} | ||
|
||
@Override | ||
public void consumeMessageBefore(ConsumeMessageContext context) { | ||
if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) { | ||
return; | ||
} | ||
List<Span> spanList = new ArrayList<>(); | ||
for (MessageExt msg : context.getMsgList()) { | ||
if (msg == null) { | ||
continue; | ||
} | ||
Tracer.SpanBuilder spanBuilder = tracer | ||
.buildSpan(TraceConstants.FROM_PREFIX + msg.getTopic()) | ||
.withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER); | ||
SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapAdapter(msg.getProperties())); | ||
if (spanContext != null) { | ||
spanBuilder.asChildOf(spanContext); | ||
} | ||
Span span = spanBuilder.start(); | ||
|
||
span.setTag(Tags.PEER_SERVICE, TraceConstants.ROCKETMQ_SERVICE); | ||
span.setTag(Tags.MESSAGE_BUS_DESTINATION, NamespaceUtil.withoutNamespace(msg.getTopic())); | ||
span.setTag(TraceConstants.ROCKETMQ_MSG_ID, msg.getMsgId()); | ||
span.setTag(TraceConstants.ROCKETMQ_TAGS, msg.getTags()); | ||
span.setTag(TraceConstants.ROCKETMQ_KEYS, msg.getKeys()); | ||
span.setTag(TraceConstants.ROCKETMQ_BODY_LENGTH, msg.getStoreSize()); | ||
span.setTag(TraceConstants.ROCKETMQ_RETRY_TIMERS, msg.getReconsumeTimes()); | ||
span.setTag(TraceConstants.ROCKETMQ_REGION_ID, msg.getProperty(MessageConst.PROPERTY_MSG_REGION)); | ||
spanList.add(span); | ||
} | ||
context.setMqTraceContext(spanList); | ||
} | ||
|
||
@Override | ||
public void consumeMessageAfter(ConsumeMessageContext context) { | ||
if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) { | ||
return; | ||
} | ||
List<Span> spanList = (List<Span>) context.getMqTraceContext(); | ||
if (spanList == null) { | ||
return; | ||
} | ||
for (Span span : spanList) { | ||
span.setTag(TraceConstants.ROCKETMQ_SUCCESS, context.isSuccess()); | ||
span.finish(); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...rc/main/java/org/apache/rocketmq/client/trace/hook/EndTransactionOpenTracingHookImpl.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,72 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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 org.apache.rocketmq.client.trace.hook; | ||
|
||
import io.opentracing.Span; | ||
import io.opentracing.SpanContext; | ||
import io.opentracing.Tracer; | ||
import io.opentracing.propagation.Format; | ||
import io.opentracing.propagation.TextMapAdapter; | ||
import io.opentracing.tag.Tags; | ||
import org.apache.rocketmq.client.hook.EndTransactionContext; | ||
import org.apache.rocketmq.client.hook.EndTransactionHook; | ||
import org.apache.rocketmq.client.trace.TraceConstants; | ||
import org.apache.rocketmq.common.message.Message; | ||
import org.apache.rocketmq.common.message.MessageType; | ||
|
||
public class EndTransactionOpenTracingHookImpl implements EndTransactionHook { | ||
|
||
private Tracer tracer; | ||
|
||
public EndTransactionOpenTracingHookImpl(Tracer tracer) { | ||
this.tracer = tracer; | ||
} | ||
|
||
@Override | ||
public String hookName() { | ||
return "EndTransactionOpenTracingHook"; | ||
} | ||
|
||
@Override | ||
public void endTransaction(EndTransactionContext context) { | ||
if (context == null) { | ||
return; | ||
} | ||
Message msg = context.getMessage(); | ||
Tracer.SpanBuilder spanBuilder = tracer | ||
.buildSpan(TraceConstants.END_TRANSACTION) | ||
.withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_PRODUCER); | ||
SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapAdapter(msg.getProperties())); | ||
if (spanContext != null) { | ||
spanBuilder.asChildOf(spanContext); | ||
} | ||
|
||
Span span = spanBuilder.start(); | ||
span.setTag(Tags.PEER_SERVICE, TraceConstants.ROCKETMQ_SERVICE); | ||
span.setTag(Tags.MESSAGE_BUS_DESTINATION, msg.getTopic()); | ||
span.setTag(TraceConstants.ROCKETMQ_TAGS, msg.getTags()); | ||
span.setTag(TraceConstants.ROCKETMQ_KEYS, msg.getKeys()); | ||
span.setTag(TraceConstants.ROCKETMQ_SOTRE_HOST, context.getBrokerAddr()); | ||
span.setTag(TraceConstants.ROCKETMQ_MSG_ID, context.getMsgId()); | ||
span.setTag(TraceConstants.ROCKETMQ_MSG_TYPE, MessageType.Trans_msg_Commit.name()); | ||
span.setTag(TraceConstants.ROCKETMQ_TRANSACTION_ID, context.getTransactionId()); | ||
span.setTag(TraceConstants.ROCKETMQ_TRANSACTION_STATE, context.getTransactionState().name()); | ||
span.setTag(TraceConstants.ROCKETMQ_IS_FROM_TRANSACTION_CHECK, context.isFromTransactionCheck()); | ||
span.finish(); | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
...t/src/main/java/org/apache/rocketmq/client/trace/hook/SendMessageOpenTracingHookImpl.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,88 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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 org.apache.rocketmq.client.trace.hook; | ||
|
||
import io.opentracing.Span; | ||
import io.opentracing.SpanContext; | ||
import io.opentracing.Tracer; | ||
import io.opentracing.propagation.Format; | ||
import io.opentracing.propagation.TextMapAdapter; | ||
import io.opentracing.tag.Tags; | ||
import org.apache.rocketmq.client.hook.SendMessageContext; | ||
import org.apache.rocketmq.client.hook.SendMessageHook; | ||
import org.apache.rocketmq.client.producer.SendStatus; | ||
import org.apache.rocketmq.client.trace.TraceConstants; | ||
import org.apache.rocketmq.common.message.Message; | ||
|
||
public class SendMessageOpenTracingHookImpl implements SendMessageHook { | ||
|
||
private Tracer tracer; | ||
|
||
public SendMessageOpenTracingHookImpl(Tracer tracer) { | ||
this.tracer = tracer; | ||
} | ||
|
||
@Override | ||
public String hookName() { | ||
return "SendMessageOpenTracingHook"; | ||
} | ||
|
||
@Override | ||
public void sendMessageBefore(SendMessageContext context) { | ||
if (context == null) { | ||
return; | ||
} | ||
Message msg = context.getMessage(); | ||
Tracer.SpanBuilder spanBuilder = tracer | ||
.buildSpan(TraceConstants.TO_PREFIX + msg.getTopic()) | ||
.withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_PRODUCER); | ||
SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapAdapter(msg.getProperties())); | ||
if (spanContext != null) { | ||
spanBuilder.asChildOf(spanContext); | ||
} | ||
Span span = spanBuilder.start(); | ||
tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new TextMapAdapter(msg.getProperties())); | ||
span.setTag(Tags.PEER_SERVICE, TraceConstants.ROCKETMQ_SERVICE); | ||
span.setTag(Tags.MESSAGE_BUS_DESTINATION, msg.getTopic()); | ||
span.setTag(TraceConstants.ROCKETMQ_TAGS, msg.getTags()); | ||
span.setTag(TraceConstants.ROCKETMQ_KEYS, msg.getKeys()); | ||
span.setTag(TraceConstants.ROCKETMQ_SOTRE_HOST, context.getBrokerAddr()); | ||
span.setTag(TraceConstants.ROCKETMQ_MSG_TYPE, context.getMsgType().name()); | ||
span.setTag(TraceConstants.ROCKETMQ_BODY_LENGTH, msg.getBody().length); | ||
context.setMqTraceContext(span); | ||
} | ||
|
||
@Override | ||
public void sendMessageAfter(SendMessageContext context) { | ||
if (context == null || context.getMqTraceContext() == null) { | ||
return; | ||
} | ||
if (context.getSendResult() == null) { | ||
return; | ||
} | ||
|
||
if (context.getSendResult().getRegionId() == null) { | ||
return; | ||
} | ||
|
||
Span span = (Span) context.getMqTraceContext(); | ||
span.setTag(TraceConstants.ROCKETMQ_SUCCESS, context.getSendResult().getSendStatus().equals(SendStatus.SEND_OK)); | ||
span.setTag(TraceConstants.ROCKETMQ_MSG_ID, context.getSendResult().getMsgId()); | ||
span.setTag(TraceConstants.ROCKETMQ_REGION_ID, context.getSendResult().getRegionId()); | ||
span.finish(); | ||
} | ||
} |
Oops, something went wrong.