-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first try to implement steps for RabbitMQ * fix ci * Added new steps and ATs * added return statement * revert change in return statement * correlator added in the message steps * fix: apply suggested changes Co-authored-by: Ismael Taboada Rodero <[email protected]> * fix: typos * fix: step is added for parallel execution of ATs * fix: feedback received with the steps * fix: debugf format * fix: new step added for failure cases * fix: feedback received * Remove correlationId use * fix: refactor unsubscribe step Co-authored-by: Ismael Taboada Rodero <[email protected]> Co-authored-by: Cesar Brea <[email protected]> Co-authored-by: Mario LG <[email protected]>
- Loading branch information
1 parent
5ccba19
commit ea4da6c
Showing
10 changed files
with
685 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2021 Telefonica Cybersecurity & Cloud Tech SL | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rabbit | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// ContextKey defines a type to store the rabbit session in context.Context. | ||
type ContextKey string | ||
|
||
var contextKey ContextKey = "rabbitSession" | ||
|
||
// InitializeContext adds the rabbit session to the context. | ||
// The new context is returned because context is immutable. | ||
func InitializeContext(ctx context.Context) context.Context { | ||
var session Session | ||
return context.WithValue(ctx, contextKey, &session) | ||
} | ||
|
||
// GetSession returns the rabbit session stored in context. | ||
// Note that the context should be previously initialized with InitializeContext function. | ||
func GetSession(ctx context.Context) *Session { | ||
return ctx.Value(contextKey).(*Session) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2021 Telefonica Cybersecurity & Cloud Tech SL | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rabbit | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path" | ||
|
||
"github.com/Telefonica/golium" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var logger *Logger | ||
|
||
// GetLogger returns the logger for rabbit messages in publish/subscribe. | ||
// If the logger is not created yet, it creates a new instance of Logger. | ||
func GetLogger() *Logger { | ||
if logger != nil { | ||
return logger | ||
} | ||
dir := golium.GetConfig().Log.Directory | ||
path := path.Join(dir, "rabbit-pubsub.log") | ||
logger, err := NewLogger(path) | ||
if err != nil { | ||
logrus.Fatalf("Error creating rabbit logger with file: '%s'. %s", path, err) | ||
} | ||
return logger | ||
} | ||
|
||
// Logger logs the HTTP request and response in a configurable file. | ||
type Logger struct { | ||
log *log.Logger | ||
} | ||
|
||
// NewLogger creates an instance of the logger. | ||
// It configures the file path where the RabbitMQ interactions are written. | ||
func NewLogger(path string) (*Logger, error) { | ||
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Logger{ | ||
log: log.New(file, "", log.Ldate|log.Lmicroseconds|log.LUTC), | ||
}, nil | ||
} | ||
|
||
// LogPublishedMessage logs a rabbit message published to a topic. | ||
func (l Logger) LogPublishedMessage(msg, topic, corr string) { | ||
l.log.Printf("Publish to %s [%s]:\n%s\n\n", topic, corr, msg) | ||
} | ||
|
||
// LogReceivedMessage logs a rabbit message received from a topic. | ||
func (l Logger) LogReceivedMessage(msg, topic, corr string) { | ||
l.log.Printf("Received from %s [%s]:\n%s\n\n", topic, corr, msg) | ||
} |
Oops, something went wrong.