Created
June 9, 2020 14:41
-
-
Save iamazeem/a8a24092132e1741a76956192f2104cc to your computer and use it in GitHub Desktop.
ProtoBuf Schema and Ruby Script for Generating Messages Samples (Binary and JSON)
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
syntax = "proto3"; | |
package service.logging; | |
import "google/protobuf/timestamp.proto"; | |
message Log { | |
message Context { | |
google.protobuf.Timestamp timestamp = 1; | |
string host_or_ip = 2; | |
string service_name = 3; | |
string user = 4; | |
} | |
enum Level { | |
DEBUG = 0; | |
INFO = 1; | |
WARN = 2; | |
ERROR = 3; | |
FATAL = 4; | |
} | |
Context context = 1; | |
Level level = 2; | |
string message = 3; | |
} | |
message Batch { | |
string type = 1; | |
repeated Log batch = 2; | |
} |
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
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require_relative 'log_pb' | |
require 'google/protobuf/well_known_types' # Google::Protobuf::Timestamp | |
require 'json' | |
def serialized_log | |
log = Service::Logging::Log.new | |
log.context = Service::Logging::Log::Context.new( | |
timestamp: Google::Protobuf::Timestamp.new(seconds: Time.now.to_i, nanos: 0), | |
host_or_ip: '192.168.xxx.xxx', | |
service_name: 'test', | |
user: 'test' | |
) | |
log.level = Service::Logging::Log::Level::INFO | |
log.message = "This is a test log generated by [#{$PROGRAM_NAME}]." | |
log | |
end | |
def serailized_batch(count, to_json) | |
batch = Service::Logging::Batch.new | |
batch.type = 'service.logging.Log' | |
count.times do | |
batch.batch.push(serialized_log) | |
end | |
if to_json == false | |
Service::Logging::Batch.encode(batch) | |
else | |
Service::Logging::Batch.encode_json(batch) | |
end | |
end | |
def write_log_samples | |
samples_size = [1, 2, 5, 10] | |
samples_size.each do |s| | |
batch_binary = serailized_batch(s, false) | |
puts batch_binary | |
filename = 'logbatch' + s.to_s + '.bin' | |
File.open(filename, 'wb') do |file| | |
file.write(batch_binary) | |
end | |
puts '+++' | |
batch_json = serailized_batch(s, true) | |
puts batch_json | |
filename = 'logbatch' + s.to_s + '.json' | |
File.open(filename, 'wb') do |file| | |
file.write(batch_json) | |
end | |
puts '======' | |
end | |
end | |
write_log_samples |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Download and generate samples: