Skip to content

Instantly share code, notes, and snippets.

@iamazeem
Created June 9, 2020 14:41
Embed
What would you like to do?
ProtoBuf Schema and Ruby Script for Generating Messages Samples (Binary and JSON)
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;
}
#!/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
@iamazeem
Copy link
Author

Download and generate samples:

wget https://gist.githubusercontent.com/iamazeem/a8a24092132e1741a76956192f2104cc/raw/dd0e4f9ac19ba89c5bd65f0cb594332c8f475e45/logbatch.rb
wget https://gist.githubusercontent.com/iamazeem/a8a24092132e1741a76956192f2104cc/raw/dd0e4f9ac19ba89c5bd65f0cb594332c8f475e45/log.proto

protoc --ruby_out=. log.proto
chmod +x logbatch.rb
./logbatch.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment