Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ZipkinRestTemplateSender NAME. #282

Merged
merged 3 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* 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 com.alipay.sofa.tracer.plugins.zipkin.adapter;

import com.alipay.common.tracer.core.utils.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.regex.Pattern;

/**
* 网络操作工具类
*
* @author <a href=mailto:[email protected]>GengZhang</a>
*/
class NetUtils {
/**
* slf4j Logger for this class
*/
private final static Logger LOGGER = LoggerFactory.getLogger(NetUtils.class);

/**
* 任意地址
*/
public static final String ANYHOST = "0.0.0.0";
/**
* 本机地址正则
*/
private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$");

/**
* IPv4地址
*/
public static final Pattern IPV4_PATTERN = Pattern
.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");

private NetUtils() {
}

/**
* 是否本地地址 127.x.x.x 或者 localhost
*
* @param host 地址
* @return 是否本地地址
*/
public static boolean isLocalHost(String host) {
return StringUtils.isNotBlank(host)
&& (LOCAL_IP_PATTERN.matcher(host).matches() || "localhost".equalsIgnoreCase(host));
}

/**
* 是否默认地址 0.0.0.0
*
* @param host 地址
* @return 是否默认地址
*/
public static boolean isAnyHost(String host) {
return ANYHOST.equals(host);
}

/**
* 是否IPv4地址 0.0.0.0
*
* @param host 地址
* @return 是否默认地址
*/
public static boolean isIPv4Host(String host) {
return StringUtils.isNotBlank(host) && IPV4_PATTERN.matcher(host).matches();
}

/**
* 是否合法地址(非本地,非默认的IPv4地址)
*
* @param address InetAddress
* @return 是否合法
*/
private static boolean isValidAddress(InetAddress address) {
if (address == null || address.isLoopbackAddress()) {
return false;
}
String name = address.getHostAddress();
return (name != null && !isAnyHost(name) && !isLocalHost(name) && isIPv4Host(name));
}

/**
* 得到本机IPv4地址
*
* @return ip地址
*/
public static String getLocalIpv4() {
InetAddress address = getLocalAddress();
return address == null ? null : address.getHostAddress();
}

/**
* 遍历本地网卡,返回第一个合理的IP,保存到缓存中
*
* @return 本地网卡IP
*/
public static InetAddress getLocalAddress() {
InetAddress localAddress = null;
try {
localAddress = InetAddress.getLocalHost();
if (isValidAddress(localAddress)) {
return localAddress;
}
} catch (Throwable e) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e);
}
}
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces != null) {
while (interfaces.hasMoreElements()) {
try {
NetworkInterface network = interfaces.nextElement();
Enumeration<InetAddress> addresses = network.getInetAddresses();
while (addresses.hasMoreElements()) {
try {
InetAddress address = addresses.nextElement();
if (isValidAddress(address)) {
return address;
}
} catch (Throwable e) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn(
"Error when retrieving ip address: " + e.getMessage(), e);
}
}
}
} catch (Throwable e) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e);
}
}
}
}
} catch (Throwable e) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e);
}
}
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Can't get valid host, will use 127.0.0.1 instead.");
}
return localAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import com.alipay.common.tracer.core.span.LogData;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.utils.StringUtils;

import java.net.InetAddress;
import java.util.Map;

import io.opentracing.tag.Tags;
import zipkin2.Endpoint;
import zipkin2.Span;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Map;

/***
* ZipkinV2SpanAdapter : convent sofaTracer span model to zipkin span model
Expand All @@ -39,10 +39,11 @@ public class ZipkinV2SpanAdapter {
/**
* cache and performance improve
*/
private int ipAddressInt = -1;
private InetAddress localIpAddress = null;

/**
* convent sofaTracerSpan model to zipKinSpan model
*
* @param sofaTracerSpan original span
* @return zipkinSpan model
*/
Expand Down Expand Up @@ -101,6 +102,7 @@ public static long spanIdToLong(String spanId) {

/**
* from http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash
*
* @param data String data
* @return fnv hash code
*/
Expand All @@ -116,22 +118,16 @@ public static long FNV64HashCode(String data) {
}

private Endpoint getZipkinEndpoint(SofaTracerSpan span) {
InetAddress ipAddress = null;
if (this.ipAddressInt <= 0) {
try {
ipAddress = InetAddress.getLocalHost();
this.ipAddressInt = ByteBuffer.wrap(ipAddress.getAddress()).getInt();
} catch (UnknownHostException e) {
//127.0.0.1 256
this.ipAddressInt = 256 * 256 * 256 * 127 + 1;
}
if (localIpAddress == null) {
localIpAddress = NetUtils.getLocalAddress();
}
String appName = span.getTagsWithStr().get(CommonSpanTags.LOCAL_APP);
return Endpoint.newBuilder().serviceName(appName).ip(ipAddress).build();
return Endpoint.newBuilder().serviceName(appName).ip(localIpAddress).build();
}

/**
* Put the baggage data into the tags
*
* @param zipkinSpan
* @param span
*/
Expand All @@ -151,6 +147,7 @@ private void addZipkinTagsWithBaggage(Span.Builder zipkinSpan, SofaTracerSpan sp

/**
* convent Annotations
*
* @param zipkinSpan
* @param span
*/
Expand All @@ -174,6 +171,7 @@ private void addZipkinAnnotations(Span.Builder zipkinSpan, SofaTracerSpan span)

/**
* convent tags
*
* @param zipkinSpan
* @param span
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Call<Void> sendSpans(List<byte[]> encodedSpans) {
} catch (Throwable e) {
SelfLog.error("Failed to report span to remote server. Current rest url is " + url, e);
}
return null;
return Call.create(null);
}

private void post(byte[] json) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void sendSpans() throws InterruptedException {
encodedSpans.add(tests);
Call<Void> voidCall = zipkinRestTemplateSender.sendSpans(encodedSpans);
Thread.sleep(500);
Assert.assertTrue(voidCall == null);
Assert.assertTrue(voidCall != null);
}
}