Hi, I made my fastapi app to log data into json fo...
# support
m
Hi, I made my fastapi app to log data into json format also I added
span_id
and
trace_id
in my log (type is
int
) but logs are not shown in signoz here is my code
Copy code
import json_logging
import logging
import sys
from config import config
from fastapi import FastAPI

app = FastAPI(title=config.app_title)


class JSONLogWebFormatter(json_logging.JSONLogFormatter):
    """
    Formatter for web application log
    """

    def _format_log_object(self, record, request_util):
        json_log_object = super(JSONLogWebFormatter, self)._format_log_object(
            record, request_util
        )
        current_span = trace.get_current_span()
        if current_span:
            json_log_object["span_id"] = current_span.get_span_context().span_id
            json_log_object["trace_id"] = current_span.get_span_context().trace_id
        json_log_object.update(
            {
                "correlation_id": request_util.get_correlation_id(
                    within_formatter=True
                ),
            }
        )
        return json_log_object


json_logging.init_fastapi(enable_json=True, custom_formatter=JSONLogWebFormatter)
json_logging.init_request_instrument(app, custom_formatter=JSONLogWebFormatter)

# init the logger as usual
logger = logging.getLogger()
logger.setLevel(logging._nameToLevel[config.log_level.upper()])
logger.addHandler(logging.StreamHandler(sys.stdout))
n
Or If your application is running in docker or k8s, then logs emitted to stdout will be collected by signoz
m
I'm in k8s and other logs are collecting except this app
I think something are wrong with my log structure
Or with my configuration
@nitya-signoz What can I do to fix that?
n
If you logs are getting printed to stdout then it will be collected, did you try to filter in the UI
m
yes
nothing shows in the SigNoz dashboard after filtering
Copy code
{
  "written_at": "2023-03-01T15:56:00.206Z",
  "written_ts": 1677686160206573000,
  "msg": "",
  "type": "log",
  "logger": "fastapi-request-logger",
  "thread": "MainThread",
  "level": "INFO",
  "module": "implementation",
  "line_no": 35,
  "span_id": "176794658234006051",
  "trace_id": "227587342002673840811008499138396058579",
  "correlation_id": "272ba70209901ac67929a2595244ce13"
}
here is the sample log
@nitya-signoz
I have removed trace_id and span_id from logs but I'm not able to see logs in signoz
@nitya-signoz
n
Can you check if by any change if the namespace where you have deployed the above application is excluded in the otel collector config file receiver ?
m
Copy code
- type: filter
              id: remove_some_logs
              expr: 'resource["k8s.namespace.name"] matches "(^stage-.*$)" and resource["k8s.namespace.name"] != "stage-this-project"'
              drop_ratio: 1.0
stage-this-project is the name of the namespace
I think this filter is not making problems
n
It shouldn’t cause any issue, but try removing it?
or if there is something else.
m
I will check But there are no other drop filters If I remove that my storage will become 100% full
n
Got it then try deploying in some other namesapce ?
m
I will test that
I changed the namespace but problem persist
@nitya-signoz