Resolve envoy service name from a different position (#6654)

This commit is contained in:
Gao Hongtao 2021-03-31 12:09:18 +08:00 committed by GitHub
parent 9a3e8357cc
commit 7d06c210ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 41 additions and 18 deletions

View File

@ -63,6 +63,7 @@ Release Notes.
* Optimize the self monitoring grafana dashboard.
* Enhance the export service.
* Add function `retagByK8sMeta` and opt type `K8sRetagType.Pod2Service` in MAL for k8s to relate pods and services.
* Using "service.istio.io/canonical-name" to replace "app" label to resolve Envoy ALS service name
#### UI
* Update selector scroller to show in all pages.

View File

@ -327,7 +327,7 @@ envoy-metric:
# the available variables are `pod`, `service`, f.e., you can use `${service.metadata.name}-${pod.metadata.labels.version}`
# to append the version number to the service name.
# Be careful, when using environment variables to pass this configuration, use single quotes(`''`) to avoid it being evaluated by the shell.
k8sServiceNameRule: ${K8S_SERVICE_NAME_RULE:"${service.metadata.name}"}
k8sServiceNameRule: ${K8S_SERVICE_NAME_RULE:"${pod.metadata.labels.(service.istio.io/canonical-name)}"}
prometheus-fetcher:
selector: ${SW_PROMETHEUS_FETCHER:-}

View File

@ -13,5 +13,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.
serviceName: ${LABELS.app}
serviceName: ${LABELS."service.istio.io/canonical-name"}
serviceInstanceName: ${NAME}

View File

@ -48,7 +48,8 @@ public class ResourceUtils {
if (url == null) {
throw new FileNotFoundException("path not found: " + path);
}
return Objects.requireNonNull(new File(url.getPath()).listFiles(), "No files in " + path);
return Arrays.stream(Objects.requireNonNull(new File(url.getPath()).listFiles(), "No files in " + path))
.filter(File::isFile).toArray(File[]::new);
}
public static File[] getPathFiles(String parentPath, String[] fileNames) throws FileNotFoundException {

View File

@ -34,7 +34,7 @@ public class ServiceNameFormatter {
private final StringBuffer serviceNamePattern;
public ServiceNameFormatter(String rule) {
rule = StringUtils.defaultIfBlank(rule, "${service.metadata.name}");
rule = StringUtils.defaultIfBlank(rule, "${pod.metadata.labels.(service.istio.io/canonical-name)}");
this.properties = new ArrayList<>();
this.serviceNamePattern = new StringBuffer();

View File

@ -39,6 +39,9 @@ import org.apache.skywalking.oap.server.receiver.envoy.als.ServiceMetaInfo;
import org.yaml.snakeyaml.Yaml;
@Slf4j
/**
* FieldsHelper
*/
public enum FieldsHelper {
SINGLETON;
@ -82,7 +85,24 @@ public enum FieldsHelper {
final StringBuffer serviceNamePattern = new StringBuffer();
while (m.find()) {
final String property = m.group("property");
flatBuffersFieldNames.add(Splitter.on('.').omitEmptyStrings().splitToList(property));
List<String> tokens = Splitter.on('.').omitEmptyStrings().splitToList(property);
StringBuilder tokenBuffer = new StringBuilder();
List<String> compactedTokens = new ArrayList<>(tokens.size());
for (String token : tokens) {
if (tokenBuffer.length() == 0 && token.startsWith("\"")) {
tokenBuffer.append(token);
} else if (tokenBuffer.length() > 0) {
tokenBuffer.append(".").append(token);
if (token.endsWith("\"")) {
compactedTokens.add(tokenBuffer.toString().replaceAll("\"", ""));
tokenBuffer.setLength(0);
}
} else {
compactedTokens.add(token);
}
}
flatBuffersFieldNames.add(compactedTokens);
m.appendReplacement(serviceNamePattern, "%s");
}

View File

@ -28,6 +28,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static com.google.common.collect.ImmutableSortedMap.of;
import static junit.framework.TestCase.assertEquals;
@RequiredArgsConstructor
@ -40,22 +41,22 @@ public class ServiceNameFormatterTest {
return new Case[] {
new Case(
null,
ImmutableMap.of("service", service("Clash")),
ImmutableMap.of("pod", pod(of("service.istio.io/canonical-name", "Clash"))),
"Clash"
),
new Case(
null,
ImmutableMap.of("service", service("ClashX"), "pod", pod("version", "v1")),
ImmutableMap.of("pod", pod(of("service.istio.io/canonical-name", "ClashX", "service.istio.io/canonical-revision", "v1"))),
"ClashX"
),
new Case(
"${service.metadata.name}-${pod.metadata.labels.version}",
ImmutableMap.of("service", service("Clash"), "pod", pod("version", "v1beta")),
"${pod.metadata.labels.(service.istio.io/canonical-name)}-${pod.metadata.labels.(service.istio.io/canonical-revision)}",
ImmutableMap.of("pod", pod(of("service.istio.io/canonical-name", "Clash", "service.istio.io/canonical-revision", "v1beta"))),
"Clash-v1beta"
),
new Case(
"${pod.metadata.labels.app}",
ImmutableMap.of("service", service("Clash"), "pod", pod("app", "ClashX-alpha")),
"${pod.metadata.labels.(service.istio.io/canonical-name)}",
ImmutableMap.of("service", service("Clash"), "pod", pod(of("service.istio.io/canonical-name", "ClashX-alpha"))),
"ClashX-alpha"
)
};
@ -80,14 +81,14 @@ public class ServiceNameFormatterTest {
};
}
static V1Pod pod(final String label, final String value) {
static V1Pod pod(ImmutableMap<String, String> lb) {
return new V1Pod() {
@Override
public V1ObjectMeta getMetadata() {
return new V1ObjectMeta() {
@Override
public Map<String, String> getLabels() {
return ImmutableMap.of(label, value);
return lb;
}
};
}

View File

@ -51,22 +51,22 @@ public class FieldsHelperTest {
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{
"serviceName: ${LABELS.app}\nserviceInstanceName: ${NAME}",
"serviceName: ${LABELS.\"service.istio.io/canonical-name\"}\nserviceInstanceName: ${NAME}",
"productpage",
"productpage-v1-65576bb7bf-4mzsp"
},
{
"serviceName: ${LABELS.app}-${LABELS.version}\nserviceInstanceName: ${NAME}.${NAMESPACE}",
"serviceName: ${LABELS.\"service.istio.io/canonical-name\"}-${LABELS.version}\nserviceInstanceName: ${NAME}.${NAMESPACE}",
"productpage-v1",
"productpage-v1-65576bb7bf-4mzsp.default"
},
{
"serviceName: ${LABELS.app}-${CLUSTER_ID}\nserviceInstanceName: ${NAME}.${NAMESPACE}.${SERVICE_ACCOUNT}",
"serviceName: ${LABELS.\"service.istio.io/canonical-name\"}-${CLUSTER_ID}\nserviceInstanceName: ${NAME}.${NAMESPACE}.${SERVICE_ACCOUNT}",
"productpage-Kubernetes",
"productpage-v1-65576bb7bf-4mzsp.default.bookinfo-productpage"
},
{
"serviceName: fixed-${LABELS.app}\nserviceInstanceName: yeah_${NAME}",
"serviceName: fixed-${LABELS.\"service.istio.io/canonical-name\"}\nserviceInstanceName: yeah_${NAME}",
"fixed-productpage",
"yeah_productpage-v1-65576bb7bf-4mzsp"
}

View File

@ -13,5 +13,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.
serviceName: e2e::${LABELS.app}
serviceName: e2e::${LABELS."service.istio.io/canonical-name"}
serviceInstanceName: ${NAME}