How to unittest prometheus GaugeVec - unit-testing

I am new to golang so it's kind of hard to understand what would be the best way to test prometheus.GuageVec.
Here is my try 1. where i am trying to get metrics channel. But this request is timing out?
cb, _ := GaugeVecs[name]
ch := make(chan prometheus.Metric)
cb.Collect(ch)
metric := <-ch
fmt.Printf("%+v", metric)
The second try that i did was
cb, _ := GaugeVecs[name]
err := testutil.CollectAndCompare(cb, strings.NewReader(`expected outcome`), "name")
assert.Nil(t, err)
^^^ this one got error that parsing expected metrics failed: text format parsing error in line 1: unexpected end of input stream.
How can i test the output of metric?

Related

How to save S3 file to AWS lambda tmp directory and how to delete a file from AWS lambda tmp directory using golang

Need to download a file from s3 bucket and store in to tmp directory in lambda function. After that need apply grep command into file using "os/exec". I tried to do this algorithm using golang.
I tried through following way but it is not successful approach.
func MyHandler(ctx context.Context, s3Event events.S3Event) {
for _, record := range s3Event.Records {
s3record := record.S3
bucketName := s3record.Bucket.Name
fileName := s3record.Object.Key
download_path := "/tmp/"
file, err := os.Create(download_path + fileName)
if err != nil {
fmt.Println(err)
}
defer file.Close()
sess, _ := session.NewSession(&aws.Config{Region: aws.String("us-east-1")})
downloader := s3manager.NewDownloader(sess)
numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(fileName),
})
if err != nil {
fmt.Println(err)
}
fmt.Println("Downloaded", file.Name(), numBytes, "bytes")
}
}
Please verify may approach correct or suggest a correct approach to store the file from s3 bucket into tem folder of lambda function everything I need to do using golang.
I have following confusion also
Do we need to create directory folder in lambda function or it is a default directory already there.
Can we apply os/exec command into tem folder and can we grep the file.
Do we need to delete uploaded file from tem folder then How? or The file will be deleted a automatically.

Image upload failed using AWS SDK V2 in GO

I am using AWS S3 service to upload images. Yesterday I updated the SDK v1 to v2 and found that the image upload is failing with the following error:
operation error S3: PutObject, https response error StatusCode: 403, RequestID: XXXXXXXXXXX, HostID: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, api error SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your key and signing method.
UPDATED:
I have aws credentials on my home folder in linux in .aws folder in the following format:
[default]
aws_access_key_id = XXXXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
Here is the code:
package main
import (
"context"
"fmt"
"io"
"net/http"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func main() {
fileName := "test123.jpg"
filePath := "/BUCKET_NAME/uploads/aman/2021/6/25/"
res, err := http.Get("https://images.app.goo.gl/mpQ5nXYXjdUMKGgW7")
if err != nil || res.StatusCode != 200 {
// handle errors
}
defer res.Body.Close()
UploadFileInS3Bucket(res.Body, fileName, filePath)
}
func UploadFileInS3Bucket(file io.Reader, fileName, filePath string) {
cfg, err := awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion("REGION"),
)
client := s3.NewFromConfig(cfg)
uploader := manager.NewUploader(client)
uploadResp, err := uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(filePath),
Key: aws.String(fileName),
Body: file,
ContentType: aws.String("image"),
})
fmt.Println(uploadResp)
fmt.Println(err)
}
I did not change any credentials/buckets/regions in my code.However if I run the code with SDK v1 then it works fine & images are uploading.
What is going wrong with the SDK v2 ?
After spending a couple of days, I came to know that SDK V2 takes following format for Bucket & Key field:
fileName := "uploads/2021/6/25/test123.jpg"
filePath := "BUCKET_NAME"
Basically for these fields there is vice versa behaviour in SDK V1 & V2. Above is the V2. Below is the V1:
fileName := "test123.jpg"
filePath := "/BUCKET_NAME/uploads/2021/6/25/"

Getting SerializationException while trying to PutLogEvents on cloudwatch using golang

I am trying to achieve following using my program:
Create log-group on aws cloudwatch
Create log-stream under above log-group
Put log-events under above log-stream
All this using go lang
package main
import (
"time"
"fmt"
"github.com/jcxplorer/cwlogger"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws"
)
func main() {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := cloudwatchlogs.New(sess)
logGroupName := "my-log-group";
logStreamName := "my-log-stream";
logGroupInput := cloudwatchlogs.CreateLogGroupInput{LogGroupName: &logGroupName}
svc.CreateLogGroup(&logGroupInput);
logStreamInput := cloudwatchlogs.CreateLogStreamInput{LogGroupName: &logGroupName, LogStreamName: &logStreamName}
svc.CreateLogStream(&logStreamInput)
logevents := make([]*cloudwatchlogs.InputLogEvent, 1)
logevents = append(logevents, &cloudwatchlogs.InputLogEvent{
Message: aws.String("Simple log message"),
Timestamp: aws.Int64(111),
})
p := cloudwatchlogs.PutLogEventsInput{LogEvents: logevents, LogGroupName: &logGroupName, LogStreamName: &logStreamName}
resp, err := svc.PutLogEvents(&p)
if err != nil {
panic(err)
}
fmt.Print("Next Token: {}", resp.NextSequenceToken)
}
Now when I run above code, it successfully creates log-group and log-stream and I can verify that in aws cloudwatch. But for some reason PutLogEvents fails with following error:
panic: SerializationException:
status code: 400, request id: 0685efcc-47e3-11e9-b528-81f33ec2f468
I am not sure what may be wrong here. Any suggestion or direction will be really helpful.
Thanks in advance.
Reason for SerializationException was:logevents := make([]*cloudwatchlogs.InputLogEvent, 1)
followed by append which created first empty entry in slice. I replaced code with
logevents := make([]*cloudwatchlogs.InputLogEvent, 0) and it got resolved.
Additionally while debugging to find why logs were not getting populated I figured out that timestamp value used is not valid one. According to aws documentation timestamp for each event can't be older than 14 days and can't be more than 2hr in future.
Here is link: https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
Hope it will be helpful to someone facing similar issue in future.

How to execute an exe file from AWS lambda

I have written a lambda function that executes another exe file named abc.exe.
Now I have created a zip of lambda function and uploaded it to aws. I am not sure where to put my "abc.exe"
I tried putting it in the same zip but I get below error:
exec: "abc": executable file not found in $PATH:
Here is my lambda function code:
func HandleLambdaEvent(request Request) (Response, error) {
fmt.Println("Input", request.Input)
fmt.Println("Output", request.Output)
cmd := exec.Command("abc", "-v", "--lambda", request.Input, "--out", request.Output)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
return Response{Message: fmt.Sprintf(stderr.String())}, nil
}
fmt.Println("Result: " + out.String())
return Response{Message: fmt.Sprintf(" %s and %s are input and output", request.Input, request.Output)}, nil
}
Update:
Trial 1:
I uploaded abc.exe to s3 then in my HandleLambdaEvent function I am downloading it to tmp/ folder. And next when i try to access it after successful download, it shows below error:
fork/exec /tmp/abc: no such file or directory:
Code to download abc.exe :
file, err2 := os.Create("tmp/abc.exe")
if err2 != nil {
fmt.Println("Unable to create file %q, %v", err2)
}
defer file.Close()
sess, _ := session.NewSession(&aws.Config{
Region: aws.String(region)},
)
downloader := s3manager.NewDownloader(sess)
numBytes, err2 := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String("abc.exe"),
})
if err2 != nil {
fmt.Println("Unable to download item %q, %v", fileName, err2)
}
fmt.Println("Downloaded", file.Name(), numBytes, "bytes")
file.Close()
are you sure you even can execute an external binary? That seems counter-intuitive to me, like it violates the point of Lambda
Perfectly acceptable. Have a look at Running Arbitrary Executables in AWS Lambda on the AWS Compute Blog.
I am not sure where to put my "abc.exe"
To run executables on Lambda package them in the ZIP file you upload. Then do something like
exec.Command(path.Join(os.GetEnv("LAMBDA_TASK_ROOT"), "abc.exe"))
What sort of file is the .exe file? Is it a Windows app?
You won't be able to run Windows apps on Lambda. The linked blog post says: If you compile your own binaries, ensure that they’re either statically linked or built for the matching version of Amazon Linux

AWS SES does not send emails using SendRawEmail operation

I have troubles sending emails with AWS golang sdk using SendRawEmail operation. Even though I get no errors and receive MessageId back from AWS, I do not receive the email.
Sending emails using SendEmail works fine and I receive the email.
My code:
session, err := session.NewSession()
if err != nil {
return err
}
svc := ses.New(session, &aws.Config{Region: aws.String("eu-west-1")})
messageContent := `From: "Alice" <xxx#xxx>
To: "Bob" <xxx#xxx>
Return-Path: <xxx#xxx>
Subject: Hello
Content-Language: en-US
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
This is a test email`
base64messageContent := base64.StdEncoding.EncodeToString([]byte(messageContent))
source := aws.String("xxx#xxx")
destinations := []*string{aws.String("xxx#xxx")}
message := ses.RawMessage{Data: []byte(base64messageContent)}
input := ses.SendRawEmailInput{Source: source, Destinations: destinations, RawMessage: &message}
output, err := svc.SendRawEmail(&input)
if err != nil {
return err
}
log.Println("Response from SES", output)
return nil
}
I am using my Gmail as the destination email, if that makes any difference.
Data in RawData should not be base64 encoded. As documentation states:
// Data is automatically base64 encoded/decoded by the SDK.