Data scope differences between query and analysis and stream processing
When you use Alibaba Cloud Log Service (SLS) for both query and analysis and stream processing, you may encounter data scope differences. You can resolve these issues by using two timestamps: __time__ and __receive_time__.
Symptoms
When you use Log Service for query and analysis, you may encounter timestamp discrepancies after data is synchronized from a source to a destination. For example:
-
After data from a week ago is synchronized from SLS to MaxCompute, the data counts for the same time interval are inconsistent.
-
After data is synchronized from SLS to another Logstore using data transformation, data is missing from the specified time interval in the destination.
Possible causes
-
In query and analysis mode, log data is processed in batches based on a specific time window or data volume. Data is collected and aggregated before analysis, allowing for complex queries and calculations.
-
In stream processing mode, log data arrives as a continuous stream and is processed immediately. Downstream streaming jobs filter and process data based on the
__receive_time__field. This enables real-time data processing for immediate responses.
As shown in the following figure, for the same time interval from 00:00 to 18:00 on May 24, 2023, stream processing includes the green data block, but query and analysis does not. This occurs because query and analysis and stream processing use different data scopes. As a result, applying the same time interval to both modes yields inconsistent results.

Solution
Timestamps in SLS data
-
__time__(log time): This timestamp indicates when a log was sent or when the event occurred, such as the time a user visited a webpage or a product was sold. In SLS, there are two primary ways to set this value:-
Logtail: Logtail is a lightweight agent that Log Service provides for log collection. When Logtail collects logs, it automatically adds a creation timestamp. If this timestamp differs from your application's event time, you can configure a time zone offset in Logtail. You can also use a plugin to extract a time field from the log entry to populate the
__time__field. For example, a transaction log might be created after the event based on a database record, but your business requires thecreate_timefield to be the log time. -
API/SDK: When writing logs to Log Service using an API or SDK, you can explicitly specify the
__time__field. If not specified, it defaults to the time the log is sent.
-
-
__receive_time__(arrival time): This timestamp indicates when a log is received by the Log Service server. In Log Service, arrival time is a system tag and cannot be modified. If you enable the Manage Logstore feature when you create a Logstore, the server automatically adds this timestamp as a tag to the log upon receipt. This timestamp is generally unrelated to business logic. With minimal pipeline delay, the arrival time can approximate the event time.

When a log entry arrives at Log Service, the service calculates the difference between the log time (the __time__ field) and the arrival time (the __tag__:__receive_time__ field). Based on this difference, Log Service classifies the log into one of two categories:
-
If the difference is within the range of
(-180, 900]seconds, the data is considered real-time data. This means the log data is immediately available for use and analysis, which is suitable for real-time monitoring and rapid response scenarios. -
If the difference is within the range of [-7*86400, -180] seconds, the data is considered historical data. This data is suitable for scenarios such as data backfilling, performance analysis, and long-term trend analysis.
SLS use cases
These two timestamps support different operations and analyses in Log Service, enabling two distinct use cases:
-
Query and analysis
In common business scenarios, you can use the __time__ field to query and sort logs by log time, which allows you to retrieve and analyze log data within a specific time range.
-
Stream processing
You can use the __receive_time__ field for stream consumption, such as in data transformation, shipping, consumption, and computation. This field also helps you measure log ingestion latency and assess data timeliness.
Using both timestamps provides the flexibility to analyze log data for diverse application requirements. They also simplify operations such as real-time monitoring, troubleshooting, and performance analysis in Log Service.
Solution examples
The solutions to the following two problems are provided in both SQL and SPL. For more information about SPL, see SPL syntax.
-
How to use stream consumption to count log entries that fall within a
__time__window.Assume you query the SLS Query and Analysis page for data from 00:00 to 18:00 on May 23, 2023, and get N log entries. Assuming a 1-minute data ingestion delay, you can use
__receive_time__in a stream processing job to retrieve the same N entries.-
SQL
* | select * from log where __time__ >= to_unixtime('2023-05-23 00:00:00') and __time__ < to_unixtime('2023-05-23 18:00:00') -
SPL
* | where "__tag__:__receive_time__" >= to_unixtime('2023-05-23 00:01:00') and "__tag__:__receive_time__" < to_unixtime('2023-05-23 18:01:00')
-
-
How to use query and analysis to count log entries that fall within a
__receive_time__window.Suppose a job on the SLS Data Transformation page processes data received from 00:00 to 18:00 on May 23, 2023, resulting in N log entries. If you assume a 1-minute delay for data to arrive, you can use
__time__in a query to get the same N log entries.-
SQL
* | select * from log where __time__ >= to_unixtime('2023-05-22 23:59:00') and __time__ < to_unixtime('2023-05-23 17:59:00') -
SPL
* | where "__tag__:__receive_time__" >= to_unixtime('2023-05-23 00:00:00') and "__tag__:__receive_time__" < to_unixtime('2023-05-23 18:00:00')
-