Skip to content

Commit 6e9045d

Browse files
committed
refactor: Remove custom protobuf time conversion functions
1 parent 6519dd8 commit 6e9045d

File tree

8 files changed

+51
-75
lines changed

8 files changed

+51
-75
lines changed

internal/types/jobs.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package types
33
import (
44
"time"
55

6+
"google.golang.org/protobuf/types/known/durationpb"
7+
"google.golang.org/protobuf/types/known/timestamppb"
8+
69
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
710
)
811

@@ -73,11 +76,11 @@ func ConvertJob(job Job) *typespb.Job {
7376
FailureReason: job.FailureReason,
7477

7578
Timestamps: &typespb.JobTimestamps{
76-
CreatedAt: ConvertTime(job.CreatedAt),
77-
QueuedAt: ConvertTime(job.QueuedAt),
78-
StartedAt: ConvertTime(job.StartedAt),
79-
FinishedAt: ConvertTime(job.FinishedAt),
80-
ErasedAt: ConvertTime(job.ErasedAt),
79+
CreatedAt: timestamppb.New(valOrZero(job.CreatedAt)),
80+
QueuedAt: timestamppb.New(valOrZero(job.QueuedAt)),
81+
StartedAt: timestamppb.New(valOrZero(job.StartedAt)),
82+
FinishedAt: timestamppb.New(valOrZero(job.FinishedAt)),
83+
ErasedAt: timestamppb.New(valOrZero(job.ErasedAt)),
8184
},
8285

8386
QueuedDuration: durationpb.New(job.QueuedDuration),

internal/types/merge_request.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package types
33
import (
44
"time"
55

6+
"google.golang.org/protobuf/types/known/timestamppb"
7+
68
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
79
)
810

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

98100
Timestamps: &typespb.MergeRequestTimestamps{
99-
CreatedAt: ConvertTime(mr.CreatedAt),
100-
UpdatedAt: ConvertTime(mr.UpdatedAt),
101-
MergedAt: ConvertTime(mr.MergedAt),
102-
ClosedAt: ConvertTime(mr.ClosedAt),
101+
CreatedAt: timestamppb.New(valOrZero(mr.CreatedAt)),
102+
UpdatedAt: timestamppb.New(valOrZero(mr.UpdatedAt)),
103+
MergedAt: timestamppb.New(valOrZero(mr.MergedAt)),
104+
ClosedAt: timestamppb.New(valOrZero(mr.ClosedAt)),
103105
},
104106

105107
Name: mr.Name,
@@ -206,9 +208,9 @@ func ConvertMergeRequestNoteEvent(event MergeRequestNoteEvent) *typespb.MergeReq
206208
Id: int64(event.Id),
207209
MergeRequest: ConvertMergeRequestReference(event.MergeRequest),
208210

209-
CreatedAt: ConvertTime(event.CreatedAt),
210-
UpdatedAt: ConvertTime(event.UpdatedAt),
211-
ResolvedAt: ConvertTime(event.ResolvedAt),
211+
CreatedAt: timestamppb.New(valOrZero(event.CreatedAt)),
212+
UpdatedAt: timestamppb.New(valOrZero(event.UpdatedAt)),
213+
ResolvedAt: timestamppb.New(valOrZero(event.ResolvedAt)),
212214

213215
Type: event.Type,
214216
System: event.System,

internal/types/metrics.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package types
22

3-
import "github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
3+
import (
4+
"time"
5+
6+
"google.golang.org/protobuf/types/known/timestamppb"
7+
8+
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
9+
)
410

511
type Metric struct {
612
Id string
@@ -30,6 +36,6 @@ func ConvertMetric(metric Metric) *typespb.Metric {
3036
Name: metric.Name,
3137
Labels: labels,
3238
Value: metric.Value,
33-
Timestamp: ConvertUnixMilli(metric.Timestamp),
39+
Timestamp: timestamppb.New(time.UnixMilli(metric.Timestamp)),
3440
}
3541
}

internal/types/pipeline.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package types
33
import (
44
"time"
55

6+
"google.golang.org/protobuf/types/known/durationpb"
7+
"google.golang.org/protobuf/types/known/timestamppb"
8+
69
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
710
)
811

@@ -68,15 +71,15 @@ func ConvertPipeline(pipeline Pipeline) *typespb.Pipeline {
6871
Status: pipeline.Status,
6972

7073
Timestamps: &typespb.PipelineTimestamps{
71-
CommittedAt: ConvertTime(pipeline.CommittedAt),
72-
CreatedAt: ConvertTime(pipeline.CreatedAt),
73-
UpdatedAt: ConvertTime(pipeline.UpdatedAt),
74-
StartedAt: ConvertTime(pipeline.StartedAt),
75-
FinishedAt: ConvertTime(pipeline.FinishedAt),
74+
CommittedAt: timestamppb.New(valOrZero(pipeline.CommittedAt)),
75+
CreatedAt: timestamppb.New(valOrZero(pipeline.CreatedAt)),
76+
UpdatedAt: timestamppb.New(valOrZero(pipeline.UpdatedAt)),
77+
StartedAt: timestamppb.New(valOrZero(pipeline.StartedAt)),
78+
FinishedAt: timestamppb.New(valOrZero(pipeline.FinishedAt)),
7679
},
7780

78-
QueuedDuration: ConvertDuration(float64(pipeline.QueuedDuration)),
79-
Duration: ConvertDuration(float64(pipeline.Duration)),
81+
QueuedDuration: durationpb.New(pipeline.QueuedDuration),
82+
Duration: durationpb.New(pipeline.Duration),
8083
Coverage: pipeline.Coverage,
8184

8285
Warnings: pipeline.Warnings,

internal/types/project.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package types
33
import (
44
"time"
55

6+
"google.golang.org/protobuf/types/known/timestamppb"
7+
68
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
79
)
810

@@ -74,9 +76,9 @@ func ConvertProject(p Project) *typespb.Project {
7476
FullPath: p.FullPath,
7577

7678
Timestamps: &typespb.ProjectTimestamps{
77-
CreatedAt: ConvertTime(p.CreatedAt),
78-
UpdatedAt: ConvertTime(p.UpdatedAt),
79-
LastActivityAt: ConvertTime(p.LastActivityAt),
79+
CreatedAt: timestamppb.New(valOrZero(p.CreatedAt)),
80+
UpdatedAt: timestamppb.New(valOrZero(p.UpdatedAt)),
81+
LastActivityAt: timestamppb.New(valOrZero(p.LastActivityAt)),
8082
},
8183

8284
Statistics: convertProjectStatistics(p.Statistics),

internal/types/sections.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package types
33
import (
44
"time"
55

6+
"google.golang.org/protobuf/types/known/durationpb"
7+
"google.golang.org/protobuf/types/known/timestamppb"
8+
69
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
710
)
811

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

2427
Name: section.Name,
25-
StartedAt: ConvertTime(section.StartedAt),
26-
FinishedAt: ConvertTime(section.FinishedAt),
27-
Duration: ConvertDuration(section.Duration.Seconds()),
28+
StartedAt: timestamppb.New(valOrZero(section.StartedAt)),
29+
FinishedAt: timestamppb.New(valOrZero(section.FinishedAt)),
30+
Duration: durationpb.New(section.Duration),
2831
}
2932
}

internal/types/traces.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
99
resourcepb "go.opentelemetry.io/proto/otlp/resource/v1"
1010
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
11-
"google.golang.org/protobuf/types/known/timestamppb"
1211
)
1312

1413
func PipelineSpan(pipeline Pipeline) *tracepb.Span {
@@ -202,11 +201,3 @@ func convertStatus(status string) *tracepb.Status {
202201
Code: code,
203202
}
204203
}
205-
206-
func timestampUnixNano(ts *timestamppb.Timestamp) uint64 {
207-
if ts == nil {
208-
return 0
209-
}
210-
const nsPerSecond uint64 = 1_000_000_000
211-
return uint64(ts.Seconds)*nsPerSecond + uint64(ts.Nanos)
212-
}

internal/types/types.go

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,9 @@
11
package types
22

3-
import (
4-
"time"
5-
6-
"google.golang.org/protobuf/types/known/durationpb"
7-
"google.golang.org/protobuf/types/known/timestamppb"
8-
)
9-
10-
func ConvertTime(t *time.Time) *timestamppb.Timestamp {
11-
if t == nil {
12-
return nil
3+
func valOrZero[T any](p *T) T {
4+
var v T
5+
if p != nil {
6+
v = *p
137
}
14-
return timestamppb.New(*t)
15-
}
16-
17-
func ConvertUnixSeconds(ts int64) *timestamppb.Timestamp {
18-
return &timestamppb.Timestamp{
19-
Seconds: ts,
20-
Nanos: 0,
21-
}
22-
}
23-
24-
func ConvertUnixMilli(ts int64) *timestamppb.Timestamp {
25-
const msPerSecond int64 = 1_000
26-
const nsPerMilli int64 = 1_000
27-
return &timestamppb.Timestamp{
28-
Seconds: ts / msPerSecond,
29-
Nanos: int32((ts % msPerSecond) * nsPerMilli),
30-
}
31-
}
32-
33-
func ConvertUnixNano(ts int64) *timestamppb.Timestamp {
34-
const nsPerSecond int64 = 1_000_000_000
35-
return &timestamppb.Timestamp{
36-
Seconds: ts / nsPerSecond,
37-
Nanos: int32(ts % nsPerSecond),
38-
}
39-
}
40-
41-
func ConvertDuration(d float64) *durationpb.Duration {
42-
return durationpb.New(time.Duration(d * float64(time.Second)))
8+
return v
439
}

0 commit comments

Comments
 (0)