Skip to content

Commit 0dc3f63

Browse files
author
Vipul Rawat
authored
add error and debug logs for google and mqtt (#399)
1 parent 10113ff commit 0dc3f63

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

pkg/gofr/datasource/pubsub/google/google.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func (g *googleClient) Publish(ctx context.Context, topic string, message []byte
7070

7171
t, err := g.getTopic(ctx, topic)
7272
if err != nil {
73+
g.logger.Errorf("error creating %s err: %v", topic, err)
74+
7375
return err
7476
}
7577

@@ -80,6 +82,8 @@ func (g *googleClient) Publish(ctx context.Context, topic string, message []byte
8082

8183
_, err = result.Get(ctx)
8284
if err != nil {
85+
g.logger.Errorf("error publishing to google topic %s err: %v", topic, err)
86+
8387
return err
8488
}
8589

@@ -120,7 +124,7 @@ func (g *googleClient) Subscribe(ctx context.Context, topic string) (*pubsub.Mes
120124
})
121125

122126
if err != nil {
123-
g.logger.Errorf("Error getting a message: %s", err.Error())
127+
g.logger.Errorf("error getting a message from google: %s", err.Error())
124128

125129
return nil, err
126130
}

pkg/gofr/datasource/pubsub/google/google_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestGoogleClient_PublishTopic_Error(t *testing.T) {
9393
g := &googleClient{client: getGoogleClient(t), Config: Config{
9494
ProjectID: "test",
9595
SubscriptionName: "sub",
96-
}, metrics: mockMetrics}
96+
}, metrics: mockMetrics, logger: testutil.NewMockLogger(testutil.DEBUGLOG)}
9797
defer g.client.Close()
9898

9999
ctx, cancel := context.WithCancel(context.Background())

pkg/gofr/datasource/pubsub/mqtt/mqtt.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ func New(config *Config, logger Logger, metrics Metrics) *MQTT {
6363
client := mqtt.NewClient(options)
6464

6565
if token := client.Connect(); token.Wait() && token.Error() != nil {
66-
logger.Errorf("cannot connect to MQTT, HostName : %v, Port : %v, error : %v", config.Hostname, config.Port, token.Error())
66+
logger.Errorf("cannot connect to MQTT, host: %v, port: %v, error: %v", config.Hostname, config.Port, token.Error())
6767

6868
return &MQTT{Client: client, config: config, logger: logger}
6969
}
7070

7171
msg := make(map[string]chan *pubsub.Message)
7272

73-
logger.Debugf("connected to MQTT, HostName : %v, Port : %v", config.Hostname, config.Port)
73+
logger.Debugf("connected to MQTT, host: %v, port: %v", config.Hostname, config.Port)
7474

7575
return &MQTT{Client: client, config: config, logger: logger, msgChanMap: msg, mu: new(sync.RWMutex), metrics: metrics}
7676
}
@@ -82,15 +82,13 @@ func getDefaultClient(config *Config, logger Logger, metrics Metrics) *MQTT {
8282
clientID = getClientID(config.ClientID)
8383
)
8484

85-
logger.Debugf("using %v clientID for this session", clientID)
86-
8785
opts := mqtt.NewClientOptions()
8886
opts.AddBroker(fmt.Sprintf("tcp://%s:%d", host, port))
8987
opts.SetClientID(clientID)
9088
client := mqtt.NewClient(opts)
9189

9290
if token := client.Connect(); token.Wait() && token.Error() != nil {
93-
logger.Errorf("cannot connect to MQTT, HostName : %v, Port : %v, error : %v", host, port, token.Error())
91+
logger.Errorf("cannot connect to MQTT, host: %v, port: %v, error: %v", host, port, token.Error())
9492

9593
return &MQTT{Client: client, config: config, logger: logger}
9694
}
@@ -102,6 +100,7 @@ func getDefaultClient(config *Config, logger Logger, metrics Metrics) *MQTT {
102100
msg := make(map[string]chan *pubsub.Message)
103101

104102
logger.Debugf("connected to MQTT, HostName : %v, Port : %v", config.Hostname, config.Port)
103+
logger.Debugf("using %v clientID for this MQTT session", clientID)
105104

106105
return &MQTT{Client: client, config: config, logger: logger, msgChanMap: msg, mu: new(sync.RWMutex), metrics: metrics}
107106
}
@@ -174,9 +173,13 @@ func (m *MQTT) Subscribe(ctx context.Context, topic string) (*pubsub.Message, er
174173
token := m.Client.Subscribe(topic, m.config.QoS, handler)
175174

176175
if token.Wait() && token.Error() != nil {
176+
m.logger.Errorf("error getting a message from MQTT, err: %v", token.Error())
177+
177178
return nil, token.Error()
178179
}
179180

181+
m.logger.Debugf("received mqtt message %v on topic %v", string(messg.Value), topic)
182+
180183
m.metrics.IncrementCounter(ctx, "app_pubsub_subscribe_success_count", "topic", topic)
181184

182185
// blocks if there are no messages in the channel
@@ -196,6 +199,8 @@ func (m *MQTT) Publish(ctx context.Context, topic string, message []byte) error
196199
return token.Error()
197200
}
198201

202+
m.logger.Debugf("published message %v on topic %v", string(message), topic)
203+
199204
m.metrics.IncrementCounter(ctx, "app_pubsub_publish_success_count", "topic", topic)
200205

201206
return nil

0 commit comments

Comments
 (0)