-
Notifications
You must be signed in to change notification settings - Fork 100
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
Feature: collect and report logs #147
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8522eda
Point to v8.6.0 protocols
Superskyyy ecf5032
Feature - collect and report logs through gRPC
Superskyyy 55e5f4e
Merge branch 'master' into grpc-logging
Superskyyy 0267083
Fix circular import agent-sw_logging
Superskyyy 80ef678
Merge branch 'master' into grpc-logging
Superskyyy ca9d332
Fix - Remove wrongly named layout field
Superskyyy b3cfc03
Fix - Lint
Superskyyy 3091d62
Provides more type hints
Superskyyy 2cb7614
- Sync logReporter with latest changes.
Superskyyy 1319e0f
Doc - Add log-reporter-related EnvVars
Superskyyy 9521f47
Use latest skywalking-eyes to ignore .gitignore
Superskyyy 25ade5d
Fix - Add .gitignore to .licenserc.yaml
Superskyyy a71c6b8
- Move Formatter initialization to outer scope.
Superskyyy 407ceb9
New document - A complete guide to log reporter.
Superskyyy eb83f1f
Add a new section in README pointing to the log reporter guide.
Superskyyy File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,6 @@ header: | |
- 'LICENSE' | ||
- 'NOTICE' | ||
- '.github/PULL_REQUEST_TEMPLATE' | ||
- '.gitignore' | ||
|
||
comment: on-failure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Python agent gRPC log reporter | ||
|
||
This functionality reports logs collected from the Python logging module(in theory, also logging libraries depending on the core logging module). | ||
|
||
To utilize this feature, you will need to add some new configurations to the agent initialization step. | ||
|
||
## Enabling the feature | ||
```Python | ||
from skywalking import agent, config | ||
|
||
config.init(collector_address='127.0.0.1:11800', service_name='your awesome service', | ||
log_grpc_reporter_active=True, log_grpc_collector_address='127.0.0.1:11800') | ||
agent.start() | ||
``` | ||
|
||
`log_grpc_reporter_active=True` - Enables the log reporter. | ||
|
||
`log_grpc_collector_address` - For now, the log reporter uses a separate gRPC channel(will be merged upon the [SkyWalking Satellite Sidecar](https://github.com/apache/skywalking-satellite) project matures). | ||
If you would like to use the Satellite sidecar, you will need to configure an address pointing to its gatherer. Otherwise, you can simply keep the address the same as the OAP. | ||
|
||
`log_grpc_reporter_max_buffer_size` and `log_grpc_reporter_max_message_size` - Used to limit the reporting overhead. | ||
|
||
Alternatively, you can pass configurations through environment variables. | ||
Please refer to [EnvVars.md](EnvVars.md) for the list of environment variables associated with the log reporter. | ||
|
||
## Specify a logging level | ||
Only the logs with a level equal to or higher than the specified will be collected and reported. | ||
In other words, the agent ignores some unwanted logs based on your level threshold. | ||
|
||
`log_grpc_reporter_level` - The string name of a logger level. | ||
|
||
Note that it also works with your custom logger levels, simply specify its string name in the config. | ||
|
||
## Formatting | ||
Note that regardless of the formatting, Python agent will always report the following three tags - | ||
|
||
`level` - the logger level name | ||
|
||
`logger` - the logger name | ||
|
||
`thread` - the thread name | ||
### Customize the reported log format | ||
You can choose to report collected logs in a custom layout. | ||
|
||
If not set, the agent uses the layout below by default, else the agent uses your custom layout set in `log_grpc_reporter_layout`. | ||
|
||
`'%(asctime)s [%(threadName)s] %(levelname)s %(name)s - %(message)s'` | ||
|
||
If the layout is set to `None`, the reported log content will only contain the pre-formatted `LogRecord.message`(`msg % args`) without any additional styles and information. | ||
|
||
### Transmit un-formatted logs | ||
You can also choose to report the log messages without any formatting. | ||
It separates the raw log msg `logRecord.msg` and `logRecord.args`, then puts them into message content and tags starting from `argument.0`, respectively, along with an `exception` tag if an exception was raised. | ||
|
||
Note when you set `log_grpc_reporter_formatted` to False, it ignores your custom layout introduced above. | ||
|
||
As an example, the following code: | ||
```Python | ||
logger.info("SW test log %s %s %s", 'arg0', 'arg1', 'arg2') | ||
``` | ||
|
||
Will result in: | ||
```json | ||
{ | ||
"content": "SW test log %s %s %s", | ||
"tags": [ | ||
{ | ||
"key": "argument.0", | ||
"value": "arg0" | ||
}, | ||
{ | ||
"key": "argument.1", | ||
"value": "arg1" | ||
}, | ||
{ | ||
"key": "argument.2", | ||
"value": "arg2" | ||
} | ||
] | ||
} | ||
``` |
Submodule protocol
updated
31 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
import logging | ||
import traceback | ||
from queue import Queue, Empty | ||
from time import time | ||
|
||
import grpc | ||
|
||
from skywalking import config | ||
from skywalking.agent import Protocol | ||
from skywalking.agent.protocol.interceptors import header_adder_interceptor | ||
from skywalking.client.grpc import GrpcServiceManagementClient, GrpcLogDataReportService | ||
from skywalking.loggings import logger | ||
from skywalking.protocol.logging.Logging_pb2 import LogData | ||
|
||
|
||
class GrpcLogProtocol(Protocol): | ||
def __init__(self): | ||
self.properties_sent = False | ||
self.state = None | ||
|
||
if config.force_tls: | ||
self.channel = grpc.secure_channel(config.log_grpc_collector_address, grpc.ssl_channel_credentials(), | ||
options=(('grpc.max_send_message_length', | ||
config.log_grpc_reporter_max_message_size),)) | ||
else: | ||
self.channel = grpc.insecure_channel(config.log_grpc_collector_address, | ||
options=(('grpc.max_send_message_length', | ||
config.log_grpc_reporter_max_message_size),)) | ||
if config.authentication: | ||
self.channel = grpc.intercept_channel( | ||
self.channel, header_adder_interceptor('authentication', config.authentication) | ||
) | ||
|
||
self.channel.subscribe(self._cb, try_to_connect=True) | ||
self.service_management = GrpcServiceManagementClient(self.channel) | ||
self.log_reporter = GrpcLogDataReportService(self.channel) | ||
|
||
def _cb(self, state): | ||
logger.debug('grpc log reporter channel connectivity changed, [%s -> %s]', self.state, state) | ||
self.state = state | ||
|
||
def heartbeat(self): | ||
try: | ||
if not self.properties_sent: | ||
self.service_management.send_instance_props() | ||
self.properties_sent = True | ||
|
||
logger.debug( | ||
'log reporter service heart beats, [%s], [%s]', | ||
config.service_name, | ||
config.service_instance, | ||
) | ||
self.service_management.send_heart_beat() | ||
|
||
except grpc.RpcError: | ||
self.on_error() | ||
|
||
def on_error(self): | ||
traceback.print_exc() if logger.isEnabledFor(logging.DEBUG) else None | ||
self.channel.unsubscribe(self._cb) | ||
self.channel.subscribe(self._cb, try_to_connect=True) | ||
|
||
def report(self, queue: Queue, block: bool = True): | ||
start = time() | ||
|
||
def generator(): | ||
while True: | ||
try: | ||
timeout = config.QUEUE_TIMEOUT - int(time() - start) # type: int | ||
if timeout <= 0: # this is to make sure we exit eventually instead of being fed continuously | ||
return | ||
log_data = queue.get(block=block, timeout=timeout) # type: LogData | ||
except Empty: | ||
return | ||
|
||
queue.task_done() | ||
|
||
logger.debug('Reporting Log') | ||
|
||
yield log_data | ||
|
||
try: | ||
self.log_reporter.report(generator()) | ||
|
||
except grpc.RpcError: | ||
self.on_error() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides this doc change, let's add a specific doc to show users how to use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wu-sheng I just added a detailed guide, please see if that is sufficient.