source-extension.md 3.4 KB
Newer Older
W
Wing 已提交
1 2 3
# Source and scope extension for new metrics
From the [OAL scope introduction](../concepts-and-designs/oal.md#scope), you should already have understood what a scope is.
If you would like to create more extensions, you need to have a deeper understanding of what a **source** is. 
wu-sheng's avatar
wu-sheng 已提交
4

W
Wing 已提交
5 6
**Source** and **scope** are interrelated concepts. **Scope** declares the ID (int) and name, while **source** declares the attributes.
Follow these steps to create a new source and sccope.
wu-sheng's avatar
wu-sheng 已提交
7

W
Wing 已提交
8
1. In the OAP core module, it provides **SourceReceiver** internal services.
wu-sheng's avatar
wu-sheng 已提交
9 10 11 12 13 14
```java
public interface SourceReceiver extends Service {
    void receive(Source source);
}
```

W
Wing 已提交
15 16
2. All data of the analysis must be a **org.apache.skywalking.oap.server.core.source.Source** sub class that is
tagged by `@SourceType` annotation, and included in the `org.apache.skywalking` package. Then, it can be supported by the OAL script and OAP core.
wu-sheng's avatar
wu-sheng 已提交
17

W
Wing 已提交
18
Take the existing source **service** as an example.
wu-sheng's avatar
wu-sheng 已提交
19
```java
wu-sheng's avatar
wu-sheng 已提交
20 21 22
@ScopeDeclaration(id = SERVICE_INSTANCE, name = "ServiceInstance", catalog = SERVICE_INSTANCE_CATALOG_NAME)
@ScopeDefaultColumn.VirtualColumnDefinition(fieldName = "entityId", columnName = "entity_id", isID = true, type = String.class)
public class ServiceInstance extends Source {
wu-sheng's avatar
wu-sheng 已提交
23
    @Override public int scope() {
wu-sheng's avatar
wu-sheng 已提交
24
        return DefaultScopeDefine.SERVICE_INSTANCE;
wu-sheng's avatar
wu-sheng 已提交
25 26 27 28 29 30 31
    }

    @Override public String getEntityId() {
        return String.valueOf(id);
    }

    @Getter @Setter private int id;
wu-sheng's avatar
wu-sheng 已提交
32
    @Getter @Setter @ScopeDefaultColumn.DefinedByField(columnName = "service_id") private int serviceId;
wu-sheng's avatar
wu-sheng 已提交
33
    @Getter @Setter private String name;
wu-sheng's avatar
wu-sheng 已提交
34
    @Getter @Setter private String serviceName;
wu-sheng's avatar
wu-sheng 已提交
35 36 37 38 39 40 41 42
    @Getter @Setter private String endpointName;
    @Getter @Setter private int latency;
    @Getter @Setter private boolean status;
    @Getter @Setter private int responseCode;
    @Getter @Setter private RequestType type;
}
```

W
Wing 已提交
43
3. The `scope()` method in source returns an ID, which is not a random value. This ID must be declared through the `@ScopeDeclaration` annotation too. The ID in `@ScopeDeclaration` and ID in `scope()` method must be the same for this source.
wu-sheng's avatar
wu-sheng 已提交
44

W
Wing 已提交
45 46
4. The `String getEntityId()` method in source requests the return value representing the unique entity to which the scope relates. For example, in this service scope, the ID is the service ID, which represents a particular service, like the `Order` service.
This value is used in the [OAL group mechanism](../concepts-and-designs/oal.md#group).
wu-sheng's avatar
wu-sheng 已提交
47

W
Wing 已提交
48 49 50
5. `@ScopeDefaultColumn.VirtualColumnDefinition` and `@ScopeDefaultColumn.DefinedByField` are required. All declared fields (virtual/byField) will be pushed into a persistent entity, and maps to lists such as the ElasticSearch index and Database table column.
For example, the entity ID and service ID for endpoint and service instance level scope are usually included. Take a reference from all existing scopes.
All these fields are detected by OAL Runtime, and are required during query.
wu-sheng's avatar
wu-sheng 已提交
51

W
Wing 已提交
52
6. Add scope name as keyword to OAL grammar definition file, `OALLexer.g4`, which is at the `antlr4` folder of the `generate-tool-grammar` module.
wu-sheng's avatar
wu-sheng 已提交
53

W
Wing 已提交
54
7. Add scope name as keyword to the parser definition file, `OALParser.g4`, which is located in the same folder as `OALLexer.g4`.
wu-sheng's avatar
wu-sheng 已提交
55 56 57


___
W
Wing 已提交
58 59 60 61 62
After finishing these steps, you could build a receiver, which do
1. Obtain the original data of the metrics.
1. Build the source, and send to `SourceReceiver`.
1. Complete your OAL scripts.
1. Repackage the project.