Skip to content

Commit

Permalink
refactor: Remove custom protobuf time conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cluttrdev committed Nov 1, 2024
1 parent 6519dd8 commit 6e9045d
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 75 deletions.
13 changes: 8 additions & 5 deletions internal/types/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package types
import (
"time"

"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
)

Expand Down Expand Up @@ -73,11 +76,11 @@ func ConvertJob(job Job) *typespb.Job {
FailureReason: job.FailureReason,

Timestamps: &typespb.JobTimestamps{
CreatedAt: ConvertTime(job.CreatedAt),
QueuedAt: ConvertTime(job.QueuedAt),
StartedAt: ConvertTime(job.StartedAt),
FinishedAt: ConvertTime(job.FinishedAt),
ErasedAt: ConvertTime(job.ErasedAt),
CreatedAt: timestamppb.New(valOrZero(job.CreatedAt)),
QueuedAt: timestamppb.New(valOrZero(job.QueuedAt)),
StartedAt: timestamppb.New(valOrZero(job.StartedAt)),
FinishedAt: timestamppb.New(valOrZero(job.FinishedAt)),
ErasedAt: timestamppb.New(valOrZero(job.ErasedAt)),
},

QueuedDuration: durationpb.New(job.QueuedDuration),
Expand Down
16 changes: 9 additions & 7 deletions internal/types/merge_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
"time"

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
)

Expand Down Expand Up @@ -96,10 +98,10 @@ func ConvertMergeRequest(mr MergeRequest) *typespb.MergeRequest {
Project: ConvertProjectReference(mr.Project),

Timestamps: &typespb.MergeRequestTimestamps{
CreatedAt: ConvertTime(mr.CreatedAt),
UpdatedAt: ConvertTime(mr.UpdatedAt),
MergedAt: ConvertTime(mr.MergedAt),
ClosedAt: ConvertTime(mr.ClosedAt),
CreatedAt: timestamppb.New(valOrZero(mr.CreatedAt)),
UpdatedAt: timestamppb.New(valOrZero(mr.UpdatedAt)),
MergedAt: timestamppb.New(valOrZero(mr.MergedAt)),
ClosedAt: timestamppb.New(valOrZero(mr.ClosedAt)),
},

Name: mr.Name,
Expand Down Expand Up @@ -206,9 +208,9 @@ func ConvertMergeRequestNoteEvent(event MergeRequestNoteEvent) *typespb.MergeReq
Id: int64(event.Id),
MergeRequest: ConvertMergeRequestReference(event.MergeRequest),

CreatedAt: ConvertTime(event.CreatedAt),
UpdatedAt: ConvertTime(event.UpdatedAt),
ResolvedAt: ConvertTime(event.ResolvedAt),
CreatedAt: timestamppb.New(valOrZero(event.CreatedAt)),
UpdatedAt: timestamppb.New(valOrZero(event.UpdatedAt)),
ResolvedAt: timestamppb.New(valOrZero(event.ResolvedAt)),

Type: event.Type,
System: event.System,
Expand Down
10 changes: 8 additions & 2 deletions internal/types/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package types

import "github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
import (
"time"

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
)

type Metric struct {
Id string
Expand Down Expand Up @@ -30,6 +36,6 @@ func ConvertMetric(metric Metric) *typespb.Metric {
Name: metric.Name,
Labels: labels,
Value: metric.Value,
Timestamp: ConvertUnixMilli(metric.Timestamp),
Timestamp: timestamppb.New(time.UnixMilli(metric.Timestamp)),
}
}
17 changes: 10 additions & 7 deletions internal/types/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package types
import (
"time"

"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
)

Expand Down Expand Up @@ -68,15 +71,15 @@ func ConvertPipeline(pipeline Pipeline) *typespb.Pipeline {
Status: pipeline.Status,

Timestamps: &typespb.PipelineTimestamps{
CommittedAt: ConvertTime(pipeline.CommittedAt),
CreatedAt: ConvertTime(pipeline.CreatedAt),
UpdatedAt: ConvertTime(pipeline.UpdatedAt),
StartedAt: ConvertTime(pipeline.StartedAt),
FinishedAt: ConvertTime(pipeline.FinishedAt),
CommittedAt: timestamppb.New(valOrZero(pipeline.CommittedAt)),
CreatedAt: timestamppb.New(valOrZero(pipeline.CreatedAt)),
UpdatedAt: timestamppb.New(valOrZero(pipeline.UpdatedAt)),
StartedAt: timestamppb.New(valOrZero(pipeline.StartedAt)),
FinishedAt: timestamppb.New(valOrZero(pipeline.FinishedAt)),
},

QueuedDuration: ConvertDuration(float64(pipeline.QueuedDuration)),
Duration: ConvertDuration(float64(pipeline.Duration)),
QueuedDuration: durationpb.New(pipeline.QueuedDuration),
Duration: durationpb.New(pipeline.Duration),
Coverage: pipeline.Coverage,

Warnings: pipeline.Warnings,
Expand Down
8 changes: 5 additions & 3 deletions internal/types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
"time"

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
)

Expand Down Expand Up @@ -74,9 +76,9 @@ func ConvertProject(p Project) *typespb.Project {
FullPath: p.FullPath,

Timestamps: &typespb.ProjectTimestamps{
CreatedAt: ConvertTime(p.CreatedAt),
UpdatedAt: ConvertTime(p.UpdatedAt),
LastActivityAt: ConvertTime(p.LastActivityAt),
CreatedAt: timestamppb.New(valOrZero(p.CreatedAt)),
UpdatedAt: timestamppb.New(valOrZero(p.UpdatedAt)),
LastActivityAt: timestamppb.New(valOrZero(p.LastActivityAt)),
},

Statistics: convertProjectStatistics(p.Statistics),
Expand Down
9 changes: 6 additions & 3 deletions internal/types/sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package types
import (
"time"

"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
)

Expand All @@ -22,8 +25,8 @@ func ConvertSection(section Section) *typespb.Section {
Job: ConvertJobReference(section.Job),

Name: section.Name,
StartedAt: ConvertTime(section.StartedAt),
FinishedAt: ConvertTime(section.FinishedAt),
Duration: ConvertDuration(section.Duration.Seconds()),
StartedAt: timestamppb.New(valOrZero(section.StartedAt)),
FinishedAt: timestamppb.New(valOrZero(section.FinishedAt)),
Duration: durationpb.New(section.Duration),
}
}
9 changes: 0 additions & 9 deletions internal/types/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
resourcepb "go.opentelemetry.io/proto/otlp/resource/v1"
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
"google.golang.org/protobuf/types/known/timestamppb"
)

func PipelineSpan(pipeline Pipeline) *tracepb.Span {
Expand Down Expand Up @@ -202,11 +201,3 @@ func convertStatus(status string) *tracepb.Status {
Code: code,
}
}

func timestampUnixNano(ts *timestamppb.Timestamp) uint64 {
if ts == nil {
return 0
}
const nsPerSecond uint64 = 1_000_000_000
return uint64(ts.Seconds)*nsPerSecond + uint64(ts.Nanos)
}
44 changes: 5 additions & 39 deletions internal/types/types.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
package types

import (
"time"

"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

func ConvertTime(t *time.Time) *timestamppb.Timestamp {
if t == nil {
return nil
func valOrZero[T any](p *T) T {
var v T
if p != nil {
v = *p
}
return timestamppb.New(*t)
}

func ConvertUnixSeconds(ts int64) *timestamppb.Timestamp {
return &timestamppb.Timestamp{
Seconds: ts,
Nanos: 0,
}
}

func ConvertUnixMilli(ts int64) *timestamppb.Timestamp {
const msPerSecond int64 = 1_000
const nsPerMilli int64 = 1_000
return &timestamppb.Timestamp{
Seconds: ts / msPerSecond,
Nanos: int32((ts % msPerSecond) * nsPerMilli),
}
}

func ConvertUnixNano(ts int64) *timestamppb.Timestamp {
const nsPerSecond int64 = 1_000_000_000
return &timestamppb.Timestamp{
Seconds: ts / nsPerSecond,
Nanos: int32(ts % nsPerSecond),
}
}

func ConvertDuration(d float64) *durationpb.Duration {
return durationpb.New(time.Duration(d * float64(time.Second)))
return v
}

0 comments on commit 6e9045d

Please sign in to comment.