@@ -4,13 +4,15 @@ import (
44 "encoding/json"
55 "flag"
66 "fmt"
7- "os"
87 "log"
98 "net/http"
9+ "os"
10+ "strconv"
11+
1012 "github.com/confluentinc/confluent-kafka-go/kafka"
11- "github.com/teslamotors/fleet-telemetry/protos"
1213 "github.com/prometheus/client_golang/prometheus"
1314 "github.com/prometheus/client_golang/prometheus/promhttp"
15+ "github.com/teslamotors/fleet-telemetry/protos"
1416 "google.golang.org/protobuf/proto"
1517)
1618
@@ -97,19 +99,13 @@ func main() {
9799 continue
98100 }
99101
100- // Output the message to console
101- vehicleDataJSON , err := json .MarshalIndent (vehicleData , "" , " " )
102- if err != nil {
103- log .Printf ("Failed to marshal vehicle data to JSON: %v\n " , err )
104- continue
105- }
106- fmt .Printf ("Received message: %s\n " , vehicleDataJSON )
107-
102+ // Print the VIN
103+ fmt .Printf ("VIN: %s\n " , vehicleData .Vin )
108104 // Process each Datum in the Payload
109105 for _ , datum := range vehicleData .Data {
110106 fieldName := datum .Key .String () // Get the field name from the enum
111107 value := datum .Value
112-
108+ fmt . Printf ( "Field Name: %s, Value: %v \n " , fieldName , value )
113109 switch v := value .Value .(type ) {
114110 case * protos.Value_DoubleValue :
115111 vehicleDataGauge .WithLabelValues (fieldName , vehicleData .Vin ).Set (v .DoubleValue )
@@ -128,10 +124,15 @@ func main() {
128124 }
129125 vehicleDataGauge .WithLabelValues (fieldName , vehicleData .Vin ).Set (numericValue )
130126 case * protos.Value_StringValue :
131- // Handle string value by setting a metric label
132- vehicleDataGauge .WithLabelValues (fieldName , vehicleData .Vin ).Set (0 ) // Placeholder value
133- // Alternatively, you might consider using a different metric type or logging
134- log .Printf ("Received string value for field %s: %s" , fieldName , v .StringValue )
127+ // Try to parse the string value as a float64
128+ floatVal , err := strconv .ParseFloat (v .StringValue , 64 )
129+ if err == nil {
130+ vehicleDataGauge .WithLabelValues (fieldName , vehicleData .Vin ).Set (floatVal )
131+ } else {
132+ // Handle non-numeric string values
133+ vehicleDataGauge .WithLabelValues (fieldName , vehicleData .Vin ).Set (0 ) // Placeholder value
134+ log .Printf ("Received non-numeric string value for field %s: %s" , fieldName , v .StringValue )
135+ }
135136 case * protos.Value_Invalid :
136137 // Handle invalid value
137138 log .Printf ("Received invalid value for field %s" , fieldName )
0 commit comments