meta_parser.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import logging
  2. from datetime import datetime as dt
  3. import os
  4. import json
  5. from oteldatareplay.record_reader import RecordReader
  6. dt_format = '%Y-%m-%d %H:%M:%S.%f'
  7. logger = logging.getLogger(__name__)
  8. class MetaParser:
  9. def __init__(self, reader):
  10. self.trace_id2new_id = None
  11. self.first_record_time = None
  12. self.span_id2new_id = None
  13. self.record_reader: RecordReader = reader
  14. self.meta = {}
  15. def parse_meta(self):
  16. data_dir = os.path.dirname(self.record_reader.file_path)
  17. meta_path = os.path.join(data_dir, 'meta.json')
  18. if os.path.exists(meta_path):
  19. logger.info(f'read meta from {meta_path}')
  20. with open(meta_path, 'r') as f:
  21. meta = json.load(f)
  22. self.first_record_time = dt.strptime(meta['first_record_time'], dt_format)
  23. self.span_id2new_id = {k: None for k in meta['span_id2new_id']}
  24. self.trace_id2new_id = {k: None for k in meta['trace_id2new_id']}
  25. return
  26. logger.info(f'no meta file {meta_path} exists, need parse')
  27. first_record = next(iter(self.record_reader.records_generator()))
  28. self.first_record_time = first_record.first_span_start_time
  29. self.record_reader.reset()
  30. span_ids = []
  31. trace_ids = []
  32. for record in self.record_reader.records_generator():
  33. span_ids += record.span_ids
  34. trace_ids += trace_ids
  35. self.span_id2new_id = {tid: None for tid in trace_ids}
  36. self.trace_id2new_id = {sid: None for sid in span_ids}
  37. logger.info(f'meta parse done, write meta to {meta_path}')
  38. meta = {'first_record_time': self.first_record_time.strftime(dt_format),
  39. 'span_id2new_id': [k for k in self.span_id2new_id.keys()],
  40. 'trace_id2new_id': [k for k in self.trace_id2new_id.keys()]}
  41. with open(meta_path, 'w') as f:
  42. json.dump(meta, f)
  43. self.record_reader.reset()