@@ -8,8 +8,11 @@ import (
8
8
"io"
9
9
"net/http"
10
10
"sort"
11
+ "strings"
11
12
"time"
12
13
14
+ "github.com/go-kit/log"
15
+ "github.com/go-kit/log/level"
13
16
"github.com/pkg/errors"
14
17
"github.com/prometheus/common/model"
15
18
"github.com/prometheus/prometheus/model/labels"
@@ -40,14 +43,14 @@ func newPushStats() *Stats {
40
43
}
41
44
}
42
45
43
- func ParseOTLPRequest (userID string , r * http.Request , tenantsRetention TenantsRetention , limits Limits , tracker UsageTracker ) (* logproto.PushRequest , * Stats , error ) {
46
+ func ParseOTLPRequest (userID string , r * http.Request , tenantsRetention TenantsRetention , limits Limits , tracker UsageTracker , logPushRequestStreams bool , logger log. Logger ) (* logproto.PushRequest , * Stats , error ) {
44
47
stats := newPushStats ()
45
48
otlpLogs , err := extractLogs (r , stats )
46
49
if err != nil {
47
50
return nil , nil , err
48
51
}
49
52
50
- req := otlpToLokiPushRequest (r .Context (), otlpLogs , userID , tenantsRetention , limits .OTLPConfig (userID ), limits .DiscoverServiceName (userID ), tracker , stats )
53
+ req := otlpToLokiPushRequest (r .Context (), otlpLogs , userID , tenantsRetention , limits .OTLPConfig (userID ), limits .DiscoverServiceName (userID ), tracker , stats , logPushRequestStreams , logger )
51
54
return req , stats , nil
52
55
}
53
56
@@ -98,7 +101,7 @@ func extractLogs(r *http.Request, pushStats *Stats) (plog.Logs, error) {
98
101
return req .Logs (), nil
99
102
}
100
103
101
- func otlpToLokiPushRequest (ctx context.Context , ld plog.Logs , userID string , tenantsRetention TenantsRetention , otlpConfig OTLPConfig , discoverServiceName []string , tracker UsageTracker , stats * Stats ) * logproto.PushRequest {
104
+ func otlpToLokiPushRequest (ctx context.Context , ld plog.Logs , userID string , tenantsRetention TenantsRetention , otlpConfig OTLPConfig , discoverServiceName []string , tracker UsageTracker , stats * Stats , logPushRequestStreams bool , logger log. Logger ) * logproto.PushRequest {
102
105
if ld .LogRecordCount () == 0 {
103
106
return & logproto.PushRequest {}
104
107
}
@@ -113,6 +116,10 @@ func otlpToLokiPushRequest(ctx context.Context, ld plog.Logs, userID string, ten
113
116
114
117
resourceAttributesAsStructuredMetadata := make (push.LabelsAdapter , 0 , resAttrs .Len ())
115
118
streamLabels := make (model.LabelSet , 30 ) // we have a default labels limit of 30 so just initialize the map of same size
119
+ var pushedLabels model.LabelSet
120
+ if logPushRequestStreams {
121
+ pushedLabels = make (model.LabelSet , 30 )
122
+ }
116
123
117
124
shouldDiscoverServiceName := len (discoverServiceName ) > 0 && ! stats .IsAggregatedMetric
118
125
hasServiceName := false
@@ -129,6 +136,9 @@ func otlpToLokiPushRequest(ctx context.Context, ld plog.Logs, userID string, ten
129
136
if action == IndexLabel {
130
137
for _ , lbl := range attributeAsLabels {
131
138
streamLabels [model .LabelName (lbl .Name )] = model .LabelValue (lbl .Value )
139
+ if logPushRequestStreams && pushedLabels != nil {
140
+ pushedLabels [model .LabelName (lbl .Name )] = model .LabelValue (lbl .Value )
141
+ }
132
142
133
143
if ! hasServiceName && shouldDiscoverServiceName {
134
144
for _ , labelName := range discoverServiceName {
@@ -151,6 +161,23 @@ func otlpToLokiPushRequest(ctx context.Context, ld plog.Logs, userID string, ten
151
161
streamLabels [model .LabelName (LabelServiceName )] = model .LabelValue (ServiceUnknown )
152
162
}
153
163
164
+ if logPushRequestStreams {
165
+ var sb strings.Builder
166
+ sb .WriteString ("{" )
167
+ labels := make ([]string , 0 , len (pushedLabels ))
168
+ for name , value := range pushedLabels {
169
+ labels = append (labels , fmt .Sprintf (`%s="%s"` , name , value ))
170
+ }
171
+ sb .WriteString (strings .Join (labels , ", " ))
172
+ sb .WriteString ("}" )
173
+
174
+ level .Debug (logger ).Log (
175
+ "msg" , "OTLP push request stream before service name discovery" ,
176
+ "stream" , sb .String (),
177
+ "service_name" , streamLabels [model .LabelName (LabelServiceName )],
178
+ )
179
+ }
180
+
154
181
if err := streamLabels .Validate (); err != nil {
155
182
stats .Errs = append (stats .Errs , fmt .Errorf ("invalid labels: %w" , err ))
156
183
continue
0 commit comments