DefaultScopeDefine.java 16.3 KB
Newer Older
wu-sheng's avatar
wu-sheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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.skywalking.oap.server.core.source;

import java.lang.annotation.Annotation;
wu-sheng's avatar
wu-sheng 已提交
22 23 24 25 26
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
27
import lombok.Setter;
wu-sheng's avatar
wu-sheng 已提交
28 29 30 31 32 33
import org.apache.skywalking.oap.server.core.UnexpectedException;
import org.apache.skywalking.oap.server.core.annotation.AnnotationListener;

public class DefaultScopeDefine {
    private static final Map<String, Integer> NAME_2_ID = new HashMap<>();
    private static final Map<Integer, String> ID_2_NAME = new HashMap<>();
wu-sheng's avatar
wu-sheng 已提交
34
    private static final Map<String, List<ScopeDefaultColumn>> SCOPE_COLUMNS = new HashMap<>();
wu-sheng's avatar
wu-sheng 已提交
35

36
    /**
37
     * All metrics IDs in [0, 10,000) are reserved in Apache SkyWalking.
38
     * <p>
39 40
     * If you want to extend the scope, recommend to start with 10,000.
     */
41 42 43 44 45

    /**
     * @since 9.0.0
     */
    public static final int UNKNOWN = 0;
46 47 48 49
    /**
     * @since Deprecated from 9.0.0
     */
    @Deprecated
wu-sheng's avatar
wu-sheng 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    public static final int ALL = 0;
    public static final int SERVICE = 1;
    public static final int SERVICE_INSTANCE = 2;
    public static final int ENDPOINT = 3;
    public static final int SERVICE_RELATION = 4;
    public static final int SERVICE_INSTANCE_RELATION = 5;
    public static final int ENDPOINT_RELATION = 6;
    public static final int SERVICE_INSTANCE_JVM_CPU = 8;
    public static final int SERVICE_INSTANCE_JVM_MEMORY = 9;
    public static final int SERVICE_INSTANCE_JVM_MEMORY_POOL = 10;
    public static final int SERVICE_INSTANCE_JVM_GC = 11;
    public static final int SEGMENT = 12;
    public static final int ALARM = 13;
    public static final int DATABASE_ACCESS = 17;
    public static final int DATABASE_SLOW_STATEMENT = 18;
65 66 67
    public static final int SERVICE_INSTANCE_CLR_CPU = 19;
    public static final int SERVICE_INSTANCE_CLR_GC = 20;
    public static final int SERVICE_INSTANCE_CLR_THREAD = 21;
68
    public static final int ENVOY_INSTANCE_METRIC = 22;
69
    public static final int ZIPKIN_SPAN = 23;
70
    @Deprecated
wu-sheng's avatar
wu-sheng 已提交
71
    public static final int JAEGER_SPAN = 24;
72
    @Deprecated
73
    public static final int HTTP_ACCESS_LOG = 25;
74
    public static final int PROFILE_TASK = 26;
75
    public static final int PROFILE_TASK_LOG = 27;
76
    public static final int PROFILE_TASK_SEGMENT_SNAPSHOT = 28;
77
    public static final int SERVICE_META = 29;
78 79
    public static final int SERVICE_INSTANCE_UPDATE = 30;
    public static final int NETWORK_ADDRESS_ALIAS = 31;
80
    public static final int UI_TEMPLATE = 32;
Stalary's avatar
Stalary 已提交
81
    public static final int SERVICE_INSTANCE_JVM_THREAD = 33;
wu-sheng's avatar
wu-sheng 已提交
82

83 84 85 86 87 88 89 90 91
    // browser
    public static final int BROWSER_ERROR_LOG = 34;
    public static final int BROWSER_APP_PERF = 35;
    public static final int BROWSER_APP_PAGE_PERF = 36;
    public static final int BROWSER_APP_SINGLE_VERSION_PERF = 37;
    public static final int BROWSER_APP_TRAFFIC = 38;
    public static final int BROWSER_APP_SINGLE_VERSION_TRAFFIC = 39;
    public static final int BROWSER_APP_PAGE_TRAFFIC = 40;

92 93 94
    public static final int LOG = 41;
    public static final int ENDPOINT_META = 42;

95 96
    public static final int EVENT = 43;

97 98
    public static final int SERVICE_INSTANCE_JVM_CLASS = 44;

99
    public static final int PROCESS = 45;
100 101 102
    public static final int EBPF_PROFILING_TASK = 46;
    public static final int EBPF_PROFILING_SCHEDULE = 47;
    public static final int EBPF_PROFILING_DATA = 48;
103
    public static final int SERVICE_LABEL = 49;
104
    public static final int TAG_AUTOCOMPLETE = 50;
105 106 107
    public static final int ZIPKIN_SERVICE = 51;
    public static final int ZIPKIN_SERVICE_SPAN = 52;
    public static final int ZIPKIN_SERVICE_RELATION = 53;
108
    public static final int PROCESS_RELATION = 54;
109 110
    public static final int CACHE_ACCESS = 55;
    public static final int CACHE_SLOW_ACCESS = 56;
111

112 113 114 115 116
    public static final int TCP_SERVICE = 57;
    public static final int TCP_SERVICE_INSTANCE = 58;
    public static final int TCP_SERVICE_RELATION = 59;
    public static final int TCP_SERVICE_INSTANCE_RELATION = 60;
    public static final int TCP_SERVICE_INSTANCE_UPDATE = 61;
117
    public static final int SAMPLED_SLOW_TRACE = 62;
118

119
    /**
wu-sheng's avatar
wu-sheng 已提交
120
     * Catalog of scope, the metrics processor could use this to group all generated metrics by oal rt.
121 122 123 124
     */
    public static final String SERVICE_CATALOG_NAME = "SERVICE";
    public static final String SERVICE_INSTANCE_CATALOG_NAME = "SERVICE_INSTANCE";
    public static final String ENDPOINT_CATALOG_NAME = "ENDPOINT";
125 126 127
    public static final String SERVICE_RELATION_CATALOG_NAME = "SERVICE_RELATION";
    public static final String SERVICE_INSTANCE_RELATION_CATALOG_NAME = "SERVICE_INSTANCE_RELATION";
    public static final String ENDPOINT_RELATION_CATALOG_NAME = "ENDPOINT_RELATION";
128
    public static final String PROCESS_CATALOG_NAME = "PROCESS";
129
    public static final String PROCESS_RELATION_CATALOG_NAME = "PROCESS_RELATION";
130 131 132 133

    private static final Map<Integer, Boolean> SERVICE_CATALOG = new HashMap<>();
    private static final Map<Integer, Boolean> SERVICE_INSTANCE_CATALOG = new HashMap<>();
    private static final Map<Integer, Boolean> ENDPOINT_CATALOG = new HashMap<>();
134 135 136
    private static final Map<Integer, Boolean> SERVICE_RELATION_CATALOG = new HashMap<>();
    private static final Map<Integer, Boolean> SERVICE_INSTANCE_RELATION_CATALOG = new HashMap<>();
    private static final Map<Integer, Boolean> ENDPOINT_RELATION_CATALOG = new HashMap<>();
137
    private static final Map<Integer, Boolean> PROCESS_CATALOG = new HashMap<>();
138
    private static final Map<Integer, Boolean> PROCESS_RELATION_CATALOG = new HashMap<>();
139

140 141 142 143 144 145 146
    @Setter
    private static boolean ACTIVE_EXTRA_MODEL_COLUMNS = false;

    public static void activeExtraModelColumns() {
        ACTIVE_EXTRA_MODEL_COLUMNS = true;
    }

wu-sheng's avatar
wu-sheng 已提交
147 148 149
    /**
     * Annotation scan listener
     */
wu-sheng's avatar
wu-sheng 已提交
150
    public static class Listener implements AnnotationListener {
151 152
        @Override
        public Class<? extends Annotation> annotation() {
wu-sheng's avatar
wu-sheng 已提交
153 154 155
            return ScopeDeclaration.class;
        }

156 157 158
        @Override
        public void notify(Class originalClass) {
            ScopeDeclaration declaration = (ScopeDeclaration) originalClass.getAnnotation(ScopeDeclaration.class);
wu-sheng's avatar
wu-sheng 已提交
159 160 161 162 163 164
            if (declaration != null) {
                addNewScope(declaration, originalClass);
            }
        }
    }

wu-sheng's avatar
wu-sheng 已提交
165 166 167
    /**
     * Add a new scope based on the scan result
     *
168
     * @param declaration   includes the definition.
wu-sheng's avatar
wu-sheng 已提交
169 170 171
     * @param originalClass represents the class having the {@link ScopeDeclaration} annotation
     */
    private static final void addNewScope(ScopeDeclaration declaration, Class originalClass) {
wu-sheng's avatar
wu-sheng 已提交
172 173
        int id = declaration.id();
        if (ID_2_NAME.containsKey(id)) {
wu-sheng's avatar
wu-sheng 已提交
174 175 176
            throw new UnexpectedException(
                "ScopeDeclaration id=" + id + " at " + originalClass.getName() + " has conflict with another named " + ID_2_NAME
                    .get(id));
wu-sheng's avatar
wu-sheng 已提交
177
        }
178 179 180 181
        if (id < 0) {
            throw new UnexpectedException(
                "ScopeDeclaration id=" + id + " at " + originalClass.getName() + " is negative. ");
        }
wu-sheng's avatar
wu-sheng 已提交
182 183
        String name = declaration.name();
        if (NAME_2_ID.containsKey(name)) {
wu-sheng's avatar
wu-sheng 已提交
184 185 186
            throw new UnexpectedException(
                "ScopeDeclaration fieldName=" + name + " at " + originalClass.getName() + " has conflict with another id= " + NAME_2_ID
                    .get(name));
wu-sheng's avatar
wu-sheng 已提交
187
        }
188

wu-sheng's avatar
wu-sheng 已提交
189 190
        ID_2_NAME.put(id, name);
        NAME_2_ID.put(name, id);
191

wu-sheng's avatar
wu-sheng 已提交
192 193
        List<ScopeDefaultColumn> scopeDefaultColumns = new ArrayList<>();

194 195
        ScopeDefaultColumn.VirtualColumnDefinition virtualColumn = (ScopeDefaultColumn.VirtualColumnDefinition) originalClass
            .getAnnotation(ScopeDefaultColumn.VirtualColumnDefinition.class);
wu-sheng's avatar
wu-sheng 已提交
196
        if (virtualColumn != null) {
wu-sheng's avatar
wu-sheng 已提交
197 198
            scopeDefaultColumns.add(
                new ScopeDefaultColumn(virtualColumn.fieldName(), virtualColumn.columnName(), virtualColumn
199
                    .type(), virtualColumn.isID(), virtualColumn.length()));
wu-sheng's avatar
wu-sheng 已提交
200 201 202 203
        }
        Field[] scopeClassField = originalClass.getDeclaredFields();
        if (scopeClassField != null) {
            for (Field field : scopeClassField) {
wu-sheng's avatar
wu-sheng 已提交
204 205
                ScopeDefaultColumn.DefinedByField definedByField = field.getAnnotation(
                    ScopeDefaultColumn.DefinedByField.class);
wu-sheng's avatar
wu-sheng 已提交
206
                if (definedByField != null) {
207 208
                    if (!definedByField.requireDynamicActive() || ACTIVE_EXTRA_MODEL_COLUMNS) {
                        scopeDefaultColumns.add(
209 210 211
                            new ScopeDefaultColumn(
                                field.getName(), definedByField.columnName(), field.getType(), false,
                                definedByField.length()
212 213
                            ));
                    }
wu-sheng's avatar
wu-sheng 已提交
214 215 216 217 218 219
                }
            }
        }

        SCOPE_COLUMNS.put(name, scopeDefaultColumns);

220 221 222 223 224 225 226 227 228 229 230
        String catalogName = declaration.catalog();
        switch (catalogName) {
            case SERVICE_CATALOG_NAME:
                SERVICE_CATALOG.put(id, Boolean.TRUE);
                break;
            case SERVICE_INSTANCE_CATALOG_NAME:
                SERVICE_INSTANCE_CATALOG.put(id, Boolean.TRUE);
                break;
            case ENDPOINT_CATALOG_NAME:
                ENDPOINT_CATALOG.put(id, Boolean.TRUE);
                break;
231 232 233 234 235 236 237 238 239
            case SERVICE_RELATION_CATALOG_NAME:
                SERVICE_RELATION_CATALOG.put(id, Boolean.TRUE);
                break;
            case SERVICE_INSTANCE_RELATION_CATALOG_NAME:
                SERVICE_INSTANCE_RELATION_CATALOG.put(id, Boolean.TRUE);
                break;
            case ENDPOINT_RELATION_CATALOG_NAME:
                ENDPOINT_RELATION_CATALOG.put(id, Boolean.TRUE);
                break;
240 241 242
            case PROCESS_CATALOG_NAME:
                PROCESS_CATALOG.put(id, Boolean.TRUE);
                break;
243 244 245
            case PROCESS_RELATION_CATALOG_NAME:
                PROCESS_RELATION_CATALOG.put(id, Boolean.TRUE);
                break;
246
        }
wu-sheng's avatar
wu-sheng 已提交
247 248
    }

wu-sheng's avatar
wu-sheng 已提交
249 250 251 252 253 254
    /**
     * Fetch the name from given id
     *
     * @param id represents an existing scope id.
     * @return scope name.
     */
wu-sheng's avatar
wu-sheng 已提交
255 256 257 258 259 260 261 262
    public static String nameOf(int id) {
        String name = ID_2_NAME.get(id);
        if (name == null) {
            throw new UnexpectedException("ScopeDefine id = " + id + " not found.");
        }
        return name;
    }

wu-sheng's avatar
wu-sheng 已提交
263 264 265 266 267 268
    /**
     * Fetch the id of given name
     *
     * @param name represents an existing scope name
     * @return scope id
     */
wu-sheng's avatar
wu-sheng 已提交
269 270 271
    public static int valueOf(String name) {
        Integer id = NAME_2_ID.get(name);
        if (id == null) {
wu-sheng's avatar
wu-sheng 已提交
272
            throw new UnexpectedException("ScopeDefine fieldName = " + name + " not found.");
wu-sheng's avatar
wu-sheng 已提交
273 274 275 276
        }
        return id;
    }

wu-sheng's avatar
wu-sheng 已提交
277 278 279
    /**
     * Reset all existing scope definitions. For test only.
     */
wu-sheng's avatar
wu-sheng 已提交
280 281 282
    public static void reset() {
        NAME_2_ID.clear();
        ID_2_NAME.clear();
wu-sheng's avatar
wu-sheng 已提交
283
        SCOPE_COLUMNS.clear();
wu-sheng's avatar
wu-sheng 已提交
284
    }
285

wu-sheng's avatar
wu-sheng 已提交
286
    /**
287
     * Check whether the given scope ID belongs service catalog
wu-sheng's avatar
wu-sheng 已提交
288 289 290 291
     *
     * @param scopeId represents an existing scope id.
     * @return true is current scope set {@link ScopeDeclaration#catalog()} == {@link #SERVICE_CATALOG_NAME}
     */
292 293 294 295
    public static boolean inServiceCatalog(int scopeId) {
        return SERVICE_CATALOG.containsKey(scopeId);
    }

wu-sheng's avatar
wu-sheng 已提交
296
    /**
297
     * Check whether the given scope ID belongs service instance catalog
wu-sheng's avatar
wu-sheng 已提交
298 299 300 301
     *
     * @param scopeId represents an existing scope id.
     * @return true is current scope set {@link ScopeDeclaration#catalog()} == {@link #SERVICE_INSTANCE_CATALOG_NAME}
     */
302 303 304 305
    public static boolean inServiceInstanceCatalog(int scopeId) {
        return SERVICE_INSTANCE_CATALOG.containsKey(scopeId);
    }

wu-sheng's avatar
wu-sheng 已提交
306
    /**
307
     * Check whether the given scope ID belongs endpoint catalog
wu-sheng's avatar
wu-sheng 已提交
308 309 310 311
     *
     * @param scopeId represents an existing scope id.
     * @return true is current scope set {@link ScopeDeclaration#catalog()} == {@link #ENDPOINT_CATALOG_NAME}
     */
312 313 314
    public static boolean inEndpointCatalog(int scopeId) {
        return ENDPOINT_CATALOG.containsKey(scopeId);
    }
wu-sheng's avatar
wu-sheng 已提交
315

316
    /**
317
     * Check whether the given scope ID belongs service relation catalog
318 319 320 321 322 323 324 325 326
     *
     * @param scopeId represents an existing scope id.
     * @return true is current scope set {@link ScopeDeclaration#catalog()} == {@link #SERVICE_RELATION_CATALOG_NAME}
     */
    public static boolean inServiceRelationCatalog(int scopeId) {
        return SERVICE_RELATION_CATALOG.containsKey(scopeId);
    }

    /**
327
     * Check whether the given scope ID belongs service instance relation catalog
328 329 330 331 332 333 334 335 336
     *
     * @param scopeId represents an existing scope id.
     * @return true is current scope set {@link ScopeDeclaration#catalog()} == {@link #SERVICE_INSTANCE_RELATION_CATALOG_NAME}
     */
    public static boolean inServiceInstanceRelationCatalog(int scopeId) {
        return SERVICE_INSTANCE_RELATION_CATALOG.containsKey(scopeId);
    }

    /**
337
     * Check whether the given scope ID belongs endpoint relation catalog
338 339 340 341 342 343 344 345
     *
     * @param scopeId represents an existing scope id.
     * @return true is current scope set {@link ScopeDeclaration#catalog()} == {@link #ENDPOINT_RELATION_CATALOG_NAME}
     */
    public static boolean inEndpointRelationCatalog(int scopeId) {
        return ENDPOINT_RELATION_CATALOG.containsKey(scopeId);
    }

346 347 348 349 350 351 352 353 354 355
    /**
     * Check whether the given scope ID belongs process catalog
     *
     * @param scopeId represents an existing scope id.
     * @return true is current scope set {@link ScopeDeclaration#catalog()} == {@link #PROCESS_CATALOG_NAME}
     */
    public static boolean inProcessCatalog(int scopeId) {
        return PROCESS_CATALOG.containsKey(scopeId);
    }

356 357 358 359 360 361 362 363 364 365
    /**
     * Check whether the given scope ID belongs process relation catalog
     *
     * @param scopeId represents an existing scope id.
     * @return true is current scope set {@link ScopeDeclaration#catalog()} == {@link #PROCESS_RELATION_CATALOG_NAME}
     */
    public static boolean inProcessRelationCatalog(int scopeId) {
        return PROCESS_RELATION_CATALOG.containsKey(scopeId);
    }

366 367 368 369
    /**
     * Get the catalog string name of the given scope
     *
     * @param scope id of the source scope.
370
     * @return literal string name of the catalog owning the scope. Return `ALL` by default.
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
     */
    public static String catalogOf(int scope) {
        if (inServiceCatalog(scope)) {
            return SERVICE_CATALOG_NAME;
        }
        if (inServiceInstanceCatalog(scope)) {
            return SERVICE_INSTANCE_CATALOG_NAME;
        }
        if (inEndpointCatalog(scope)) {
            return ENDPOINT_CATALOG_NAME;
        }
        if (inServiceRelationCatalog(scope)) {
            return SERVICE_RELATION_CATALOG_NAME;
        }
        if (inServiceInstanceRelationCatalog(scope)) {
            return SERVICE_INSTANCE_RELATION_CATALOG_NAME;
        }
        if (inEndpointRelationCatalog(scope)) {
            return ENDPOINT_RELATION_CATALOG_NAME;
        }
391 392 393
        if (inProcessCatalog(scope)) {
            return PROCESS_CATALOG_NAME;
        }
394 395 396
        if (inProcessRelationCatalog(scope)) {
            return PROCESS_RELATION_CATALOG_NAME;
        }
397
        return "ALL";
398 399
    }

wu-sheng's avatar
wu-sheng 已提交
400 401 402 403 404 405 406 407 408 409 410 411
    /**
     * Get the default columns defined in Scope. All those columns will forward to persistent entity.
     *
     * @param scopeName of the default columns
     */
    public static List<ScopeDefaultColumn> getDefaultColumns(String scopeName) {
        List<ScopeDefaultColumn> scopeDefaultColumns = SCOPE_COLUMNS.get(scopeName);
        if (scopeDefaultColumns == null) {
            throw new UnexpectedException("ScopeDefine name = " + scopeName + " not found.");
        }
        return scopeDefaultColumns;
    }
412
}